輸出所有水仙花數

Description

打印出所有"水仙花數",所謂"水仙花數"是指一個三位數,其各位數字立方和等於該本身。例如:153是一個水仙花數,因爲153=1^3+5^3+3^3。 Output:

153
370
371
407

Input

Output

所有的水仙花數,從小的開始。每行一個

Sample Input

Sample Output

HINT


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
    int n,a,b,c;
    for(n=100;n<1000;n++)
    {
        a=n/100; //分別獲取百位、十位、個位,進行檢驗
        b=n/10-a*10;
        c=n%10;
        if(n==a*a*a+b*b*b+c*c*c) cout<<n<<endl;
        else continue;
    }
    return 0;
}
/**************************************************************
    Problem: 1036
    User: 201358501133
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1264 kb
****************************************************************/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章