hdu2010 水仙花数【C++】

水仙花数

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 234155    Accepted Submission(s): 66514


Problem Description
春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。
现在要求输出所有在m和n范围内的水仙花数。
 
Input
输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。
 
Output
对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。
 
Sample Input
100 120 300 380
 
Sample Output
no 370 371
不用交换 m和n的次序也能AC
 1 #include<cstdio>
 2 using namespace std;
 3 int main()
 4 {
 5     int m,n;
 6 
 7     while(scanf("%d %d",&m,&n)!=EOF)
 8     {
 9         int result[1000];
10         int count = 0;
11         if(m>n)
12         {
13             m = m^n;
14             n = m^n;
15             m = m^n;
16         }
17         for(int i = m;i<=n;++i)
18         {
19             int a = i/100;
20             int b = i%100/10;
21             int c = i%10;
22             if(a*a*a+b*b*b+c*c*c==i)
23             {
24                result[count++] = i;
25             }
26 
27         }
28         if(count == 0)
29         {
30             printf("no\n");
31         }
32         else
33         {
34             for(int i = 0;i<count;++i)
35             {
36                 if(i==count-1)
37                 {
38                     printf("%d\n",result[i]);
39                 }
40                 else
41                 {
42                     printf("%d ",result[i]);
43                 }
44             }
45         }
46 
47     }
48     return 0;
49 }

猜你喜欢

转载自www.cnblogs.com/knmxx/p/9339941.html