ZOJ Problem Set - 2736 Daffodil number

Daffodil number

Time Limit: 1 Second       Memory Limit: 32768 KB

The daffodil number is one of the famous interesting numbers in the mathematical world. A daffodil number is a three-digit number whose value is equal to the sum of cubes of each digit.

For example. 153 is a daffodil as 153 = 13 + 53 + 33.

Input

There are several test cases in the input, each case contains a three-digit number.

Output

One line for each case. if the given number is a daffodil number, then output "Yes", otherwise "No".

Sample Input

153
610

Sample Output

Yes
No


Author: LIU, Yaoting
Source: Zhejiang Provincial Programming Contest 2006, Preliminary

SOURCE CODE:

1 #include<iostream>
2 #include<string>
3  using namespace std;
4
5  const string YES = "Yes";
6 const string NO = "No";
7
8 bool isDaffodil(int n)
9 {
10 if(n < 100 || n > 999)//100一下或者999以上,都不是Daffodil number
11 {
12 return false;
13 }
14 int a = n / 100; //取百位
15 int b = (n % 100) / 10;//取十位
16 int c = n % 10;//取个位
17 if( n == (a*a*a + b*b*b + c*c*c) )//相等则为Daffodil number
18 {
19 return true;
20 }
21 return false;
22 }
23
24 int main()
25 {
26 int daffodil[1000];//
27 for(int i = 0;i < 1000;i++)//初始化一个1000为长的整形数组,保存每一位是否为Daffodil number的信息,若是则相应位置上保存为1,若不是则保存为0,若还未判定则保存为-1
28 {
29 daffodil[i] = -1;
30 }
31 int num;
32 while(cin>>num)
33 {
34 if(num < 100 || num > 999)//仅3位数有效
35 {
36 cout<<NO;
37 }
38 switch(daffodil[num])
39 {
40 case -1://该数未判定
41 if( isDaffodil(num) )
42 {
43 daffodil[num] = 1;
44 cout<<YES;
45 }
46 else
47 {
48 daffodil[num] = 0;
49 cout<<NO;
50 }
51 break;
52 case 0://不是Daffodil number
53 cout<<NO;
54 break;
55 case 1://是Daffodil number
56 cout<<YES;
57 break;
58 default:
59 break;
60 }
61 cout<<endl;
62 }
63 return 0;
64 }

转载于:https://www.cnblogs.com/malloc/archive/2011/07/02/2096304.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值