51Nod-1015原题链接
原题:
水仙花数是指一个 n 位数 ( n >= 3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)
给出一个整数M,求 >= M的最小的水仙花数。
Input
一个整数M(10 <= M <= 1000)
Output
输出>= M的最小的水仙花数
Sample Input
99
Sample Output
153
题意:
水仙花数:一个 n 位数,它的每个位上的数字的 n 次幂之和等于它本身。
输入一个在10-1000的数m;找出大于它的最小水仙花数。
题解:
水题,直接暴力求即可。
附上代码:
#include <iostream>
#include <cmath>
using namespace std;
int m,res,q[5];
int wide(int a)
{
int flag=0,i=0;
while(a!=0)
{
q[i++]=a%10;
a=a/10;
++flag;
}
return flag;
}
void deal()
{
int flag=0,sum=0,l;
for(int a=m;;a++)
{
l=wide(a);
sum=pow(q[0],l)+pow(q[1],l)+pow(q[2],l)+pow(q[3],l);
if(sum==a){res=a;break;}
}
}
int main()
{
cin>>m;
deal();
cout <<res<< endl;
return 0;
}