PTA水仙花数
程序员文章站
2022-07-15 08:53:53
...
#include <stdio.h>
int narcissistic( int number );
void PrintN( int m, int n );
int main()
{
int m, n;
scanf("%d %d", &m, &n);
if ( narcissistic(m) ) printf("%d is a narcissistic number\n", m);
PrintN(m, n);
if ( narcissistic(n) ) printf("%d is a narcissistic number\n", n);
return 0;
}
/* 你的代码将被嵌在这里 */
#include <math.h>
int narcissistic( int number ){
if(number>=10000){ //大于1万
int hun,ten,inv,tho,tth;
tth=number/10000;
tho=number%10000/1000;
hun=number%1000/100;
ten=number%100/10;
inv=number%10;
if( pow(tth,5) + pow(tho,5) + pow(hun,5) + pow(ten,5) + pow(inv,5) == number){
return 1;
}else{
return 0;
}}
if(number>=1000){ //大于一千
int hun,ten,tho,tth;
tth=number/1000;
tho=number%1000/100;
hun=number%100/10;
ten=number%10;
if( pow(tth,4) + pow(tho,4) + pow(hun,4) + pow(ten,4) == number){
return 1;
}else{
return 0;
}}
if(number>=100){ //大于一百
int hun,ten,inv;
hun=number/100;
ten=number/10%10;
inv=number%10;
if( pow(hun,3) + pow(ten,3) + pow(inv,3) == number){
return 1;
}else{
return 0;
}
}
}
void PrintN( int m, int n ){
for(int i=m+1;i<n;i++){
if(narcissistic(i))
printf("%d\n",i);
}
}