首页 > 试题广场 >

记负均正II

[编程题]记负均正II
  • 热度指数:225723 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入 n 个整型数,统计其中的负数个数并求所有非负数的平均值,结果保留一位小数,如果没有非负数,则平均值为0
本题有多组输入数据,输入到文件末尾。

数据范围: ,其中每个数都满足

输入描述:

输入任意个整数,每行输入一个。



输出描述:

输出负数个数以及所有非负数的平均值

示例1

输入

-13
-4
-7

输出

3
0.0
示例2

输入

-12
1
2

输出

1
1.5
看前面有人说这道题是一行输入,现在好像又变成多行了。
ans = 0
num = 0
neg = 0
while True:
    try:
        n = int(input())
        if  n > 0:
            ans += n
            num += 1
        else:
            neg += 1
    except EOFError:
        break
print(neg)
print(round(ans/num, 1)) if num > 0 else print(0.0)

编辑于 2020-09-09 11:26:04 回复(2)
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int cntn = 0, cntp = 0;
        float sum = 0;
        while(sc.hasNext()){
            int num = sc.nextInt();
            if(num < 0) cntn++;
            else{
                sum += num;
                cntp++;
            }
        }
        DecimalFormat fnum = new DecimalFormat("##0.0");  
        System.out.println(cntn);
        if(cntp == 0) System.out.println("0.0");
        else System.out.println(fnum.format(sum / cntp));
    }
}
发表于 2018-07-19 11:25:25 回复(0)
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int m;
	int s=0;
	int ct=0;
	int zct=0;
	while(cin>>m)
	{		
		if (m<0)
		{
			++ct;
		}
		else if(m>0)
		{
			s+=m;
			++zct;
		}		
	}
	double v=(double)s/zct;
	cout<<ct<<endl;
	cout<<setiosflags(ios::fixed)<<setprecision(1)<<v<<endl;
	return 0;
}

发表于 2016-05-08 21:03:56 回复(0)
import java.util.*;
 
public class Main{
    public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
        int count1 = 0;
        int count2 = 0;
        float sum = 0;
        while(in.hasNext()){
            int n = in.nextInt();
            if(n<0){
                count1++;
            }else{
                count2++;
                sum+=n;
            }
        }
        float average = sum/count2;
        System.out.println(count1);
        System.out.printf("%.1f\n",average);
	}
}

发表于 2016-04-06 17:01:17 回复(8)
#include<iostream>
#include<vector>

using namespace std;


int main()
{
    int n;
    int count1 = 0;
    int count2 = 0;
    double arv = 0;
    while(cin>>n)
    {
        if(n>=0)
        {
            count1++;
            arv += n;
        }
        else
        {
            count2++;
        }
    }
    printf("%d\n%0.1lf\n",count2,arv/count1);
    return 0;
}

没有太多好说的,很基础的题了,就是后面的输出自己要注意点。而且他是循环输入的,一定要注意
发表于 2019-12-03 18:58:42 回复(2)
import sys
lines = sys.stdin.readlines()
lst = []
for line in lines:
    line.replace('/n', '')
    lst.append(int(line))
s, z, f = 0, 0, 0
for x in lst:
    if x > 0:
        z += 1
        s += x
    elif x < 0:
        f += 1
if not z:
    print(f)
    print(0.0)
else:
    print(f)
    print('%.1f'%(s/z))

发表于 2022-07-27 18:37:10 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        float sum=0;
        float count1=0;
        int count2=0;
        while(sc.hasNextInt()){
            int n=sc.nextInt();
            if(n>=0){
                sum+=n;
                count1++;
            }else{
               count2++; 
            }
        }
        float avg=sum/count1;
        System.out.println(count2);
        if(count1>0){//存在没有整数的情况,如果还输出sum/count1,0/0是非法的
        System.out.print(String.format("%.1f",avg));
       //保留一位小数的正确输出方式
        }else{
         System.out.print(0.0);   
        }
    }
}
发表于 2022-04-01 16:19:13 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //非负数数和
        int pos = 0;
        //非负数个数
        int posCount = 0;
        //负数个属于
        int negCount = 0;
        while(sc.hasNextInt()){
            int n = sc.nextInt();
            if(n >= 0){
                pos += n;
                posCount ++ ;
            }
            if(n < 0){
                negCount++;
            }
           
        }
         System.out.println(negCount);
        if(posCount==0){
            System.out.println(0.0);
        }else{
            System.out.printf("%.1f",(double)pos/posCount);
        }
    }
}

发表于 2021-12-02 00:04:12 回复(0)
......
int countfu = 0;
int countsum = 0;
float countzeng = 0;
......
float f = countsum/countzeng;
......
最开始看到输出结果愣了一下,测试是.1,我是.0
然后想起来最开始把计正数与求正数和都设置为int型变量了。
两个int相除结果还是int型,转换float也没有小数点后数字的。
发表于 2021-10-28 22:25:16 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int countF=0,countZ=0,sum=0;
        while(sc.hasNext()){
            int n=sc.nextInt();
            if(n<0){
                countF++;
            }
            else if(n>0){
                countZ++;
                sum+=n;
            }
        }
        System.out.println(countF);
        if(countZ==0)
            System.out.println(0.0);
        else
            System.out.println(String.format("%.1f",(double)sum/countZ));
    }
}

