使用C++语言输入20个有符号整数,统计正整数、零、负整数的个数
时间: 2024-01-31 12:02:50 浏览: 31
```c
#include <stdio.h>
int main() {
int num, positive = 0, zero = 0, negative = 0;
printf("请输入20个有符号整数:\n");
for (int i = 0; i < 20; i++) {
scanf("%d", &num);
if (num > 0) {
positive++;
} else if (num == 0) {
zero++;
} else {
negative++;
}
}
printf("正整数:%d,零:%d,负整数:%d\n", positive, zero, negative);
return 0;
}
```
相关问题
c++,连续输入n个整数,统计其中正数、负数和零的个数
好的,我可以为您解答这个问题。您可以使用一个循环语句,输入n个整数,并逐个判断它们是正数、负数还是零。在每次输入后,您可以使用if语句来判断是正数、负数还是零,并对应地累加计数器的值。最后,输出三个计数器的值即可,例如:
```
#include <stdio.h>
int main() {
int n, num, positive = 0, negative = 0, zero = 0;
printf("请输入整数个数n:");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("请输入第%d个整数:", i+1);
scanf("%d", &num);
if (num > 0) {
positive++;
} else if (num < 0) {
negative++;
} else {
zero++;
}
}
printf("正数个数:%d\n负数个数:%d\n零的个数:%d\n", positive, negative, zero);
return 0;
}
```
这段代码可以统计连续输入的n个整数中,正数、负数和零的个数,并输出结果。如果您有任何疑问,可以继续问我。
c++语言输入两个正整数求最大公约数和最小公倍数
```c
#include <stdio.h>
// 求最大公约数
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 main() {
int a, b;
printf("请输入两个正整数:\n");
scanf("%d%d", &a, &b);
printf("它们的最大公约数为:%d\n", gcd(a, b));
printf("它们的最小公倍数为:%d\n", lcm(a, b));
return 0;
}
```
相关推荐
![](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)