|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- //请完成求最大公约数算法,补全函数里的代码段
- #include <iostream>
- #include <string.h>
- int ComFactor(int m, int n)
- {
- int r = m % n;
- if (r != 0){
- n--;
- return ComFactor(m,n)};
- return n;
- }
- int main( )
- {
- int m,n;
- cin>>m>>n;
- cout << ComFactor(m, n) << endl;
- return 0;
- }
复制代码
学校作业
if 语句是自己写的,其他都是原题。
不知道哪里错了
帮你修改了。不应该用if,应该用while循环
- //请完成求最大公约数算法,补全函数里的代码段
- #include <iostream>
- #include <string.h>
- using namespace std;
- int ComFactor(int m, int n)
- {
- int r = m % n;
- while (r != 0){
- m=n;
- n=r;
- r=m%n;
- }
- return n;
- };
- int main( )
- {
- int m,n;
- cin>>m>>n;
- cout << ComFactor(m, n) << endl;
- return 0;
- }
复制代码
|
|