首页 > 试题广场 >

统计数据正负个数

[编程题]统计数据正负个数
  • 热度指数:25753 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
输入10个整数,分别统计输出正数、负数的个数。

输入描述:
输入10个整数(范围-231~231-1),用空格分隔。


输出描述:
两行,第一行正数个数,第二行负数个数,具体格式见样例。
示例1

输入

-1 2 3 -6 7 8 -1 6 8 10

输出

positive:7
negative:3
#include<stdio.h>
int main()
{
    int ch[10],p=0,n=0,i;
    for(i=0;i<10;i++)
    {scanf("%d",&ch[i]);
     if(ch[i]<0)
         n++;
     if(ch[i]>0)
         p++;
    }
    printf("positive:%d\nnegative:%d\n",p,n);
}
发表于 2020-06-08 13:08:32 回复(0)
#include<stdio.h>
int main(){
    int i,a[10],p=0,n=0;
    for(i=0;i<10;i++){
        scanf("%d",&a[i]);
        //getchar();
        if(a[i]>0) p++;
        if(a[i]<0) n++;
    }
    printf("positive:%d \n",p);
    printf("negative:%d",n);
    return 0;
}
发表于 2022-06-12 09:52:24 回复(0)
#include <stdio.h>

int main(){
    int a[10] = { 0 };
    int neg =0, pos = 0;
    for(int i = 0; i < 10; i++){
        scanf("%d", &a[i]);
        a[i] > 0 ? pos++ : neg++;
    }
    printf("positive:%d\nnegative:%d\n", pos, neg);
    return 0;
}

发表于 2022-05-22 15:56:24 回复(0)
#include<stdio.h>
int main(){
    int arr[10];
    for(int i=0;i<10;i++){
    scanf("%d",&arr[i]);
    }
    int count1=0;
    int count2=0;
    for(int i=0;i<10;i++){
        if(arr[i]>0){
            count1++;
        }else if(arr[i]<0){
            count2++;
        }
    }
    printf("positive:%d\n",count1);
    printf("negative:%d\n",count2);
    return 0;
}

发表于 2024-10-30 14:57:08 回复(0)
#include <stdio.h>

int main() {
    int a[10] = {0};
    int positive_number = 0;
    int negative_number = 0;
    for (int i = 0; i < 10; i++) {
        scanf("%d ", &a[i]);
        if (a[i] > 0) {

            positive_number++;
        }
        if (a[i] < 0) {
            negative_number++;
        }
    }
    printf("positive:%d\nnegative:%d", positive_number,negative_number);
    return 0;
}

编辑于 2024-01-11 11:21:09 回复(0)
#include <stdio.h>

int main()
{
    int a=0;
    int b=0,c=0;
    int i=0;
    for(i=0;i<10;i++)
    {
        scanf("%d ",&a);
        if(a>0)
        b++;
        else if(a<0)
        c++;
    }
    printf("positive:%d\n",b);
    printf("negative:%d\n",c);
    return 0;
}
编辑于 2023-12-10 19:17:02 回复(0)
#include <stdio.h>

int main() {
    int positive = 0,negative = 0,num = 0;

    for (int i = 0; i < 10; i++)
    {
        scanf("%d",&num);
        if (num > 0)
            positive++;
        if (num < 0)
            negative++;
    }
    printf("positive:%d\n",positive);
    printf("negative:%d\n",negative);
    
    return 0;
}

发表于 2023-06-09 00:07:06 回复(0)
num=list(map(int,input().split()))
p=0
n=0
for i in num:
    if i>=0:
        p+=1
    else:
        n+=1  
print('positive:{}'.format(p))
print('negative:{}'.format(n))   

发表于 2023-01-18 10:08:45 回复(0)
#include <stdio.h>

int main()
{
    int x;
    int p = 0, n = 0;
    while(scanf("%d", &x) != EOF)
    {
        if(x > 0)
            p++;
        else
            n++;
    }
    printf("positive:%d\nnegative:%d\n",p, n);

    return 0;
}

发表于 2022-11-04 18:29:07 回复(0)
#include<iostream>
using namespace std;
int main(){
    int positive = 0, negative = 0, temp;
    while(cin >> temp){
        if(0 <= temp)
            positive++;
        else
            negative++;
    }
    cout << "positive:" << positive << endl;
    cout << "negative:" << negative << endl;
    return 0;
}

