先找出小於1000000的阿姆斯壯數

迴圈

輸入上限下限

找出符合下限的值

輸出直到大於上限 或 沒東西輸出

若都沒輸出

輸出none

 

#include <iostream>

#define N 1000000

using namespace std;

 

 

int armstrong[20];
int armNum = 0;
void setArmstrong()
{
    int powTable[8][6] ={ {2, 4, 8, 16, 32, 64},
                                       {3, 9, 27, 81, 243, 729},
                                       {4, 16, 64, 256, 1024, 4096},
                                       {5, 25, 125, 625, 3125, 15625},
                                      {6, 36, 216, 1296, 7776, 46656},
                                      {7, 49, 343, 2401, 16807, 117649},
                                      {8, 64, 512, 4096, 32768, 262144},
                                      {9, 81, 729, 6561, 59049, 531441} };

    for(int i = 1, total, digit; i<N; i++)
    {
        total = digit = 0;
        for(int j = i; j>0; j/=10)
            digit++;

        for(int j = i; j>0; j/=10)
        {
            if(j%10 == 1)
                total++;
            else if(j%10)
                total += powTable[j%10 - 2][digit-1];
        }

        if(total == i)
            armstrong[armNum++] = total;
    }
}

int main()
{
    setArmstrong();

    int lower, upper;
    int i;
    bool found;

    while(cin >> lower >> upper)
    {
        i = 0;
        while(armstrong[i] < lower)i++;

        found = false;
        for(; armstrong[i] <= upper && i < armNum; i++)
        {
            found = true;
            cout << armstrong[i] << " ";
        }

        if(!found)
            cout << "none";
        cout << endl;
    }

    return 0;
}
 

 
arrow
arrow
    全站熱搜

    大神(偽) 發表在 痞客邦 留言(0) 人氣()