C语言代码
#include
int main(){
int i=100,j=0;
int count=0;
int num=0;
for (; i < 1000; i++)
{
count = i;
for(;j<3;j++){
num = num + (count%10)*(count%10)*(count%10);
count = (int)(count/10);
}
if (num==i)
{
printf("%d是一个水仙花数\n",num);
num=0;
count=0;
j=0;
}else{
num=0;
count=0;
j=0;
}
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
python实现
count = 0
for i in range(100,1000):
for j in range(3):
num = str(i)
count = count + eval(num[j])**3
if count == i:
print("{}是水仙花数".format(i))
count = 0
else:
count = 0