用c++打印出所有的水仙花数
时间: 2024-05-27 18:14:50 浏览: 9
#include <stdio.h>
#include <math.h>
int main() {
int i, j, k, n;
printf("水仙花数有:\n");
for (n = 100; n <= 999; n++) {
i = n / 100; // 获取百位数
j = (n / 10) % 10; // 获取十位数
k = n % 10; // 获取个位数
if (n == pow(i, 3) + pow(j, 3) + pow(k, 3)) {
printf("%d ", n);
}
}
printf("\n");
return 0;
}
相关问题
c++打印所有的水仙花数
水仙花数是指一个三位数,其各位数字的立方和等于该数本身。例如,153是一个水仙花数,因为 $1^3 + 5^3 + 3^3 = 153$。
以下是使用 C++ 编写的打印所有水仙花数的程序:
```cpp
#include <iostream>
using namespace std;
int main() {
for (int i = 100; i <= 999; i++) {
int num = i;
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += digit * digit * digit;
num /= 10;
}
if (sum == i) {
cout << i << " ";
}
}
cout << endl;
return 0;
}
```
这个程序使用了一个 `for` 循环来遍历所有的三位数,对于每个数,它使用一个 `while` 循环来计算该数的各位数字的立方和。如果这个和等于原数,则将这个数打印出来。
用Microsoft visual studio编写程序,打印出所有的三位数水仙花数
### 回答1:
#include <iostream>
using namespace std;
int main()
{
for(int i=100; i<1000; i++) // 从100开始遍历至999
{
int a = i / 100; // 十位
int b = i / 10 % 10; // 百位
int c = i % 10; // 个位
if(i == (a*a*a + b*b*b + c*c*c)) // 判断是否为水仙花数
{
cout << i << endl; // 输出水仙花数
}
}
return 0;
}
### 回答2:
水仙花数是指一个三位数,它的每个数字的立方和等于该数本身。例如,153是一个水仙花数,因为1³ + 5³ + 3³ = 153。以下是使用Microsoft Visual Studio编写程序打印所有的三位数水仙花数的示例代码:
```C++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
for (int i = 100; i < 1000; i++) {
int hundreds = i / 100;
int tens = (i / 10) % 10;
int units = i % 10;
int sumOfCubes = pow(hundreds, 3) + pow(tens, 3) + pow(units, 3);
if (sumOfCubes == i) {
cout << i << "是一个水仙花数" << endl;
}
}
return 0;
}
```
这段代码使用了一个for循环,从100到999循环遍历所有的三位数。在循环体内部,我们首先将三位数i分解成百位数、十位数和个位数,并使用pow函数计算每个数字的立方。然后,我们将这些立方和加起来,并与i进行比较。如果它们相等,那么i就是一个水仙花数,我们就把这个数打印出来。
执行这段代码,会在控制台输出所有的三位数水仙花数,例如:
153是一个水仙花数
370是一个水仙花数
371是一个水仙花数
407是一个水仙花数
以此类推,直到999结束。
### 回答3:
水仙花数是指一个三位数,其各个位上的数字的立方和等于该数本身。下面是用Microsoft Visual Studio编写程序来打印出所有的三位数水仙花数的简单代码:
```c++
#include <iostream>
#include <cmath>
int main() {
// 遍历所有的三位数
for (int i = 100; i < 1000; i++) {
// 分离每一位数字
int hundreds = i / 100; // 百位数
int tens = (i % 100) / 10; // 十位数
int ones = i % 10; // 个位数
// 计算立方和
int sumOfCubes = pow(hundreds, 3) + pow(tens, 3) + pow(ones, 3);
// 判断是否为水仙花数并打印结果
if (sumOfCubes == i) {
std::cout << i << " 是水仙花数" << std::endl;
}
}
return 0;
}
```
上述程序首先从100遍历到999所有的三位数。然后,使用整除和取余操作来分离每一个数字的百位、十位和个位。接下来,使用pow函数计算每个位上数字的立方,并将其累加到sumOfCubes变量中。最后,通过比较sumOfCubes和当前遍历到的三位数i是否相等来确定是否为水仙花数,并打印出结果。
运行此程序,将会输出所有的三位数水仙花数,即153、370、371、407。
相关推荐
![cbp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)