#include <iostream>
using namespace std;
int gcf(int a, int b);
int main() {
int a = 0, b = 0;
while (1) {
cin >> a;
if (a == 0)
break;
cin >> b;
cout << "GCF = " gcf(a, b) << endl;
}
return 0;
}
int gcf(int a, int b) {
if (a % b == 0)
return b;
else
return gcf(b, a % b);
}
所以,我的问题是:我怎样才能使程序显示最大公因子的所有步骤?例如,如果a
为300且b
为500,则程序应显示如下步骤:
GCF(300500)=&GT;
GCF(500300)=&GT;
GCF(300200)=&GT;
GCF(200100)=&GT;
GCF = 100
答案 0 :(得分:-2)
#include <iostream>
using namespace std;
int gcf(int a, int b);
int main() {
int a = 0, b = 0;
while (1) {
cin >> a;
if (a == 0)
break;
cin >> b;
cout<<"GCF("<<a<<","<<b<<")=>";
cout<<gcf(a,b);
}
return 0;
}
int gcf(int a, int b) {
if (a % b == 0)
return b;
else
return gcf(b, a % b);
}