PAT (Advanced Level) Practice — 1108 Finding Average (20 分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37345402/article/details/87906531

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805360777347072

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

:题意 长度为1时,如果它不是数字,则不合法。如果字符串中有除了0-9、‘-’、‘.’之外的字符,则不合法。

‘-’只能有一个,且不能位于开头,‘.’只能有一个,wa的点在于数据中的小数点是可以放在开头的。

判断结束后,将字符转化为数字,如果有小数点,分小数点前后计算得到数字。

如果数字范围不在-1000~1000,则不合法,最后输出其平均值,注意只有1个数字合法时,输出的为‘number’

for(int i=0;i<pos;i++){
			if(a[i]=='-'){
				continue;
			}else{
				ans1=ans1*10+(a[i]-'0');
			}
		}
		for(int i=len-1;i>pos;i--){
			ans2=(ans2+a[i]-'0')/10.0;
		}
		double ans=ans1+ans2;
}
#include<iostream>
#include<cstring>
using namespace std;
int main(){
	char a[1111];
	double sum=0.0;
	double cnt=0;
	int n;
	cin>>n;
	while(n--){
		cin>>a;
		int len=strlen(a); 
		if(len==1){
			if(a[0]<'0'||a[0]>'9'){
				printf("ERROR: %s is not a legal number\n",a); 
				continue;
			}
		}
		int f=0;
		for(int i=0;i<len;i++){
			if(a[i]!='.'&&a[i]!='-'&&(a[i]<'0'||a[i]>'9')){
				f++;
			}
		}
		if(f>0){
			printf("ERROR: %s is not a legal number\n",a);
			continue;
		} 
		int cnt1=0,cnt2=0,pos=-1;
		for(int i=0;i<len;i++){
			if(a[i]=='-'){
				cnt1++;
			}
			if(a[i]=='.'){
				cnt2++;
				pos=i;
			}
		}
		if(cnt1>=2){
			printf("ERROR: %s is not a legal number\n",a);
			continue;
		}else if(cnt1==1){
			if(a[0]!='-'){
				printf("ERROR: %s is not a legal number\n",a);
				continue;
			}
		}
		if(cnt2>=2){
			printf("ERROR: %s is not a legal number\n",a);
			continue;
		}
		else if(cnt2==1){
			if(pos<=len-4){
				printf("ERROR: %s is not a legal number\n",a);
		    	continue;
			}	
		}
		double ans1=0,ans2=0;
		if(cnt2==0){
			pos=len;
		}
		for(int i=0;i<pos;i++){
			if(a[i]=='-'){
				continue;
			}else{
				ans1=ans1*10+(a[i]-'0');
			}
		}
		for(int i=len-1;i>pos;i--){
			ans2=(ans2+a[i]-'0')/10.0;
		}
		double ans=ans1+ans2;
		if(a[0]=='-'){
			ans=-ans;
		}
		if(ans>1000||ans<-1000){
			printf("ERROR: %s is not a legal number\n",a);
			continue;
		}else{
			cnt++;
			sum+=ans;
		}
	}
	if(cnt==0){
		printf("The average of 0 numbers is Undefined\n");
	}else if(cnt==1){
		printf("The average of 1 number is %.2lf\n",sum);
	}else{
		sum/=cnt;
		printf("The average of %.0lf numbers is %.2lf\n",cnt,sum);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37345402/article/details/87906531
今日推荐