Tính trung bình cộng

View as PDF



Author:
Problem types
Points: 100 (p) Time limit: 1.0s Memory limit: 1G Input: stdin Output: stdout

Nhập vào một dãy \(N\) số nguyên \(A_{1},A_{2},...,A_{N}\).

Hãy in ra màn hình Trung bình cộng các phần tử âm.

Input

  • Dòng đầu tiên chứa số nguyên \(N\).
  • \(N\) dòng tiếp theo chứa \(N\) số nguyên \(A_{1},A_{2},...,A_{N}\).

Output

  • In ra Trung bình cộng các phần tử âm lấy \(2\) số lẻ sau phần thập phân, nếu trong dãy không có số âm nào thì in ra \(−1\).

Constraints

  • \(1 \leq n \leq 10000\)
  • \(|A_{i}| \leq 10^{9}\)

Example

Test 1

Input
7
7
6
-4 
19 
-22
51 
-82 
Output
-36.00

Comments


  • 1
    tienthanh1602ltqb    6:30 p.m. 8 aug, 2024

    FULL

    include <bits/stdc++.h>

    using namespace std;
    #define ll long long
    #define tt ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
    int main(){
    double n,x;
    cin>>n;
    double t=0,d=0;
    for (int i=0;i<n;i++) { cin>>x;
    if (x<0)
    {
    d++;
    t=t+x;
    }
    }
    double s=t/d;
    if (d==0) cout<<"-1";
    else cout<<fixed<<setprecision(2)<<s;
    return 0;
    }


    • 1
      NewbieCpp    7:00 p.m. 4 aug, 2024

      (Code C++)

      #include <iostream>
      
      using namespace std;
      int N,S,t,i;
      int main()
      {
          for(i=1;i<=8;i++)
          {
              cin>>N;
              if(N<0)
              {
                  t++;
                  S+=N;
              }
          }
          cout<<S/t;
          return 0;
      }
      

      • 0
        nguyenanhkiet123    10:18 a.m. 17 jul, 2024

        n=int(input())
        a=0
        c=0
        for i in range(1,n+1):
        b=int(input())
        if(b<0):
        a+=b
        c+=1
        if (c>0):
        print(round(a/c,2),end='0')
        else:
        print("-1")


        • 1
          Avocadorable    8:06 p.m. 23 feb, 2024
          from statistics import mean
          
          n = int(input())
          lst = []
          
          for i in range(n):
              temp = int(input())
              if temp < 0:
                  lst.append(temp)
          
          if lst:
              print(f"{mean(lst):.2f}")
          else:
              print("-1")
          

          • 1
            BETHIEN    6:39 p.m. 20 feb, 2024

            n = int(input())
            a = [int(input()) for _ in range(n)]
            sum = 0
            dem = 0
            for i in range(n):
            if a[i] < 0:
            sum += a[i]
            dem += 1
            if dem == 0:
            print('-1')
            else:
            tbc = sum / dem
            print("{:.2f}".format(tbc))


            • 1
              vuhainamcrm    6:03 p.m. 10 aug, 2023

              n=int(input())
              A=[]
              for i in range(n):
              ai=int(input())
              A.append(ai)
              s=0
              tbc=0
              dem=0
              for k in range(n):
              if A[k]<0:
              s=s+A[k]
              dem=dem+1
              if dem>0:
              tbc=s/dem
              print("{:.2f}".format(tbc))
              else:
              print('-1')

              1 reply

              • -9
                minhtuanitk20    9:28 p.m. 3 oct, 2021

                This comment is hidden due to too much negative feedback. Click here to view it.

                1 reply

                • -18
                  tranminhkhoi    8:50 p.m. 17 dec, 2020

                  This comment is hidden due to too much negative feedback. Click here to view it.


                  • -22
                    tranminhkhoi    8:50 p.m. 17 dec, 2020

                    This comment is hidden due to too much negative feedback. Click here to view it.


                    • -34
                      manhnd    6:33 p.m. 17 dec, 2020

                      This comment is hidden due to too much negative feedback. Click here to view it.

                      2 replies