发表于 2021-07-15 00:24:42 回复(0)
C语言 十行代码搞定
#include <stdio.h> 
int main()
{
	int temp,count_m = 0, count_r = 0;
	float sum = 0;
	while (scanf("%d", &temp) != EOF)
	temp>=0?count_r++,sum+=(float)temp:count_m++;
	printf("%d\n%.1f", count_m, sum / (float)count_r);
    return 0;
}

发表于 2020-07-30 14:36:39 回复(0)
这个获取的输入值是真的坑  他最后一个字符居然是空格    我就说算平均值总是不对 ,还有加的时候需要吧输入值 转换成Number计算
function f5(arr){
    let minus = 0 // 负数
    let positive = [] // 正数
    for (let i = 0 ; i<arr.length ;i++) {
       if(arr[i]){
           if(arr[i]<0){
               minus++
           }else{
               positive.push(arr[i])
           }
       }
    }
    let length = positive.length
    let total = 0
    for (let i in positive){
        total+= Number(positive[i])
    }
    let mean = total/length
    console.log(minus);
    console.log(mean.toFixed(1))
}
while(line=readline()){
    var lines = line.split(' ');
   f5(lines)
}


发表于 2020-07-28 15:42:33 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String[] strs = sc.nextLine().split(" ");
        System.out.println(getNegNum(strs));
        System.out.printf("%.1f",avg(strs));
    }
    
    public static int getNegNum(String[] strs){
        int count = 0;
        for(int i=0;i<strs.length;i++){
            if(Integer.valueOf(strs[i])<0){
                count++;
            }
        }
        return count;
    }
    
    public static double avg(String[] strs){
        double sum = 0;
        for(int j=0;j<strs.length;j++){
            if(Double.valueOf(strs[j])>=0){
                sum+=Double.valueOf(strs[j]);
            }
        }
        try{
           return sum / (strs.length - getNegNum(strs));
        }catch(Exception e){
            return 0;
        }
    }
}
发表于 2020-07-09 11:26:34 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main(){
    int negN=0,posN=0,sum=0;
    for(int n;cin>>n;)
        if(n<0) ++negN;
        else if(n>0){
            ++posN;
            sum += n;
        }
    cout << negN << endl << setprecision(1) << fixed << double(sum)/posN << endl;
}

发表于 2020-06-28 19:02:50 回复(0)
while True:
    try:
        from functools import reduce
        num = list(map(int,input().split()))
        count = 0
        result = []
        for i in num:
            if i < 0:
                count += 1
            elif i > 0:
                result.append(i)
        print(count)
        print(round((reduce(lambda x,y:x+y,result)/len(result)),1))
    except:
        break

发表于 2020-03-31 15:34:26 回复(0)
a = input().split()
b,count1,count2 = 0,0,0
for i in a:
  if int(i) < 0:
    count1 += 1
  else:
    count2 += 1
    b += int(i)
print(count1)
if b:
  print(round(b/count2,1))
else:
  print(0.0)

发表于 2020-02-17 14:03:05 回复(2)
while 1:
    try:
        l = input().split()
        num = 0
        avg = 0
        for i in l:
            if i < 0:
                num += 1
            else:
                avg += int(i)
        print(num)
        print('{:.1f}'.format(avg/(len(l)-num)))
    except:
        break

发表于 2020-02-01 20:30:42 回复(0)
while True:
    try:
        
        number_list = list(map(int, input().split()))
        negative_count = 0
        non_negative_count = 0
        non_negative_sum = 0
        for i in number_list:
            if i < 0:
                negative_count += 1
            else:
                non_negative_count +=1
                non_negative_sum += i
                
        print(negative_count)
        print(round(non_negative_sum / non_negative_count, 1))
        
    except:
        break

发表于 2019-11-30 21:31:17 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.nextLine();
            String[] arrs = str.split(" ");
            int x = 0;// 负数个数
            int sum = 0;// 正数和
            int y = 0;
            for (int i = 0; i < arrs.length; i++) {
                if (arrs[i].charAt(0) == '-') {
                    x++;
                } else {
                    int n=Integer.parseInt(arrs[i]);
                    sum =sum+n;
                    y++;
                }
            }
            double reuslt = 0;
            if (y != 0)
                reuslt = (double) sum / (double) y;
            System.out.println(x);
            System.out.printf("%.1f\n",reuslt);
        }
    }
}
发表于 2019-06-28 20:57:09 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int micnt = 0;
        int zhcnt = 0;
        int zhsum = 0;
        while(sc.hasNext()) {
            int n = sc.nextInt();
            if(n < 0) {
                micnt ++;
            } else {
                zhcnt ++;
                zhsum += n;
            }
        }
        System.out.println(micnt);
        if(zhcnt == 0) {
            zhcnt = 1;
        }
        System.out.format("%.1f",zhsum*1.0/zhcnt);
    }
}

发表于 2018-10-09 18:39:03 回复(0)

问题信息

难度:
385条回答 35243浏览

热门推荐

通过挑战的用户

查看代码