首先输入要输入的整数个数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
// 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); int count=0; int sum = 0; int count2=0; for(int i=0;i<a;i++){ int b = in.nextInt(); if(b<0){ count++; }else if(b>0){ sum+=b; count2++; } } double avg =0.0; if(count2!=0){ avg = (double)sum/count2; } System.out.print(count+" "+String.format("%.1f",avg)); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); int sum = 0; int positiveNumber = 0; int negativeNumber = 0; for(int i= 0; i < a;i++){ int n = in.nextInt(); if(n > 0 && n != 0){ sum += n; positiveNumber ++; }else if (n < 0){ negativeNumber++; } } double dou = 0; if (positiveNumber != 0) { dou = (double) sum/positiveNumber; } System.out.print(negativeNumber+" "+String.format("%.1f",dou)); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 ArrayList<Integer> list=new ArrayList<>(); int n=in.nextInt(); int sum=0; for(int i=0;i<n;i++){ Integer tmp=Integer.valueOf(in.next()); if(tmp<0){ sum++; }else if(tmp>0){ list.add(tmp); } } Double result=0.0; for(int i=0;i<list.size();i++){ result+=list.get(i); } if(list.size()==0){ System.out.print(sum+" "+0.0); }else{ result=result/list.size(); System.out.print(sum+" "+String.format("%.1f",result)); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while((str=br.readLine())!=null){ int cou=Integer.parseInt(str); int[] dd=new int[cou]; String[] strArr=br.readLine().split(" "); for(int i=0;i<cou;i++){ dd[i]=Integer.parseInt(strArr[i]); } int fu=0; double sum=0.0; int lin=0; for(int i=0;i<cou;i++){ if(dd[i]<0){ fu++; continue; }else if(dd[i]==0){ lin++; continue; } sum+=dd[i]; } if(cou-fu-lin==0){ System.out.println(fu+" "+"0.0"); }else{ double s=sum/(cou-fu-lin); // System.out.println(fu+" "+String.format("%.1f",s)); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int length = scanner.nextInt(); int[] ar = new int[length]; for (int i = 0; i < length; i++) { ar[i] = scanner.nextInt(); } int counts_=0; int counts=0;int sum=0; for (int i = 0; i < length; i++) { if(ar[i]>0) {sum+=ar[i];counts+=1;} if(ar[i]<0) counts_+=1; } double avg= Math.round(sum*1.0/counts*10)*1.0/10; System.out.println(counts_+" "+avg); } }
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(); int[] ints = new int[n]; for (int i = 0; i < ints.length; i++) { ints[i] = sc.nextInt(); } solution(ints); } } private static void solution(int[] ints) { double avg = 0.0; int countB = 0; int countS = 0; for (int anInt : ints) { if (anInt > 0) { avg += anInt; countB++; } else if (anInt < 0) { countS++; } } if (countB > 0) { avg /= countB; } String avg1 = String.format("%.1f", avg); System.out.println(countS + " " + avg1); } }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int s1 = 0,s2 = 0; double sum = 0.0; int[] arr = new int[n]; for(int i = 0;i<n;i++){ arr[i] = sc.nextInt(); } for(int i = 0;i<arr.length;i++){ if(arr[i] <0) s1++; if(arr[i] >0){ sum= sum+arr[i]; s2++; } } if(sum==0){ System.out.print(s1+" "+"0.0"+"\n"); }else if(sum!=0){ System.out.print(s1+" "+String.format("%.1f",sum/s2)+"\n"); } } }
import java.text.DecimalFormat; 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(); int countFu = 0; int countZh = 0; double sum = 0.0; for (int i = 0; i < n; i++) { int in = sc.nextInt(); if (in < 0) { countFu++; } if (in > 0) { countZh++; sum = sum + in; } } System.out.println(countFu + " " + (countZh > 0? new DecimalFormat("######0.0").format(sum/countZh) : 0.0)); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ int n = sc.nextInt(); //负数个数 int negCount = 0; //正数之和 int pos = 0; //正数个数 int posCount = 0; for(int i = 0; i < n; i++){ int num = sc.nextInt(); if(num < 0){ negCount++; } if(num > 0){ pos += num; posCount++; } } System.out.printf("%d %.1f\n",negCount,(double)pos/posCount); } } }
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(); int [] a = new int[n]; int c1 = 0; int c2 = 0; double sum = 0; for(int i = 0; i < n; i++){ a[i] = sc.nextInt(); if(a[i] < 0){ c1 ++; } if(a[i] > 0){ c2 ++; sum += a[i]; } } double average = sum / c2; System.out.printf("%d %.1f\n", c1, average); } } }
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 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)); } } }