输出n以内所有的水仙花数;
时间: 2024-04-25 18:09:52 浏览: 11
可以的,输出n以内所有的水仙花数是:
#include <stdio.h>
#include <math.h>
int main()
{
int n;
printf("请输入一个正整数n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int temp = i;
int sum = 0;
while (temp) {
sum += pow(temp % 10, 3);
temp /= 10;
}
if (sum == i) {
printf("%d ", i);
}
}
return 0;
}
如果您还有其它问题,请随时问我哦~
相关问题
输出1000以内的水仙花数
好的,水仙花数指的是一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。因此,我们可以通过枚举 100-999 这个区间内的整数,计算每个整数是否是水仙花数,然后输出所有的水仙花数。以下是代码示例:
```python
for i in range(100,1000):
num = str(i)
total = 0
for n in num:
total += int(n)**3
if total == i:
print(i)
```
输出结果为:
```
153
370
371
407
```
用c语言输出1000以内的所有水仙花数
好的,这个问题可以回答。水仙花数是指一个三位数,其各位数字立方和等于该数本身。例如,153是一个水仙花数,因为1³+5³+3³=153。以下是C语言代码实现输出1000以内的所有水仙花数:
#include <stdio.h>
int main() {
int i, j, k, n;
for (n = 100; n < 1000; n++) {
i = n / 100;
j = (n / 10) % 10;
k = n % 10;
if (i * i * i + j * j * j + k * k * k == n) {
printf("%d ", n);
}
}
return 0;
}
输出结果:
153 370 371 407
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)