首页 > 试题广场 >

统计数据正负个数

[编程题]统计数据正负个数
  • 热度指数: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 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() {
    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 arr[10]={0};
int zheng=0,fu=0;
int i=0;
for(i=0;i<=9;i++)
{
    scanf("%d",&arr[i]);
if(arr[i]>0)
   zheng++;
else if(arr[i]<0)
    fu++;

}
printf("positive:%d\n",zheng);
printf("negative:%d\n",fu);

    return 0;
}
发表于 2024-08-01 15:31:28 回复(0)
#include <stdio.h>

int main()
{
    int arr[10] = { 0 };
    int i = 0;
    for(i = 0; i < 10; i++) //多个值的输入
    {
        scanf("%d", &arr[i]);
    }
    int n =0, p = 0;
    for(i = 0; i < 10; i++) //遍历数组
    {
        if(arr[i] < 0)
            n++;
        else
            p++;
    }
    printf("positive:%d\n", p);
    printf("negative:%d\n", n);

    return 0;
}

编辑于 2024-04-16 10:43:48 回复(0)
#include <stdio.h>

int main() {
    int a[10];
    int positive = 0;
    int negative = 0;
    for(int i=0; i <= 9; i++){
        scanf("%d", &a[i]);
        if(a[i]>=0){
            positive++;
        }
        else{
            negative++;
        }
    }
    printf("positive:%d\n", positive);
    printf("negative:%d\n", negative);
    return 0;
}
编辑于 2024-01-14 12:12:33 回复(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 n = 0;
    int a = 0;
    int b = 0;
    while (scanf("%d ",&n)!=EOF) 
    {
        if (n > 0)
        {
            a++;
        }
        else if (n < 0)
        {
            b++;
        }
    }
    printf("positive:%d\n", a);
    printf("negative:%d", b);
    return 0;
}

//第二种方法

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

发表于 2023-11-05 17:13:50 回复(0)
#include <stdio.h>

int main() {
    int a,x,y; //x,y用来累计正负数的个数
    x=0;
    y=0;
    for(int i=0; i<10; i++){
        scanf("%d",&a);
        if(a>0) x++; //不论输入正数还是负数都可以被x或y累计一次
        else y++;
    }
    printf("positive:%d\nnegative:%d",x,y);
    return 0;
}

发表于 2023-06-12 00:25:29 回复(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)
#include <stdio.h>

int main() {
    int arr[10] = {0};
    int pos = 0, neg = 0;
    for (int i = 0; i < 10; i++) {
        scanf("%d", &arr[i]);
    }
    for (int i = 0; i < 10; i++) {
        if (arr[i] < 0) neg++;
        else pos++;
    }
    printf("positive:%d\n", pos);
    printf("negative:%d\n", neg);
    return 0;
}

发表于 2023-03-20 19:07:28 回复(0)
#include <stdio.h>
int main() {
    // 存放输入
    long a[10];
    // 存放输入的变量
    int neg = 0, pos = 0;
    // 输入
    for (int i = 0; i < (sizeof(a) / sizeof(long)); i++) {
        scanf("%ld", &a[i]);
    }
    // 处理
    for (int j = 0; j < (sizeof(a) / sizeof(long)); j++) {
        if (a[j] > 0) {
            pos++;
        } else {
            neg++;
        }
    }
    // 输出
    printf("positive:%d\nnegative:%d\n", pos, neg);

    return 0;
}
发表于 2023-02-10 09:18:56 回复(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 <stdio.h>
int flag1=0,flag2=0;
int main() {
    int arr[10];
    for(int i=0;i<10;i++){
        scanf("%d",&arr[i]);
        if(arr[i]>0)
        flag1++;
        else
        flag2++;
    }
    printf("positive:%d\nnegative:%d",flag1,flag2);
}

发表于 2022-10-25 09:16:46 回复(0)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int a[10];
    int i,z=0,f=0;
    for(i=0;i<10;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]>0)
            z++;
        else if(a[i]<0)
            f++;
    }
    printf("positive:%d\n",z);
    printf("negative:%d",f);
    return 0;
}

发表于 2022-08-17 11:26:04 回复(0)
发表于 2022-08-05 19:21:54 回复(0)
#include<stdio.h>
int main(){
    int a[10],i,n=0;
    for(i=0;i<10;i++){
        scanf("%d ",&a[i]);
    }
    for(i=0;i<10;i++){
        if(a[i]>=0){
            n++;
        }
    }
    printf("positive:%d\n",n);
    printf("negative:%d\n",10-n);
}
发表于 2022-07-18 12:26:03 回复(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 <stdio.h>
int main()
{
    int n=0,conat=0,sonat=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(n>=0)
        {
            conat++;
        }
        else
        {
            sonat++;
        }
    }
    printf("positive:%d\nnegative:%d\n",conat,sonat);
    return 0;
}
发表于 2022-06-11 16:36:34 回复(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)