1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-12-16 15:44:39 +00:00
202401-programming-assignments/【实践课外】10.函数1/6-7 使用函数输出水仙花数.c

42 lines
614 B
C
Raw Normal View History

2024-11-20 03:07:17 +00:00
int get_digits(int x) {
int res = 0;
while (x) {
x /= 10;
res++;
}
return res;
}
int get_pow(int x, int n) {
int res = 1;
for (int i = 0; i < n; i++) {
res *= x;
}
return res;
}
int narcissistic(int number) {
int sum = 0;
int tmp = number;
int digits = get_digits(number);
while (tmp) {
sum += get_pow(tmp % 10, digits);
tmp /= 10;
}
return sum == number;
}
void PrintN(int m, int n) {
for (int i = m + 1; i <= n - 1; i++) {
if (narcissistic(i)) {
printf("%d\n", i);
}
}
}