輸入2個正整數
求最大公因數(gcd)
題目就是這樣
2006-07-26 10:17:33 · 3 個解答 · 發問者 改變 1 in 電腦與網際網路 ➔ 程式設計
#include
2006-07-26 10:53:13 · answer #1 · answered by ? 6 · 0⤊ 0⤋
最佳解答有一點缺陷
第2個數字輸入0會出問題!
2006-09-27 00:04:37 · answer #2 · answered by 智凱 2 · 0⤊ 0⤋
//Power by Microsoft Visual Studio 2005
//可以使用 Dev-C++ 編譯此程式
#include
#include
#include
int main(int argc, char *argv[])
{
//=====START=====//
int fun_gcd(int num1st,int num2nd);
int gcd_recursive(int num1st,int num2nd);
int n1st,n2nd;
printf("GCD and LCM\n");
printf("Input 1st number: ");
scanf("%d",&n1st);
printf("Input 2nd number: ");
scanf("%d",&n2nd);
//while 迴圈方法
printf("while...\n");
printf("GCD: %d\n",fun_gcd(n1st,n2nd));
printf("LCM: %d\n",n1st*n2nd/fun_gcd(n1st,n2nd));
//recursive 遞迴方法
printf("recursive...\n");
printf("GCD: %d\n",gcd_recursive(n1st,n2nd));
printf("LCM: %d\n",n1st*n2nd/gcd_recursive(n1st,n2nd));
//=====END=====//
system("PAUSE");
return 0;
}//main
int fun_gcd(int num1st,int num2nd){//while
int num_3rd=EOF;
while(num_3rd!=0){
num_3rd=num1st%num2nd;
num1st=num2nd;
num2nd=num_3rd;
}
return num1st;
}
int gcd_recursive(int num1st,int num2nd){//recursive
if(num1st%num2nd==0){
return num2nd;
}else{
return gcd_recursive(num2nd,num1st%num2nd);
}
}
2006-07-27 09:13:32 補充:
可以刪除標頭檔#include
那是不小心加上去的
2006-07-27 05:11:39 · answer #3 · answered by Big_John-tw 7 · 0⤊ 0⤋