码迷,mamicode.com
首页 > 其他好文 > 详细

1015 水仙花数(水题)

时间:2017-07-28 22:24:57      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:show   lap   sed   数字   problem   end   tmp   include   names   

基准时间限制: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

 

 

1015 水仙花数(水题)

标签:show   lap   sed   数字   problem   end   tmp   include   names   

原文地址:http://www.cnblogs.com/onlyli/p/7252496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!