算法一:暴力求解(效率不够)
#include <stdio.h> int main() { int a = 0; int b = 0; scanf("%d %d", &a, &b); int min = a < b ? a : b; while (1) { if ((a % min == 0) && (b % min == 0)) break; min--; } printf("%d\n", min); return 0; }
算法二优化:辗转相除法,大数对小数取余,小数再对余数取余,余数为零停,无须比较,大小自动交换
#include <stdio.h> int main() { int a = 0; int b = 0; int c = 0; scanf("%d %d", &a, &b); while (c = a % b) { a = b; b = c; } printf("%d\n", b); return 0; }
标签:练习题,return,min,int,scanf,打印,最大公约数,printf,main From: https://www.cnblogs.com/xiaowanglong/p/17901146.html