请用C语言编写一个完整的.求水仙花数的程序

2024-12-13 00:29:43
推荐回答(2个)
回答1:

#include
#include
main ()
{
double i=10,j,k,n,m,l;
printf("intpu X");//输入界限就是多少以内的水仙花数
scanf(%lf,&k);
for (;i{
m=0;//判断器清零
j=int(i);//记录器赋值
for(n=0;j<1;n++)j/=10;//计算位数
j=int(i);//重新记录I的值
l=n;//记录位数
do
{
s+=(int(j%10))^l;n--;j=int(j/10);//得到n位数个位的n次方并放入累加器
}
while(n>0);//得到n位数,各位上数的n次方加和,放入判断器
if(s==int(i))printf("%.0lf是水仙花数",i);//若判断器与原数相等则输出
}
//从理论来讲,这个实现起来比较复杂,理论最大的水仙花数有33位...
下面列出24位以内所有水仙花数
153,370,371,407
1634,8208,9474
5: 93084   5: 92727   5: 54748   6: 548834   7: 9800817   7: 4210818   7: 1741725   7: 9926315   8: 24678050   8: 24678051   8: 88593477   9: 146511208   9: 912985153   9: 472335975   9: 534494836   10: 4679307774   11: 32164049650   11: 40028394225   11: 42678290603   11: 49388550606   11: 32164049651   11: 94204591914   11: 44708635679   11: 82693916578   14: 28116440335967   16: 4338281769391370   16: 4338281769391371   17: 35875699062250035   17: 21897142587612075   19: 3289582984443187032   19: 4929273885928088826   19: 4498128791164624869   20: 63105425988599693916   21: 449177399146038697307   21: 128468643043731391252   23: 27907865009977052567814   23: 35452590104031691935943   23: 27879694893054074471405   23: 21887696841122916288858   24: 174088005938065293023722   24: 188451485447897896036875

回答2:

#include
int main(int argc, char *argv[])
{ int n1,n2,n3,i;
for(i=100;i<1000;i++)
{
n1=i%10; //求个位
n2=i/10%10; //求十位
n3=i/100; //求百位
if(n1*n1*n1+n2*n2*n2+n3*n3*n3==i) //判断条件
printf("%-6d",i);
}
printf("\n");
return 0;
}