C語言編程題??急??
5
輸出所有的“水仙花數”,水仙花數是指一個3位數,其各位數字立方和等於該數本身。例如153是水仙花數,因為153=125+27+1
最佳回答:
#include <iostream>
using namespace std;
int cubic(int x)
{
return x*x*x;
}
int main()
{
int bit_0;
int bit_1;
int bit_2;
for (int i=100; i<1000; i++)
{
bit_0 = i/100;
bit_1 = i/10 - 10*bit_0;
bit_2 = i%10;
if (i == cubic(bit_0)+cubic(bit_1)+cubic(bit_2))
{
cout << i << endl;
}
}
return 0;
}
-
追問:
-
無法執行,
-
回答:
-
#include <stdio.h>
void main()
{
int a, b, c, i;
for (i = 100; i < 1000; i++)
{
a = i / 100;
b = i / 10 - a * 10;
c = i % 10;
if (a * a * a + b * b * b + c * c * c == i)
printf( "%d ", i);
}
}