优化一下问题:i(resd ==0) break:这一条跳出语句和while(resd!=0);循环中的结束条件重复了,分别修改这两个不同的语句,实现只需要一个跳出条件即可。
基于上述优化代码实现:三个整数的最大公约数(只能基于例子中的代码进行实现)。
采用input.txt文件输入三个整数的值,并将公约数结果输出到output.txt文件中。
基于new bing的编写参考:
#include <iostream>
#include <fstream>
using namespace std;
int gcd(int a, int b) {
while (b != 0) {
int temp = a % b;
a = b;
b = temp;
}
return a;
}
int main() {
int a, b, c;
ifstream fin("input.txt");
fin >> a >> b >> c;
fin.close();
int res = gcd(gcd(a, b), c);
ofstream fout("output.txt");
fout << res;
fout.close();
return 0;
}