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

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

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