pat 甲級 1108. Finding Average
這個必須寫, 想了我好多天,哼
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();
}
}