题解 | #水仙花数#
水仙花数
https://www.nowcoder.com/practice/dc943274e8254a9eb074298fb2084703
#include <stdio.h>
#include <math.h>
//这个程序运用了int型变量在除法计算时特性,比如 int a = 10, int b = 3, 则 a / b = 3
int main()
{
int a, b, count = 0;
while (scanf("%d %d", &a, &b) != EOF) {
for (int i = a; i <= b; i++) {
int first = i / 100;//百位数
int second = (i - first * 100) / 10;//十位数
int thrid = (i - first * 100) % 10;//个位数
if (i == pow(first, 3) + pow(second, 3) + pow(thrid, 3)) {
printf("%d ", i);
}
else {
count++;
}
}
if(count == b - a + 1) {
printf("no");
}
}
return 0;
}
查看11道真题和解析