1. 程式人生 > >HDU 2010 水仙花數

HDU 2010 水仙花數

Problem Description 春天是鮮花的季節,水仙花就是其中最迷人的代表,數學上有個水仙花數,他是這樣定義的:
“水仙花數”是指一個三位數,它的各位數字的立方和等於其本身,比如:153=1^3+5^3+3^3。
現在要求輸出所有在m和n範圍內的水仙花數。

Input 輸入資料有多組,每組佔一行,包括兩個整數m和n(100<=m<=n<=999)。
Output 對於每個測試例項,要求輸出所有在給定範圍內的水仙花數,就是說,輸出的水仙花數必須大於等於m,並且小於等於n,如果有多個,則要求從小到大排列在一行內輸出,之間用一個空格隔開;
如果給定的範圍內不存在水仙花數,則輸出no;
每個測試例項的輸出佔一行。
Sample Input 100 120 300 380
Sample Output no 370 371
#include <iostream>
using namespace std;
int main()
{
    int m,n;
    while (cin>>m>>n)
    {
        int x=0;
        int p[1000];
        for(int i=m; i<=n; i++)
        {
            int a=0,b=0,c=0,d;
            a=i/100;
            b=(i%100)/10;
            c=i%10;
            a=a*a*a;
            b=b*b*b;
            c=c*c*c;
            d=a+b+c;
            if(i==d)
            {
                p[x]=i;
                x++;
            }
        }
        if(!x)   cout<<"no"<<endl;
        else
        {
            for(int t=0; t<x; t++)
            {
                cout<<p[t];
                if(t<x-1) cout<<" ";
            }
            cout<<endl;
        }
    }
    return 0;
}