题解 | #水仙花数#
水仙花数
https://www.nowcoder.com/practice/dc943274e8254a9eb074298fb2084703
#include <stdio.h> int pow(int n) { return n * n * n; } void func(int n, int* sum) { if (n > 10) func(n / 10, sum); *sum += pow(n % 10); } //递归处理各立方相加 int main() { int m, n, temp = 0, flag = 0; int sum = 0; scanf("%d%d", &m, &n); for (int i = m; i <= n; i++) { temp = i; func(i, &sum); if (sum == temp) { printf("%d ", sum); flag = 1; sum = 0; } sum = 0; } if (flag != 1) printf("no"); }
}int main() {int m, n, temp = 0, flag = 0;int sum = 0;scanf("%d%d", &m, &n);for (int i = m; i <= n; i++) {temp = i;func(i, &sum);if (sum == temp) {printf("%d ", sum);flag = 1;sum = 0;}sum = 0;}if (flag != 1)printf("no");}