水仙花数(100-999)

#include<stdio.h>
int main()
{
FILE *fout;
fout=fopen(“test2-2.out”,”wb”);
int i=100;
for(i=100;i<=999;i++)
{
int a,b,c;//分割三位数
a=i/100;
b=i/10%10;
c=i%10;
if(i==a*a*a+b*b*b+c*c*c)
{
fprintf(fout,”%d\n”,i);
}
}
fclose(fout);
return 0;

}

说明:如果需要显示在标准输出可以参考下面

#include<stdio.h>
int main()
{
FILE *fout;
//fout=fopen(“test2-2.out”,”wb”);
fout=stdout;

int i=100;
for(i=100;i<=999;i++)
{
int a,b,c;//分割三位数
a=i/100;
b=i/10%10;
c=i%10;
if(i==a*a*a+b*b*b+c*c*c)
{
fprintf(fout,”%d\n”,i);
}
}
//fclose(fout);
return 0;
}

结果:
153
370
371
407



Tagged , , . Bookmark the permalink.

Comments are closed.