1108 Finding Average (20 分)

版权声明:如有错误,请指出,不胜感激。 https://blog.csdn.net/qq_36424540/article/details/84892563

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

真没注意那个单数的时候,以为是一句废话,没看见单复数。

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)

const double eps=1e-5;

const int N=110;
char str[N][N];

double get(int x,int pos){
	int len=x-pos;
	double res=1;
	for(int i=1;i<=len;i++)res*=0.1;
	return res;
}

/*
3
-0.1 -2.3- 2.01

1
-999.99
*/
int ok(int x,double& res){
	res=0;
	int len=strlen(str[x]);

	int num_pot=0,num_fu=0;
	rep(i,0,len){
		if(str[x][i]=='.')num_pot++;
		if(str[x][i]=='-')num_fu++;
		if(!(str[x][i]>='0'&&str[x][i]<='9'||str[x][i]=='.'||str[x][i]=='-'))return 0;
	}

	if(num_pot>1||num_fu>1)return 0;

	if(num_fu==1&&str[x][0]!='-')return 0;

	if(num_pot){
		int pos=0;
		for(int i=0;i<len;i++){
			if(str[x][i]=='.'){
				pos=i;break;
			}
		}
		//printf("len:%d pos:%d\n",len,pos);
		if(len-pos>3) return 0;

		int i=0;
		if(str[x][0]=='-')i++;
		for(;i<pos;i++){
			res=res*10.0+str[x][i]-'0';
		}
	
		double res2=0;
		for(i=pos+1;i<len;i++){
			res2+=get(i,pos)*(str[x][i]-'0');
		}
		res+=res2;
		if(str[x][0]=='-')res*=-1;
	}else{
		int i=0;
		if(str[x][0]=='-')i++;
		for(;i<len;i++){
			res=res*10+str[x][i]-'0';
		}
		if(str[x][0]=='-')res*=-1;
	}
	return 1;
}

int main(){
	int n;
	scanf("%d",&n);
	rep(i,0,n)scanf(" %s",str[i]);

	int cnt=0;double sum=0;
	int p=0;
	rep(i,0,n){
		double res=-2000;
		if(ok(i,res)&&res<=1000&&res>=-1000){
			//printf("i:%d %.2f\n",i,res);
			p=i;
			cnt++;
			sum+=res;
		}else{
			printf("ERROR: %s is not a legal number\n",str[i]);
		}
	}

//	printf("sum:%.2f cnt:%d\n",sum,cnt);
	if(cnt==0)printf("The average of 0 numbers is Undefined\n");
	else if(cnt==1)printf("The average of 1 number is %.2f\n",sum);
	else printf("The average of %d numbers is %.2f\n",cnt,sum/cnt);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/84892563
今日推荐