水仙花数求解(1-2000)
# include <stdio.h>
void getBit(int num,int ar[],int *length){
while (num > 0) {
ar[*length] = num % 10;
*length += 1;
num /= 10;
}
}
int pow(int x, int y) {
int mul = 1;
for (int i = 0; i < y; i++) {
mul *= x;
}
return mul;
}
void isNar(int num,int ar[],int length){
int sum = 0;
for (int i = 0;i<length; i++) {
sum += pow(ar[i], length);
}
if (sum==num)
{
printf("%d\n", num);
}
}
int main() {
for (int num =1;num<=20000;num++){
int ar[10] = { 0 }, length = 0;
getBit(num, ar, &length);
isNar(num, ar, length);
}
return 0;
}
运行如下