水仙花数是指一个 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
题意好懂的中文题,感觉自己写的麻烦了。。。
ac代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<cstring>
#include<string.h>
#include<queue>
using namespace std;
typedef long long ll;
int s[5000];
int qsm(int a,int b)
{
int t=1;
while(b)
{
if(b&1)
t*=a;
a*=a;
b>>=1;
}
return t;
}
int cal(int t)
{
int ans=0;
int num=t;
int n=0;
while(num)
{
num/=10;
n++;
}
num=t;
while(t)
{
ans+=qsm(t%10,n);
t/=10;
}
return ans==num;
}
int main()
{
int cnt=0;
for(int i=0;i<3000;i++)
{
if(cal(i))
{
s[cnt++]=i;
}
}
int m;
scanf("%d",&m);
printf("%d\n",*lower_bound(s,s+cnt,m));
return 0;
}
看了别人的代码发现,其实直接找出那几个数判断就好,本来数据范围就不大。
来自:https://www.cnblogs.com/qhy285571052/p/36cebc009dc194f5bc566b130086c662.html
#include<stdio.h>
using namespace std;
int main(){
int m;
scanf("%d",&m);
if(m > 407){
puts("1634");
}else if(m > 371){
puts("407");
}else if(m > 370){
puts("371");
}else if(m > 153){
puts("370");
}else puts("153");
}