[新手入门]求出两个整数的最大公约数与最小公倍数(GCD&LCM)
[color=#0000FF]说明:假设有A,B两整数(A,B均不为 0),A * B = GCD * LCM[/color]程序代码:
#include <stdio.h> #include <stdlib.h> int main() { int n, m; int max = 0; int temp = 0; printf("请输入两个整数:"); scanf("%d %d", &n, &m); int x = n; int y = m; while( m > 0 ) { temp = m % n; n = m; m = temp; } printf("GCD = %d\n", n); if( x > y ) { max = x; } else { max = y; } while( max < x * y ) { if( max % x == 0 && max % y == 0) { break;} else { max++; } } printf("LCM = %d\n", max); system("pause"); return 0; }
我在网站上自学的C语言,刚入门,大佬手下留情啊!
[此贴子已经被作者于2022-11-8 07:56编辑过]