17-5 公倍數

CHAWTeam


目錄:DICE C語言程式破解


題目

請撰寫一程式,輸入三個整數(小於1000),找出小於等於1000的所有公倍數。

輸入範例: 
30 45 18

輸出範例: 
90 180 270 360 450 540 630 720 810 900 990

程式碼

#include <stdio.h> int main() { int a, b, c, ans = 1; scanf("%d %d %d", &a, &b, &c); if (a == 1 || b == 1 || c == 1) { if (a * b * c <= 1000) { ans = a * b * c; printf("%d ", ans); for (int i = 2; ans * i <= 1000; i++) { printf("%d ", ans * i); } } } else if (a >= 2 && b >= 2 && c >= 2) { //質因數分解 for (int i = 2; a >= i && b >= i && c >= i; i++) { while (a % i == 0 && b % i == 0 && c % i == 0) { a /= i; b /= i; c /= i; ans *= i; } } //進一步做質因數分解 for (int i = 2; a >= i || b >= i || c >= i; i++) { while (a % i == 0 && b % i == 0) { a /= i; b /= i; ans *= i; } while (a % i == 0 && c % i == 0) { a /= i; c /= i; ans *= i; } while (b % i == 0 && c % i == 0) { b /= i; c /= i; ans *= i; } } if (ans * a * b * c <= 1000) { ans = ans * a * b * c; printf("%d ",ans); for (int i = 2; ans * i <= 1000; i++) { printf("%d ", ans * i); } } } return 0; }

輸入

30 45 18

輸出

90 180 270 360 450 540 630 720 810 900 990

查看我們在HackMD上的所有筆記
目錄:DICE C語言程式破解


👉【幫我們一個忙!】👈

👋如果您喜歡這篇文章,請在下方按5個Like!
❤您的支持是我們最大的動力!

您只要登入帳號(Facebook、Google),在下方按5個Like,我們就會收到來自LikeCoin基金會的贊助。
您只需要支持我們,完全不會花到錢!