#13687: 比較普通的寫法 歡迎參考


alexrobotvm0@gmail.com (黃子軒)

學校 : 國立內壢高級中學
編號 : 70522
來源 : [42.72.2.156]
最後登入時間 :
2021-10-23 21:15:50
d693. 最小公倍數 | From: [220.135.132.11] | 發表日期 : 2018-04-07 14:57

以下為程式碼

 

#include <stdio.h>


int two_gcd(int first, int last) {
if (first > last) {
while (true) {
first %= last;
if (first == 0)
return last;

last %= first;
if (last == 0)
return first;
}
} else {
while (true) {
last %= first;
if (last == 0)
return first;

first %= last;
if (first == 0)
return last;
}
}
}


void compute_gcd(int num, int arr[]) {
long long gcd = 0;
long long lcm = arr[0];

for (int i = 1; i < num; i++) {
gcd = two_gcd(lcm, arr[i]);
lcm = (lcm * arr[i]) / gcd;
}

printf("%lld\n", lcm);
}


int main() {
int num = 0;
int arr[11] = {0};

while (scanf("%d", &num)) {
if (num == 0)
break;
for (int i = 0; i < num; i++) {
scanf("%d", &arr[i]);
}

compute_gcd(num, arr);
}

return 0;
}

 

 
ZeroJudge Forum