本文主要是介绍c语言程序设计苏小红课后习题7.7 最大公约数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
int Gcd1(int m, int n) //穷举法求最大公约数
{int max, min;int i,t;if (m > n){max = m;min = n;}else{max = n;min = m;}for (i = min;i > 0;i--){if (min % i == 0 && max % i == 0){break;}}return i;
}int Gcd2(int m, int n) //辗转相除法求最大公约数
{int i=1;while (i != 0){i = m % n;m = n;n = i;}return m;
}int Gcd3(int m, int n) //递归法求最大公约数
{int i,t;if (m == n){i = n;}if (m < n){m = m + n;n = m - n;m = m - n;}if (m > n){for (int j = 1;;j++){m = m - n;if (m == n){i = n;break;}else if (m < 0){i = 1;break;}if (m < n){m = m + n;n = m - n;m = m - n;}}}return i;
}
int main()
{int m, n;scanf("%d%d",&m,&n);printf("%d\n", Gcd1(m, n));printf("%d\n", Gcd2(m, n));printf("%d", Gcd3(m, n));return 0;
}
这篇关于c语言程序设计苏小红课后习题7.7 最大公约数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!