iT邦幫忙

1

求大師解釋

c
eit 2019-12-26 21:04:356485 瀏覽
  • 分享至 

  • xImage

以下是一個算最大公因數和最小公倍數的程式
求各位大大說明

#include <stdio.h>

void t(int a,int b) {
int num = a * b;
if( a == 0 || b == 0) {
printf("Number can not be 0.\n");
return;
}
while( a > 0 && b > 0 ) {

if( a > b )
    a = a % b;
else
    b = b % a;

}
if( a == 0 ) {
printf("最大公因數為 %d\n",b);
printf("最小公倍數為 %d\n",(num/b));
}
else {
printf("最大公因數為 %d\n",a);
printf("最小公倍數為 %d\n",(num/a));
}
}
int main(void) {
int a,b;
int c;
printf("請輸入a,b求最大公因數及最小公倍數:");
scanf("%d %d",&a,&b);
t(a,b);
return 0;
}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
6
Darwin Watterson
iT邦好手 1 級 ‧ 2019-12-26 23:47:36
最佳解答

直接舉例說明給你了解:

求 (6, 15) 與 [6, 15]

int main(void) {
  int a,b;
  int c;
  printf("請輸入a,b求最大公因數及最小公倍數:");
  scanf("%d %d",&a,&b);

就只是從螢幕讀進你key入的 a=6 b=15
再來看

t(a, b);

實際就是讓電腦處理

t(6, 15);

進入函式後,先定義一個 num變數=6x15
理由是 「兩數的乘積 / 最大公因數 = 最大公倍數」
數學恆等式這裡不推導,詳情打電話問「國中老師」吧!/images/emoticon/emoticon39.gif

if( a == 0 || b == 0) {
  printf("Number can not be 0.\n");
  return;
}

由於6與15都大於0,上面這段「防呆」跳過不執行。

最後來看關鍵的部分

while(a>0 && b>0) {
  if(a>b) 
     a = a%b;
  else 
     b = b%a;
}

因爲 b>a 執行 else區段
所以第一次進入while迴圈的結果

a=6 b=3

不過因爲a與b都大於0 且 a>b 執行 if區段
所以第二次進入while迴圈的結果

a=0 b=3

a變0後就跳出while迴圈了!(此例迴圈判斷3次)

if( a == 0 ) {
  printf("最大公因數為 %d\n",b);
  printf("最小公倍數為 %d\n",(num/b));
} else {
  printf("最大公因數為 %d\n",a);
  printf("最小公倍數為 %d\n",(num/a));
}

因爲a=0 所以印出

最大公因數為 3
最小公倍數為 30

以上就是電腦計算的思維,可以試試 a=15 b=6
依照這個步驟多試試/images/emoticon/emoticon12.gif
這真的是入門程式必需經歷的過程啊!
你試過就是你的了!

1
小魚
iT邦大師 1 級 ‧ 2019-12-26 21:12:05

要說明什麼?
最大公因數 使用輾轉相除法 應該是基本觀念吧?
最小公倍數 是 兩者相乘/最大公因數 也算是基本觀念吧?
所以要解釋什麼?
如果還需要解釋可能要翻一下國中或國小的課本來看了.

看更多先前的回應...收起先前的回應...
eit iT邦新手 5 級 ‧ 2019-12-26 22:17:18 檢舉

int main(void) { 這一行之前的程式為什麼在執行後沒有看到執行的結果,而且為什麼可以有程式碼,有什麼特別的功用嗎?

小魚 iT邦大師 1 級 ‧ 2019-12-26 22:28:44 檢舉

所有的C++程式都會有一個
int main(void)
作為整個程式的主程式.
尤其是當你有幾十個幾百個檔案的時候,
這就是程式的進入點.
雖然他也可能是
int main()
或是
void main() //好像有些IDE不接受這個

我豆頁好痛~"~

eit iT邦新手 5 級 ‧ 2019-12-26 22:45:29 檢舉

喔喔!感謝

0
阿展展展
iT邦好手 1 級 ‧ 2019-12-26 21:53:20

所以你要問什麼/images/emoticon/emoticon19.gif/images/emoticon/emoticon19.gif

eit iT邦新手 5 級 ‧ 2019-12-26 22:01:59 檢舉

int main(void) { 這一行之前的程式為什麼在執行後沒有看到執行的結果,而且為什麼可以有程式碼,有什麼特別的功用嗎?

main是公認的程式進入點,說白了就是實際執行的區段或者更專業的說法就是ㄧ個執行緒。至於上面那段叫作函式。函式要動起來ㄧ定要被執行緒呼叫到/images/emoticon/emoticon10.gif

1
一級屠豬士
iT邦大師 1 級 ‧ 2019-12-27 00:29:46

上面已經有人說明了,這是function. 寫了一個很簡單的例子,給你參考一下.

#include <stdio.h>

void fun1(void);
void fun2(void);

int main(void) {
    printf("我是主程式\n");
    printf("主程式: 準備呼叫第一個 function\n");
    fun1();
    printf("主程式: 結束\n");
    return 0;
}

void fun1(void) {
    printf("我是第一個 function\n");
    printf("fun1: 準備呼叫第二個 function\n");
    fun2();
    printf("fun1: 第一個 function 結束\n");
}

void fun2(void) {
    printf("我是第二個 function\n");
    printf("fun2: 第二個 function 結束\n");
}

執行結果:

| => ./t2
我是主程式
主程式: 準備呼叫第一個 function
我是第一個 function
fun1: 準備呼叫第二個 function
我是第二個 function
fun2: 第二個 function 結束
fun1: 第一個 function 結束
主程式: 結束
0
ano
iT邦見習生 ‧ 2019-12-27 11:46:36

應該不會是作業吧....

eit iT邦新手 5 級 ‧ 2019-12-31 19:54:41 檢舉

不是
只有是剛好看到然後看了很久還是看不懂

我要發表回答

立即登入回答