输入 n 个整型数,统计其中的负数个数并求所有非负数的平均值,结果保留一位小数,如果没有非负数,则平均值为0
本题有多组输入数据,输入到文件末尾。
数据范围: ,其中每个数都满足
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int sum = 0; int countFu = 0; int countNoFu = 0; int num; while (in.hasNextInt()) { // 注意 while 处理多个 case num = in.nextInt(); if (num < 0) { countFu++; } else { sum += num; countNoFu++; } } double a = countNoFu == 0.0 ? 0.0 : 1.0 * sum / countNoFu ; System.out.println(countFu); System.out.println(String.format("%.1f", a)); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int positive = 0; int negative = 0; int sum = 0; while (in.hasNext()) { int n = in.nextInt(); if (n > 0) { sum += n; positive ++; } else { negative++; } } double d = positive == 0 ? 0.0 : (double) sum / positive; System.out.println(negative); System.out.println(String.format("%.1f", d)); } }
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 sum=0; while(in.hasNext()){ int num=in.nextInt(); if(num<0){ sum++; }else if(num>0){ list.add(num); } } Double result=0.0; for(int i=0;i<list.size();i++){ result+=list.get(i); } result=result/list.size(); System.out.println(sum); if(list.size()==0){ System.out.print(0.0); return; } System.out.print(String.format("%.1f",result)); } }
import java.util.Scanner; import java.lang.Integer; import java.util.ArrayList; public class Main { public static void main(String[] args) { test(); } public static void test() { Scanner sc = new Scanner(System.in); ArrayList<Integer> list = new ArrayList<>(); int count = 0; int sum = 0; double avg=0; String s = sc.nextLine(); String[] split = s.split(" "); for (int i = 0; i < split.length; i++) { int num = Integer.valueOf((split[i])); if (num < 0) { ++count; continue; } list.add(num); } System.out.println(count); if (list.size() > 0) { for (int i = 0; i < list.size(); i++) { sum += list.get(i); } avg= (double)sum / (list.size()); System.out.printf("%.1f",avg); } else { System.out.println(0.0); } sc.close(); } }看来是多行的题目,这么做不对?
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int positiveCnt = 0; int negativeCnt = 0; int sum = 0; while(sc.hasNext()){ int n = sc.nextInt(); if(n>0){ sum += n; positiveCnt++; }else{ negativeCnt++; } } double res = positiveCnt==0 ? 0.0:sum*1.0/positiveCnt; System.out.println(negativeCnt+"\n"+String.format("%.1f",res)); } }
import java.util.*; public class Main{ public static void main(String[]args){ Scanner sc = new Scanner(System.in); int positiveCnt = 0; int negativeCnt = 0; int sum = 0; while(sc.hasNext()){ int n = sc.nextInt(); if(n>0){ sum += n; positiveCnt++; }else{ negativeCnt++; } } double res = positiveCnt==0 ? 0.0:sum*1.0/positiveCnt; System.out.println(negativeCnt+"\n"+String.format("%.1f",res)); } }
import java.util.*; import java.math.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); float count1 = 0; int count2 = 0; float sum = 0; while (sc.hasNextInt()){ int cur = sc.nextInt(); if (cur >= 0){ count1++; sum += cur; }else{ count2++; } } System.out.println(count2); BigDecimal val1 = BigDecimal.valueOf(sum); BigDecimal val2 = BigDecimal.valueOf(count1); System.out.println(val2.floatValue() == 0 ? 0.0f : val1.divide(val2, 1, RoundingMode.HALF_UP).floatValue()); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int numOfNeg = 0; int numOfNonNeg = 0; double sumOfNoneNeg = 0.0; while (in.hasNextDouble()) { double x = in.nextDouble(); if (x < 0.0) { numOfNeg++; } else { sumOfNoneNeg += x; numOfNonNeg++; } } System.out.println(numOfNeg); if (numOfNonNeg == 0) { System.out.println(0.0); } else { System.out.println(String.format("%.1f", sumOfNoneNeg / (double)numOfNonNeg)); } } } }
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); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = 0; int count2 = 0; int total = 0; while (sc.hasNext()) { int temp = sc.nextInt(); if (temp >= 0) { total += temp; count2 ++; } else { count ++; } } System.out.println(count); if (count2 == 0) { System.out.println(0.0); } else { System.out.printf("%.1f%n", (double) total / count2); } } }
...... int countfu = 0; int countsum = 0; float countzeng = 0; ...... float f = countsum/countzeng; ......
import java.util.Scanner; //按题意计数和求平均 public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); double average=0, sum=0; int countNegative=0, countPositive=0; int num=0; while(sc.hasNext()){ num = sc.nextInt(); if(num<0) countNegative++; else{ countPositive++; sum += num; } } System.out.println(countNegative); if(countPositive>0) average = sum/countPositive; System.out.printf("%.1f", average); } }
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)); } }