其他分享
首页 > 其他分享> > PAT (Advanced Level) Practice 1108 Finding Average (20 分) 凌宸1642

PAT (Advanced Level) Practice 1108 Finding Average (20 分) 凌宸1642

作者:互联网

PAT (Advanced Level) Practice 1108 Finding Average (20 分) 凌宸1642

题目描述:

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.

译:基本任务很简单:给定 N 个实数,你应该计算它们的平均值。 但让事情变得复杂的是,某些输入数字可能不合法。 合法输入是 [−1000,1000] 中的实数,精确到不超过 2 位小数。 在计算平均值时,不得将那些非法数字计算在内。。


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..

译:每个输入文件包含一个测试用例。 对于每种情况,第一行给出一个正整数 N (≤100)。 然后在下一行给出 N 个数字,用一个空格分隔。


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.

译:对于每个非法输入的数字,打印一行 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


Sample Input1 (样例输入1):

7
5 -3.2 aaa 9999 2.3.4 7.123 2.35

Sample Output1 (样例输出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 Input2 (样例输入2):

2
aaa -9999

Sample Output2 (样例输出2):

ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined

The Idea:

The Codes:

#include<bits/stdc++.h>
using namespace std ;
string s ;
int n , ans ;
double sum , temp ;
bool deal(string s){
	int pos = 0 , ind = -1 ;
	for(int i = 0 ; i < s.size() ; i ++){
		if(s[i] == '-') continue ;
		else if(s[i] == '.')  ind = i , pos ++ ;
		else if(s[i] < '0' || s[i] > '9') return false ;
	}
	if(pos > 1) return false ; // 小数点个数多余 1 个  
	if(ind == 0) return false ; // 开头就是小数点 
	if(ind != -1 && s.size() - ind > 3) return false ;
	else {
		sscanf(s.c_str() , "%lf" , &temp) ;
		if(temp < -1000 || temp > 1000) return false ;  // 不在 [-1000,1000]之内 
		sum += temp ;
		ans ++ ;
		return true ;
	}
}
int main(){
	cin >> n ;
	for(int i = 0 ; i < n ; i ++){
       	cin >> s ;
		if(!deal(s)) printf("ERROR: %s is not a legal number\n" , s.c_str()) ;
	}
    if(ans == 0) printf("The average of 0 numbers is Undefined\n") ;
    else if(ans == 1) printf("The average of 1 number is %.2f\n" , sum) ;
	else printf("The average of %d numbers is %.2f\n" , ans , sum / ans) ;
	return 0 ;
} 

标签:PAT,Level,average,number,legal,小数点,numbers,ERROR,Average
来源: https://www.cnblogs.com/lingchen1642/p/15139001.html