您的位置:首页 > 其它

ZOJ Problem Set - 2736 Daffodil number

2011-07-02 15:44 218 查看
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:



#include<iostream>
#include<string>
using namespace std;

const string YES = "Yes";
const string NO = "No";

bool isDaffodil(int n)
{
if(n < 100 || n > 999)//100一下或者999以上,都不是Daffodil number
{
return false;
}
int a = n / 100; //取百位
int b = (n % 100) / 10;//取十位
int c = n % 10;//取个位
if( n == (a*a*a + b*b*b + c*c*c) )//相等则为Daffodil number
{
return true;
}
return false;
}

int main()
{
int daffodil[1000];//
for(int i = 0;i < 1000;i++)//初始化一个1000为长的整形数组,保存每一位是否为Daffodil number的信息,若是则相应位置上保存为1,若不是则保存为0,若还未判定则保存为-1
{
daffodil[i] = -1;
}
int num;
while(cin>>num)
{
if(num < 100 || num > 999)//仅3位数有效
{
cout<<NO;
}
switch(daffodil[num])
{
case -1://该数未判定
if( isDaffodil(num) )
{
daffodil[num] = 1;
cout<<YES;
}
else
{
daffodil[num] = 0;
cout<<NO;
}
break;
case 0://不是Daffodil number
cout<<NO;
break;
case 1://是Daffodil number
cout<<YES;
break;
default:
break;
}
cout<<endl;
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: