输入水仙花数
水仙花数是指一个N位正整数(N≥3),它的每个位上的数字的N次幂之和等于它本身。例如:。 本题要求编写程序,计算所有N位水仙花数。输入
输入在一行中给出一个正整数N(3≤N≤7)。
程序代码:#include <stdio.h>
int main( void )
{
unsigned map[11][9];
for( unsigned n=0; n!=11; ++n )
{
map[n][0] = 1;
for( unsigned m=1; m!=9; ++m )
map[n][m] = map[n][m-1] * n;
}
for( unsigned n=100,m=3; n!=100000000; ++n )
{
if( n >= map[10][m] )
++m;
unsigned sum = 0;
for( unsigned t=n; t; t/=10 )
sum += map[t%10][m];
if( sum == n )
printf( "%u\n", n );
}
return 0;
}
输出
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
88593477
程序代码:#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int x,a,b,c;
x=0,a=0,b=0,c=0;
for(x=100;x<=9999999;x++){
a=x/100;
b=(x/10)%10;
c=x%10;
if(x==a*a*a+b*b*b+c*c*c){
printf("%d\n",x);
}
}
return 0;
}
程序代码:#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int x,a,b,c;
x=0,a=0,b=0,c=0;
for(x=100;x<=9999999;x++){
a=x/100;
b=(x/10)%10;
c=x%10;
if(x==a*a*a+b*b*b+c*c*c){
printf("%d\n",x);
}
}
return 0;
}