C++ 5个数中3的倍数
时间: 2024-03-07 19:47:23 浏览: 49
您好!对于您的问题,我可以提供以下 C++ 代码示例来找出 5 个数中的 3 的倍数:
```c++
#include <iostream>
using namespace std;
int main() {
int num[5];
int count = 0;
cout << "请输入5个整数:" << endl;
for(int i = 0; i < 5; i++) {
cin >> num[i];
if(num[i] % 3 == 0) {
count++;
}
}
cout << "这5个数中有 " << count << " 个数是3的倍数。" << endl;
return 0;
}
```
这段代码通过循环读入 5 个整数,并统计其中是 3 的倍数的数的个数,最后输出结果。希望能够帮助到您!
相关问题
用c++求3个数最小公倍数代码
#include <stdio.h>
// 定义函数求两个数的最大公约数
int gcd(int a, int b) {
int temp;
while (b != 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
// 定义函数求三个数的最小公倍数
int lcm(int a, int b, int c) {
int temp = gcd(gcd(a, b), c); // 先求出三个数的最大公约数
return a * b * c / temp; // 最小公倍数等于三个数的乘积除以最大公约数
}
int main() {
int a, b, c;
printf("请输入三个正整数:");
scanf("%d %d %d", &a, &b, &c);
printf("它们的最小公倍数为:%d\n", lcm(a, b, c));
return 0;
}
c++多个数的最小公倍数
在C++中,你可以使用以下方法来计算多个数的最小公倍数:
```cpp
#include <iostream>
// 计算两个数的最大公约数
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// 计算两个数的最小公倍数
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
// 计算多个数的最小公倍数
int lcmMultiple(int numbers[], int n) {
int result = numbers[0];
for (int i = 1; i < n; i++) {
result = lcm(result, numbers[i]);
}
return result;
}
int main() {
int numbers[] = {2, 3, 4, 5};
int n = sizeof(numbers) / sizeof(numbers[0]);
int result = lcmMultiple(numbers, n);
std::cout << "最小公倍数: " << result << std::endl;
return 0;
}
```
在这个示例中,我们定义了两个函数:gcd(计算最大公约数)和lcm(计算两个数的最小公倍数)。然后,我们使用lcmMultiple函数来计算多个数的最小公倍数。在main函数中,我们定义了一个整数数组numbers,并计算其大小n。然后,我们调用lcmMultiple函数来计算最小公倍数,并将结果打印输出。
运行上述代码,将会输出结果为:最小公倍数: 60。这是因为2、3、4和5的最小公倍数是60。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)
![](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)