来源:《信息学奥赛一本通(C++版)》P64
【题目描述】
4.9 对于两个正整数 m,n 的最大公因数可以用do—while实现。
代码如下,请完善:
#include<iostream>
using namespace std;
int main()
{
int m,n,r;
cin>>m>>n;
do //碾转相除法
{
r=m%n;
m=____;
n=____;
}
while(____);
cout<<"the greatest common divisor:"<<_____;
return 0;
}
注:本样例为作者所加,不是书本上的内容
【输入样例】
12 3
【输出样例】
the greatest common divisor:3
本题解法如下:
注:代码仅供参考,不只有一种解法。
//这里是补全后的代码
#include<iostream>
using namespace std;
int main(){
int m,n,r;
cin>>m>>n;
do //碾转相除法
{
r=m%n;
m=n;
n=r;
}
while(r!=0);
cout<<"the greatest common divisor:"<<m<<endl;
//endl是换行,也可以不加
system("pause");//防止闪退,可以不加
return 0;
}