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:
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; }
相关文章推荐
- ZOJ Problem Set - 3543 Number String DP
- ZOJ Problem Set - 2736 Daffodil number
- ZOJ 2736 Daffodil number
- ZOJ Problem Set - 3816 Generalized Palindromic Number 搜索+贪心
- zoj 2736 Daffodil number
- ZOJ Problem Set - 1093 Monkey and Banana
- ZOJ Problem Set - 1463 Brackets Sequence
- ZOJ Problem Set - 3418 Binary Number
- ZOJ Problem Set - 3640 Help Me Escape
- ZOJ Problem Set - 3829Known Notation(贪心)
- ZOJ Problem Set–1383 Binary Numbers
- ZOJ Problem Set–1489 2^x mod n = 1
- ZOJ Problem Set - 3768 Continuous Login
- ZOJ Problem Set–1514 Fake Tickets
- ZOJ Problem Set - 3329 One Person Game
- ZOJ Problem Set - 1953 Advanced Fruits
- ZOJ Problem Set - 2969 Easy Task
- ZOJ Problem Set - 3326 An Awful Problem(模拟题,待改正)
- ZOJ Problem Set - 3603||Draw Something Cheat
- ZOJ Problem Set - 1008 Gnome Tetravex