Zi 字媒體
2017-07-25T20:27:27+00:00
[C/C++ 演算法]- 最大公因數、最小公倍數、因數分解
剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:http://openhome.cc/Gossip/AlgorithmGossip/http://openhome.cc/Gossip/AlgorithmGossip/GCDPNumber.htm
#include
#include
int gcd(int m, int n) {
while(n != 0) {
int r = m % n;
m = n;
n = r;
}
return m;
}
int lcm(int m, int n) {
return m * n / gcd(m, n);
}
int main(void) {
int m, n;
printf("輸入兩數:");
scanf("%d %d", &m, &n);
printf("Gcd:%d\n", gcd(m, n));
printf("Lcm:%d\n", lcm(m, n));
return 0;
}
寫了
5860316篇文章,獲得
23313次喜歡