51nod 1015 水仙花数

水仙花数是指一个 n 位数 ( n >= 3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)
给出一个整数M,求 >= M的最小的水仙花数。
 
Input
一个整数M(10 <= M <= 1000)
Output
输出>= M的最小的水仙花数
Input示例
99
Output示例
153

枚举
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

int m,i,j;
int powe(int a,int b)
{
    int base=a,r=1;
    while(b)
    {
        if(b&1)
        r*=base;
        base*=base;
        b>>=1;
    }
    return r;
}
int main()
{
    int t;
    cin>>m;
    for(i=m;i<=10000000;++i)
    {
        if(i>=100&&i<=999) t=3;
        else if(i>=1000&&i<=9999) t=4;
        else if(i>=10000&&i<=99999) t=5;
        else if(i>=100000&&i<=999999) t=6;
        else if(i>=1000000&&i<=9999999) t=7;
        int sum=0;
        int j=i;
        while(j)
        {
            sum+=powe(j%10,t);
            j/=10;
        }
        if(sum==i)
        {
            cout<<i<<endl;
            return 0;
        }
    }
}

 

posted @ 2017-02-08 15:55  杀猪状元  阅读(177)  评论(0编辑  收藏  举报