• 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 }

  • 相关阅读:
    c++获取线程id
    一个基于c++的log库
    防止socket程序重启等待2MSL时间
    c++头文件循环引用
    Myeclipse 8.5 优化设置
    来道题 求解释
    MyEclipse常用设置笔记
    Ubuntu 学习笔记
    Linux 下常用命令
    Oracle 学习笔记 常用查询命令篇
  • 原文地址:https://www.cnblogs.com/malloc/p/2096304.html
Copyright © 2020-2023  润新知