檢查一個3位數是水仙花數輸入:一個數字,比如 371輸出:是的,這個數字是一個水仙花數,如果不是則輸出 這個數字不是水仙花數。
2 回答

慕絲7291255
TA貢獻1859條經驗 獲得超6個贊
#include <iostream> using namespace std; int main() { int a, b, c, y, n = 0; cout << "請輸入三位數字:" << endl; cin >> n; a = n % 1000 / 100; //求第一位數 b = n % 100 / 10; //求第二位數 c = n % 10 / 1; //求第三位數 y = a*a*a + b*b*b + c*c*c; if (y == n) cout << n << "是水仙花數" << endl; else cout << n << "不是水仙花數" << endl; system("pause"); return 0; }

慕無忌1623718
TA貢獻1744條經驗 獲得超4個贊
#include
using namespace std;
int main()
{
int n;
cin >> n;
int sum = 0, m = n;
while (m != 0)
{
int x = m % 10; //截取n的個位
m /= 10; //去除n的個位
sum += x * x * x;
}
if (sum == n)
cout << "Yes\n";
else
cout << "No\n";
system("pause");
return 0;
}
- 2 回答
- 0 關注
- 1125 瀏覽
添加回答
舉報
0/150
提交
取消