1015 水仙花数(水题)

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
收藏
关注
取消关注
水仙花数是指一个 n 位数 ( n >= 3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)
给出一个整数M,求 >= M的最小的水仙花数。
Input
一个整数M(10 <= M <= 1000)
Output
输出>= M的最小的水仙花数
Input示例
99
Output示例
153

从m开始枚举,直到遇到第一个水仙花数。
 1 #include <iostream>
 2 using namespace std;
 3 int pow(int x,int n)
 4 {
 5     int ans=1;
 6     for(int i=1;i<=n;i++)
 7         ans*=x;
 8     return ans;
 9 }
10 bool is_shui(int n)
11 {
12     int tmp=n;
13     int len=0;
14     int ans=0;
15     while(tmp)
16     {
17         tmp/=10;
18         len++;
19     }
20     tmp=n;
21     while(n)
22     {
23         int t=n%10;
24         n/=10;
25         ans+=pow(t,len);
26     }
27     if(ans==tmp)
28         return true;
29     return false;
30 }
31 int main()
32 {
33     int m;
34     cin>>m;
35     while(!is_shui(m))
36         m++;
37     cout<<m<<endl;
38     return 0;
39 }
View Code
 
原文地址:https://www.cnblogs.com/onlyli/p/7252496.html