首先输入要输入的整数个数n,然后输入n个整数。输出为n个整数中负数的个数,和所有正整数的平均值,结果保留一位小数。
0即不是正整数,也不是负数,不计入计算。如果没有正数,则平均值为0。
数据范围: ,输入的整数都满足
首先输入一个正整数n,
然后输入n个整数。
输出负数的个数,和所有正整数的平均值。
11 1 2 3 4 5 6 7 8 9 0 -1
1 5.0
3 0 0 0
0 0.0
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int num = sc.nextInt(); int[] arr = new int[num]; for(int i=0;i<num;i++){ arr[i]=sc.nextInt(); } calculate(num,arr); } sc.close(); } public static void calculate(int num,int[] arr){ int numFu = 0; int numZheng = 0; int sum = 0; for(int i=0;i<num;i++){ if(arr[i]<0){ numFu++; } if(arr[i]>0){ numZheng++; sum += arr[i]; } } System.out.print(numFu+" "); System.out.format("%.1f",(float)sum/numZheng); System.out.println(); } }
//一种简单的做法 import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner s = new Scanner(System.in); while(s.hasNext()){ int n = s.nextInt(); int[] arr = new int[n]; int countf = 0; int countz = 0; double sum = 0.0; double p = 0.0; for(int i = 0;i < n;i++){ arr[i] = s.nextInt(); if(arr[i] < 0){ countf++; } if(arr[i] > 0){ countz++; sum += arr[i]; p = (double)(sum / countz); } } System.out.println(countf + " " + String.format("%.1f",p)); } } }
//注意一位小数的表达形式setiosflags(ios::fixed)<<setprecision(1)<< #include<iostream> #include<iomanip> using namespace std; int main(){ int n; while(cin>>n){ int* a=new int[n],total=0,f_num=0,z_num=0; for(int i=0;i<n;i++){ cin>>a[i]; if(a[i]>0){ total+=a[i]; z_num++; } else if(a[i]<0) f_num++; } cout<<f_num<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<(double)total/z_num<<endl; } }
#include<stdio.h> int main(){ int n,m; while(~scanf("%d",&n)){ int negative=0,positive=0; float sum=0; for(int i=0;i<n;i++){ scanf("%d",&m); if(m<0) negative++; if(m>0){ positive++; sum += m; } } printf("%d ",negative); if(positive==0) printf("0.0\n"); else printf("%.1f\n",sum/positive); } }
#include"stdio.h" int main() { int n=0; int a[2000]={0}; while(scanf("%d",&n)!=EOF) { int count=0; double sum=0.0; int m=0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); } for(int i=0;i<n;i++) { if(a[i]<0) count++; if(a[i]>0) { m++; sum+=a[i]; } } printf("%d %.1lf\n",count,sum/m); } return 0; }
import java.util.*; public class Main{ public static void main(String []args){ Scanner scan=new Scanner(System.in); while(scan.hasNext()){ int n=scan.nextInt(); int zheng=0,fu=0; float sum=0; while(n>0){ int x=scan.nextInt(); if(x<0){ fu++; }else if(x>0){ sum+=x; zheng++; } n--; } System.out.printf("%d %.1f\n",fu,sum/zheng); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(), num = 0, posNum = 0; int[] a = new int[n]; double total = 0; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); for (int i : a) { if (i < 0) { num++; } else if (i > 0) { posNum++; total += i; } } System.out.printf("%d %.1f%n", num, total / posNum); } sc.close(); } }
#include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { int num; while (cin >> num) { vector<int> v(num); int negative_num = 0, positive_num = 0, tmp, sum = 0; while (num--) cin >> tmp, v.push_back(tmp); for (size_t i = 0; i < v.size(); ++i) if (v[i] < 0) negative_num++; else if (v[i] > 0) { positive_num++; sum += v[i]; } cout << negative_num << " " << fixed << setprecision(1) << (double) sum / positive_num << endl; } return 0; }
#include <stdio.h> int main() { int n; while (scanf("%d", &n) != EOF) { double total = 0.0f; int neg_num = 0, positive_num = 0, tmp; for (int i = 0; i < n; i++) { scanf("%d", &tmp); if (tmp < 0) { neg_num++; } else if (tmp > 0) { total += tmp; positive_num++; } } printf("%d %.1f\n", neg_num, total / positive_num); } return 0; }
while True: try: n, neg_count, pos_list = int(input().strip()), 0, [] nums = map(int, input().strip().split(' ')) for num in nums: if num < 0: neg_count += 1 if num > 0: pos_list.append(num) if len(pos_list) == 0: avg = '0.0' else: # 格式化字符串的结果一定是字符串 avg = '%.1f' % (sum(pos_list)/len(pos_list)) print(str(neg_count) + ' ' + avg) except: break
#include<bits/stdc++.h> using namespace std; int main() { int m,n; while(cin>>n) { int count=0,count2=0,sum=0; double av; for(int i=0;i<n;i++) { cin>>m; if(m<0) count++; if(m>0) { count2++; sum+=m; } } av=(double)sum/count2; cout<<count<<" "; printf("%.1lf\n",av); } return 0; }
#include <iostream> using namespace std; int main() { int n,a; while(cin>>n)//这里如果改成if就通过不了,估计平台会用好几组数据去循环测试该代码 { int num1=0,num2=0,sum=0; for(int i=0;i<n;i++) { cin>>a; if(a<0) num1++; else if(a>0) { num2++; sum+=a; } } printf("%d %.1f\n",num1,(double)sum/num2); } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ for(int n;cin>>n;){ vector<int> vec(n); for(auto& i:vec) cin>>i; int negNum = 0,zeroNum=0,sum = 0; for(auto i:vec){ if(i<0) ++negNum; else if(i==0) ++zeroNum; else sum += i; } //使用setprecision(1)与fixed来对浮点数四舍五入到1位小数 cout << negNum << ' ' << setprecision(1) << fixed << (double)sum /(n-negNum-zeroNum) << endl; } }
while True: try: count=0 num=0 zero=0 n=int(input()) data=list(map(int,input().split(' '))) print(data) for i in range(n): if data[i]<0: count=count+1 elif data[i]>0: num=num+data[i] else: zero=zero+1 ave=num/(n-count-zero) print(count,int(ave)) except: break
from functools import reduce while True: try: n = int(input()) num = list(map(int,input().split())) negative = [] positive = [] for i in num: if i < 0: negative.append(i) elif i > 0: positive.append(i) print(len(negative),end=' ') print(round(((reduce(lambda x,y:x+y,positive))/(len(positive))),1)) except: break
#include<iostream> #include<vector> #include<iomanip> using namespace std; int main() { int n; vector<int> arr; while (cin >> n) { arr.resize(n); int count1 = 0; int count2 = 0; double arv = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] - 0 > 0.0000001) { count1++; arv += arr[i]; } else if (arr[i] < 0) { count2++; } } printf("%d %0.1lf\n",count2,arv/count1); } return 0; }判断的时候要注意0,不是正整数也不是负数,要跳过或者不算
#include <iostream> #include <iomanip> using namespace std; int main() { int n; while (cin >> n) { int negative_count = 0; int positive_total = 0; int positive_count = 0; # 遍历数字,需要注意的是,零不要算在内。 for(int i=1; i<=n; i++){ int j; cin >> j; if(j < 0){ negative_count++; } else if(j > 0) { positive_total += j; positive_count++; } } cout << negative_count << ' '; # 使用setprecision(1)与fixed来对浮点数四舍五入到1位小数。 cout << setprecision(1) << fixed << (double) positive_total / (double) positive_count << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { int m, cnt1 = 0, cnt2 = 0, sum = 0; vector<int> dp; for (int i = 0; i<n; i++) { cin >> m; dp.push_back(m); } for (int i = 0; i<n; i++) { if (dp[i]<0) cnt1++; else if(dp[i]>0) { cnt2++; sum += dp[i]; } } double avg = 1.0*sum / cnt2; cout << cnt1 << " "<<fixed<<setprecision(1)<< avg << endl; } system("pause"); return 0; }