发表于 2022-08-17 19:59:42 回复(0)
#include <stdio.h>
int main()
{
    int a, n = 0, p = 0;
    while(scanf("%d", &a) != EOF)
    {
        if(a > 0)
        {
            p++;
        }
        if(a < 0)
        {
            n++;
        }
    }
    printf("positive:%d\nnegative:%d", p, n);
    return 0;
}


发表于 2022-06-27 15:33:16 回复(0)
#include<iostream>
using namespace std;

int main()
{
   int cnt = 10;
   int pn = 0, nn = 0;
    while(cnt --)
    {
        int x;
        cin >> x;
        if (x > 0) pn ++;
        if (x < 0) nn ++;
    }
    cout << "positive:" << pn << endl;
    cout << "negative:" << nn << endl;
}

发表于 2022-02-26 14:09:19 回复(0)
int main(){
    int n;
    int neg = 0;
    int pos = 0;
    for(int i = 0; i < 10; i++){
        scanf("%d ", &n);
        if(n > 0)
            pos++;
        else
            neg++;
    }
    printf("positive:%d\nnegative:%d", pos, neg);
    return 0;
}

发表于 2021-11-28 11:13:20 回复(0)
#include <stdio.h>

int main()
{
    int sum = 0;
    int positive = 0;
    int negative = 0;

    while (scanf("%d", &sum) != EOF)
    {
        if (sum >= 0)
        {
            positive++;
        }
        else
        {
            negative++;
        }

    }

    printf("positive:%d\nnegative:%d", positive, negative);

    return 0;
}
发表于 2025-06-09 01:04:22 回复(0)
#include <stdio.h>

int main()
{
    int i = 0,j = 0;
    int p = 0,n = 0;

    for(i = 0;i < 10;i++)
    {
       scanf("%d",&j);
       if(j > 0) p++;
       if(j < 0) n++;
    }  
    printf("positive:%d\n",p);
    printf("negative:%d",n);
    return 0;
}
发表于 2025-04-24 21:28:37 回复(0)
#include<stdio.h>
int main()
{
    int a[10];
    for(int i=0;i<10;i++)
    {
        scanf("%d",&a[i]);
    }
    int cou=0;
    for(int i=0;i<10;i++)
    {
        if(a[i]>0)
        {
            cou++;
        }
    }
    printf("positive:%d\n",cou);
    printf("negative:%d",10-cou);
    return 0;
}
发表于 2025-02-18 19:50:16 回复(0)
a=input().split()
b,c=0,0
for i in a:
    if int(i)<0:
        b+=1
    else:
        c+=1
print(f'positive:{c}\nnegative:{b}')
发表于 2025-02-17 11:14:07 回复(0)
#include <stdio.h>
int main() {
    long long int arr[10] = {0};
//将10个数存入数组中
    int pc = 0,nc = 0;
//pc统计正值个数,nc统计负值个数
    for (int i = 0; i < 10; ++i){
        scanf("%lld",&arr[i]);
        if (arr[i] > 0){
            pc++;
        }
        else if (arr[i] < 0){
            nc++;
        }
    }
    printf("positive:%d\n",pc);
    printf("negative:%d\n",nc);
    return 0;
}
发表于 2025-01-05 18:54:21 回复(0)
#include <stdio.h>

int main() {
    int num=0;
    int i=0;
    int positive=0;
    int negative=0;
    for(i=0;i<10;i++)
    {
        scanf("%d",&num);
        if(num>0)
        {
            positive++;
        }
        else if(num<0)
        {
            negative++;
        }
    }
    printf("positive:%d\nnegative:%d\n",positive,negative);
    return 0;
}

发表于 2024-12-06 20:42:22 回复(0)
解题思路:
1、先对输入的列表进行sort(reverse=False)升序排序
2、再创建2个空列表,一个存正数,一个存负数
3、循环排序后的列表,小于1的放入负数,大于等于1的放入正数
4、计算列表的长度
num=list(map(int,input().split()))
#print(num) #[-1, 2, 3, -6, 7, 8, -1, 6, 8, 10]
num.sort(reverse=False)
#print(num)#[-6, -1, -1, 2, 3, 6, 7, 8, 8, 10]
positive=[]
negative=[]

for i in num:
    if i<1:
        negative.append(i)
    else:
        positive.append(i)
#print(len(positive))
#print(len(negative))
print(f'positive:{len(positive)}',f'negative:{len(negative)}',sep='\n')

发表于 2024-11-29 13:15:36 回复(0)