1.问题描述
输出所有的“水仙花数”,所谓的“水仙花数”是指一个三位数及其各位数字的立方和等于该数本身,例如,153是“水仙花数”,因为。
2.源码实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int u;
int a, b, c;
int i;
for(i=100; i<1000; i++)
{
u = i;
a = u % 10;
b = (u / 10) % 10;
c = (u / 100) % 10;
if(a*a*a + b*b*b + c*c*c == u)
{
printf("%d\n", u);
}
}
return 0;
}
3.编译源码
$ gcc -o test test.c -std=c89
4.运行及其结果
./test
153
370
371
407
网友评论