2017年10月6日 星期五

[C_MM32-易] Armstrong數(C++)

[C_MM32-易] Armstrong數

問題描述:所謂 " Armstrong數 " 是指一個三位數的整數,其各位數字之立方和等於該數本身。例如: 153 是一個  Armstrong數,因為 153 =1 + 53 + 33 。試撰寫一程式,判斷是否為  Armstrong數。
輸入說明輸入一個三位數正整數。
輸出說明:是 阿姆斯壯數輸出 Yes ,不是 阿姆斯壯數輸出 No 的訊息。

去掉題目包裝,就只是問計算而已。

  1. #include <iostream>  
  2. #include <cmath>  
  3. using namespace std;  
  4.   
  5. int main() {  
  6.     int num;  //使用者要測試的數字
  7.     double Armstrong, a, b, c;  
  8.     while(cin >> num)  
  9.     {  
  10.         a = floor(num / 100);  //百位
  11.         b = num / 10 % 10;     //十位
  12.         c = num % 10;          //個位
  13.         Armstrong = pow(a,3)+pow(b,3)+pow(c,3);  //各個位數數字之立方和
  14.         if(num == Armstrong)  
  15.         {  
  16.             cout << "Yes" << endl;  
  17.         }  
  18.         else  
  19.         {  
  20.             cout << "No" << endl;  
  21.         }  
  22.     }  
  23.     return 0;  
  24. }  


1 則留言: