solution Of Pat1108. Finding Average (20)

来源:互联网 发布:golang sleep用法 编辑:程序博客网 时间:2024/04/29 11:15

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.

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’,’.’和’-‘;
要求2:’.’只能有一个;
要求3:小数位最多只能有两位;
要求4:对应实数值[-1000,1000]。

程序步骤:
第一步我们要判断字符串的合法性(1,2,3);
第二步计算对应的实数值;
第三步判断是否满足范围要求。

具体程序(AC)如下:

#include<iostream>#include<vector>#include<cmath>using namespace std;bool illegal(string a,vector<int>& b,vector<int>& s)//串的合法性校验{    b.clear();    s.clear();    bool flag=true;    for(int i=0;i<a.length();++i)    {        if(a[i]<='9'&&a[i]>='0')//将数字放入数组        {            if(flag)                b.push_back(a[i]-'0');            else            {                if(s.size()==2)//保证小数位位数至多为2                    return false;                s.push_back(a[i]-'0');            }        }        else if(a[i]=='.')//dot只能出现一次        {            if(flag)                flag=false;            else                return false;        }        else if(a[i]=='-');//对减号不做处理        else            return false;    }    return true;}int vector2Value(vector<int>& v){    if(v.size()==0)        return 0;    int all=0;    for(int i=0;i<v.size();++i)        all=all*10+v[i];    return all;}int main(){    int n;    int realCount=0;    double sum=0.0;    double cur=0.0;    vector<int> bigger;    vector<int> smaller;    cin>>n;    string value;    for(int i=0;i<n;++i)    {           cin>>value;        if(illegal(value,bigger,smaller))        {            cur=0.0;            cur+=vector2Value(bigger);            cur+=(double)vector2Value(smaller)/pow(10,smaller.size());            if(cur<=1000.0&&cur>=0.0)            {                realCount++;                if(value[0]=='-')                               cur=-cur;                sum+=cur;            }            else                  cout<<"ERROR: "<<value<< " is not a legal number"<<endl;        }        else        {            cout<<"ERROR: "<<value<< " is not a legal number"<<endl;        }    }    if(realCount>1)        printf("The average of %d numbers is %.2lf\n",realCount,sum/realCount);    else if(realCount==1)//根据题意对只有一个合法实数值的        printf("The average of 1 number is %.2lf\n",sum);    else        cout<<"The average of 0 numbers is Undefined"<<endl;    return 0;}
0 0