题解 | #水仙花数#
水仙花数
https://www.nowcoder.com/practice/dc943274e8254a9eb074298fb2084703
//整数m和n(100 ≤ m ≤ n ≤ 999)间是否存在水仙花数 //(i%10):i的个位的数 //(i/10%10):i的十位的数 //(i/100%10):i的百位的数 //这题i最大999,i的百位的数可=(i/100) #include <stdio.h> #include <math.h> int main() { int m = 0, n = 0; while(scanf("%d %d", &m, &n) != EOF) { int x = 0;//标记是否存在水仙花数 int i = 0; for(i=m; i<=n; i++) { // int a = (i%10)*(i%10)*(i%10)+ // (i/10%10)*(i/10%10)*(i/10%10)+ // (i/100)*(i/100)*(i/100); //可用幂函数 int a = pow(i%10,3)+pow(i/10%10,3)+pow(i/100,3); if(a == i) { printf("%d ", i); x = 1; } } if(x == 0)//m-n间不存在水仙花数 { printf("no"); } printf("\n");//换行 } return 0; }