本题的基本要求非常简单:给定N个实数,计算它们的平均值。但复杂的是有些输入数据可能是非法的。一个“合法”的输入是[-1000,1000]区间内的实数,并且最多精确到小数点后2位。当你计算平均值的时候,不能把那些非法的数据算在内。
输入格式:
输入第一行给出正整数N(<=100)。随后一行给出N个实数,数字间以一个空格分隔。
输出格式:
对每个非法输入,在一行中输出“ERROR: X is not a legal number”,其中X是输入。最后在一行中输出结果:“The average of K numbers is Y”,其中K是合法输入的个数,Y是它们的平均值,精确到小数点后2位。如果平均值无法计算,则用“Undefined”替换Y。如果K为1,则输出“The average of 1 number is Y”。
输入样例1:
7
5 -3.2 aaa 9999 2.3.4 7.123 2.35
输出样例1:
ERROR: aaa is not a legal number
ERROR: 9999 is not a legal number
ERROR: 2.3.4 is not a legal number
ERROR: 7.123 is not a legal number
The average of 3 numbers is 1.38
输入样例2:
2
aaa -9999
输出样例2:
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined 方法一
package com.hone.basical; import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* 原题目:https://www.patest.cn/contests/pat-b-practise/1054
* @author Xia
* 注意:1、合法数字个数是0的时候The average of 0 numbers is Undefined
* 2、合法数字个数是1的时候 要输出The average of 1 number is Y
*/ public class basicalLevel1054average { public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt(); double total = 0;
int totalNum = 0;
//整理思路利用正则表达式判断输入的是否是位于[-1000,1000]之间,
//最多精确到小数点后2位的数字
for (int i = 0; i < N; i++) {
String mayNum = in.next();
if (isNum(mayNum)) { //如果是数字
double num =Double.parseDouble(mayNum);
if (num<=1000&&num>=-1000) {
total+=Double.parseDouble(mayNum);
totalNum++;
}else {
System.out.println("ERROR: "+mayNum +" is not a legal number");
}
}else {
System.out.println("ERROR: "+mayNum +" is not a legal number");
}
}
if (totalNum>1) {
DecimalFormat df =new DecimalFormat("##0.00");
String sp = df.format(total/(double)totalNum);
System.out.println("The average of "+totalNum +" numbers is "
+sp);
}else if (totalNum == 1) {
System.out.printf("The average of %.0f number is %.2f\n", totalNum, total / totalNum);
}else {
System.out.println("The average of 0 numbers is Undefined");
} } //定义函数判断是否是合法的数字
public static boolean isNum(String str){
String p = "((\\-?)(\\d+))(\\.(\\d){0,2})?";
Pattern pattern = Pattern.compile(p);
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
}
方法2
package com.hone.basical; import java.util.Scanner; /*
* 来源:http://blog.csdn.net/qq_34594236/article/details/51714618
* 思路:
* 1.因为输入数字不一定是合法的 所以不能用nextDouble();
* 2.所以这里采用字符串输入
* 3.将字符串转变成double型数,如果无法转换(即非法数)则捕捉异常,输出相应语句
* 4.如果该字符串能转变成double型数,则进一步判断是否为合法数(题目规定-1000<=x<=1000 ,并且最多精确到小数点后2位);
* 5.这里介绍主要介绍两种判断是否最多是2位小数
* 第一种:将数字转换成精确到2位小数,求与原来的作差的绝对值;如果是0则符合,否则多余2位(该方法不是很严谨)如果输入数据是1.000000则该数字也合法,显然是错误的,但是测试数据没有这类型数据
* 第二种:将字符串长度-“.”的位置-1;即可算出小数点后有几位数字
* 这里面需要灵活的利用java中的try——catch机制
*/ public class basicalLevel1054average2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
String s = sc.nextLine(); String[] number = s.split(" "); double sum = 0;
double counts = 0;
for (int i = 0; i < n; i++) {
try {
double x = Double.parseDouble(number[i]);
int times = 0;
if (number[i].contains(".")) {
times = number[i].length() - number[i].indexOf(".") - 1;
}
if (x >= -1000 && x <= 1000 && times <= 2 && times >= 0) {
sum += x;
counts++;
} else {
throw new Exception();
}
} catch (Exception e) {
System.out.printf("ERROR: %s is not a legal number\n", number[i]);
}
} if (counts == 0) {
System.out.printf("The average of %.0f numbers is Undefined", counts);
} else if (counts == 1) {
System.out.printf("The average of %.0f number is %.2f\n", counts, sum / counts);
} else {
System.out.printf("The average of %.0f numbers is %.2f\n", counts, sum / counts);
} }
}