这个必须写, 想了我好多天,哼
1108. Finding Average (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A “legal” input is a real number in [-1000, 1000] and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=100). Then N numbers are given in the next line, separated by one space.
Output Specification:
For each illegal input number, print in a line “ERROR: X is not a legal number” where X is the input. Then finally print in a line the result: “The average of K numbers is Y” where K is the number of legal inputs and Y is their average, accurate to 2 decimal places. In case the average cannot be calculated, output “Undefined” instead of Y. In case K is only 1, output “The average of 1 number is Y” instead.
Sample Input 1:
7
5 -3.2 aaa 9999 2.3.4 7.123 2.35
1108. Finding Average (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A “legal” input is a real number in [-1000, 1000] and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=100). Then N numbers are given in the next line, separated by one space.
Output Specification:
For each illegal input number, print in a line “ERROR: X is not a legal number” where X is the input. Then finally print in a line the result: “The average of K numbers is Y” where K is the number of legal inputs and Y is their average, accurate to 2 decimal places. In case the average cannot be calculated, output “Undefined” instead of Y. In case K is only 1, output “The average of 1 number is Y” instead.
Sample Input 1:
7
5 -3.2 aaa 9999 2.3.4 7.123 2.35
Sample Output 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
Sample Input 2:
2
aaa -9999
Sample Output 2:
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined
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
Sample Input 2:
2
aaa -9999
Sample Output 2:
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined
感悟
1.这个题是我觉得pat甲级20分里面最简单的,但是也是想了好多天才能够得满分的题目。哼,好气!
2.题目不难懂,就是将符合标准的数字进行求平均值,不符合标准的直接输出错误。
3.看到题目,第一反应,正则表达式嘛!当然,正则表达式是可以做出来的,但是必须必须一定一定要注意一类数,像(3.)这样的数字也是double型的!!!!。我就是没有注意只一点才导致我,只能能17分。气, 但是归根到底是自己java基本功不够扎实,double类型中,明确说了3.这样的也是对的,还是要加强基本功的练习啊!正则表达式如下:“-{0,1}[0-9]+.[0-9]{1,2}” || “-{0,1}[0-9]+” ||s.matches”-{0,1}[0-9]+.”
4.除了正则表达式,还有另外一种简单的,不需要考虑像3.这样的情况,那就是try-catch。具体就是将不符合题目要求的输出。不符合的有三种情况。1:是double,但是不是3位。2:是double,是2位,但是不再【-1000,1000】 区间。3:不是double。请注意:怎么判断是不是几位呢?很简单那,只需要判断小数点的位置嘛。(1)没有小数点,用indexof结果就是-1.(2)有小数点,用字符串长度-小数点的下标>3.这个判断超级机智呢!
5.鉴于3和4,我建议用4。
6.正则表达式代码如下
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
double sum = 0;
int k = 0;
String s = "";
for (int i = 0; i < n; i++) {
s = sc.next();
if(s.matches("-{0,1}[0-9]+.[0-9]{1,2}") || s.matches("-{0,1}[0-9]+") ||s.matches("-{0,1}[0-9]+.")){
double d = Double.valueOf(s);
//System.out.println(d);
if(d>=-1000 && d<=1000){
sum += Double.valueOf(s);
k++;
}
else{
System.out.println("ERROR: "+s+" is not a legal number");
}
}
else{
System.out.println("ERROR: "+s+" is not a legal number");
}
}
if(k==0){
System.out.println("The average of 0 numbers is Undefined");
}
else if(k>1){
BigDecimal b = new BigDecimal(sum/(k*1.0));
b = b.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println("The average of "+k+" numbers is "+b);
}
else{
BigDecimal b = new BigDecimal(sum/(k*1.0));
b = b.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println("The average of 1 number is "+b);
}
sc.close();
}
}
7.try-catch代码如下:
import java.math.BigDecimal;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
double sum = 0;
int k = 0;
String s = "";
for (int i = 0; i < n; i++) {
try {
s = sc.next();
double num = Double.parseDouble(s);
int pos = s.indexOf(".");
if ((num >= -1000 && num <= 1000) && pos == -1) {
sum += num;
k++;
} else if (s.length() - pos > 3 || num < -1000 || num > 1000) {
System.out.println("ERROR: " + s + " is not a legal number");
} else {
sum += num;
k++;
}
} catch (Exception e) {
System.out.println("ERROR: " + s + " is not a legal number");
}
}
if(k==0){
System.out.println("The average of 0 numbers is Undefined");
}
else if(k==1){
BigDecimal b = new BigDecimal(sum/(k*1.0));
b = b.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println("The average of 1 number is "+b);
}
else {
BigDecimal b = new BigDecimal(sum/(k*1.0));
b = b.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println("The average of "+k+" numbers is "+b);
}
sc.close();
}
}