本文主要是介绍【PAT】1108. Finding Average (20)【模拟】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目描述
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] 以内的真数,并且小数部分不超过两位。当你计算平均值时,那些非法数字不能被计入。
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为他们的平均值,保留两位小数。万一平均值无法计算,输出 output Undefined 来代替Y。万一K是1,则输出The average of 1 number is Y 来替代。
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个时的情况。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int N;
string a[110];
int v[110];
double judge(string s){int symbol=0,decimal=0,flag=0;double val=0,dec=1;int length=s.size();for(int i=0;i<length;i++){if(!(s[i]>='0'&&s[i]<='9')&&!(s[i]=='-'||s[i]=='.')) {flag=1;break;} else if(s[i]=='-'){if(i==0)symbol=1;else{flag=1;break;} }else if(s[i]=='.'){decimal++;if(decimal>1){flag=1;break;} }else{int temp=s[i]-'0';if(decimal){val+=(temp/pow(10,dec));dec++;if(dec>3){flag=1;break;} }else{val=val*10+temp;}}}if(flag==1||val>1000)return 1001;else if(symbol==1) return -val;else return val;}
int main(){cin>>N;double sum=0;int ccount=0;for(int i=0;i<N;i++){cin>>a[i];double temp=judge(a[i]);if(temp==1001)cout<<"ERROR: "<<a[i]<<" is not a legal number"<<endl;else sum+=temp,ccount++;}if(ccount>1) printf("The average of %d numbers is %.2lf\n",ccount,sum/ccount);else if(ccount==1)printf("The average of 1 number is %.2lf\n",sum);else printf("The average of 0 numbers is Undefined\n");return 0;
}
这篇关于【PAT】1108. Finding Average (20)【模拟】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!