--- tags: DICE C --- 19-9 完美數 === > [name=CHAWTeam] 目錄:[DICE C語言程式破解](/@CHAWTeam/DiceC) 題目 --- ```! 一個數如果恰好等於它的因數之和(除自身以外),這個數就稱為「完美數」。 例如:6=1+2+3,1、2、3都是6的因數,所以6是完美數。 請撰寫一程式,輸入一正整數N,找出1到N之間的完美數,中間以空格隔開。 輸入範例: 100 輸出範例: 6 28 ``` 程式碼 --- ```c= #include <stdio.h> int main() { int n, perfect; scanf("%d", &n); for (int i = 1; i <= n; i++) { perfect = 0; for (int j = 1; j < i; j++) { if (i % j == 0) perfect += j; } if (perfect == i) printf("%d ", i); } return 0; } ``` 執行 --- ### 輸入 ```! 100 ``` ### 輸出 ```! 6 28 ``` --- [查看我們在HackMD上的所有筆記](/@CHAWTeam) 目錄:[DICE C語言程式破解](/@CHAWTeam/DiceC) --- {%hackmd Iiu5mOixR7yWkPHKCkabBg %} <iframe class="LikeCoin" height="235" src="https://button.like.co/in/embed/chawteam/button?referrer=https://hackmd.io/@CHAWTeam/DiceC-19-9" width="100%"></iframe> --- {%hackmd i1nMRrZcTFmTvoF897K9zg %}
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up