zoukankan      html  css  js  c++  java
  • PAT 1108 Finding Average [难]

    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

     题目大意:给出n个数,可能不是合法的,给出那些合法的数的平均值。

    //我的天哪,这个题也太难了吧,难死我了,改了好多遍我为什么就是写不对?越改通过的测试点越少。。绝望。

    #include <iostream>
    #include <algorithm>
    #include<cstdio>
    #include<stdio.h>
    #include <queue>
    #include<cmath>
    #include <vector>
    #include<set>
    using namespace std;
    
    //float toNum(string s){
    //    //有小数怎么办?有点绝望,好像没有做过这样的题目。
    //    int pos=s.find(".");
    //    float num=0;
    //    for(int i=0;i<pos;i++){
    //        num=num*10+(s[i]-'0');
    //        n=atof()
    //    }
    //
    //}
    
    int main()
    {
        int n;
        cin>>n;
        string s;
        vector<float> legal;
        vector<string> nolegal;
        for(int i=0; i<n; i++)
        {
            cin>>s;
            bool flag=true;
            for(int j=0; j<s.size(); j++) //判断是否是字母
            {
                if(!(s[j]>='0'||s[j]<='9')||s[j]!='.'||!s[j]!='-')//只能是这几种。
                {
                    flag=false;
                    break;//比如2..0这样的算是合法数字吗?这就很尴尬,我感觉不是。
                }
                if((s[j]=='-'&&j)|(s[j]=='-'&&j==0&&s.size()==1)){//加上这个,并没有通过1和4测试点。
                    flag=false;break;
                }
            }
            if(s.find(".")!=string::npos)
            {
                int pos=s.find(".");
                if(s.size()-1>3||pos==0)//如果小数点出现在第一个也是不合法的,加上这个之后还是没通过14测试点。
                {
                    flag=false;
                }
                if(s.find(".",pos+1)!=string::npos)
                {
                    flag=false;
                }
            }
            float numf;
            if(flag)
            {
                numf=atof(s.c_str());
                if(numf>1000.0||numf<-1000.0)
                {
                    nolegal.push_back(s);
                }
                else
                {
                    legal.push_back(numf);
                }
            }
            else
            {
                nolegal.push_back(s);
            }
    
        }
        for(int i=0; i<nolegal.size(); i++)
        {
            printf("ERROR: %s is not a legal number
    ",nolegal[i].c_str());
        }
        if(legal.size()==0)
        {
            printf("The average of 0 numbers is Undefined");
        }
        else if(legal.size()==1)
        {
            printf("The average of 1 number is %.2f",legal[0]);
        }
        else
        {
            float sum=0;
            int size=legal.size();
            for(int i=0; i<size; i++)
                sum+=legal[i];
            printf("The average of %d number is %.2f",size,sum/size);
        }
        return 0;
    }
    //1,没有判断-是否在首位,如:1-6就视为合法了。。
    //2.只有一个.时,是不合法的。。
    //3.测试了-,发现自己的代码认为是合法的。。。绝望,好复杂。。。
    //4.发现了要给十分严重的问题,就是如果它的非法输入不一定非得是字母。。。

     //我也不知道这是为什么通不过。。。哭辽。

    学习了柳神的代码:

    参考:https://www.cnblogs.com/lanjianhappy/p/6861728.html

     sscanf(输入的字符串,格式,输出到);

    scanf是从键盘输入,而sscanf读入的是第一个参数,是个字符串。

    发现这里第三个参数根本就没有类型限制!!!

    Int sscanf( string str, string fmt, mixed var1, mixed var2 ... );
      int scanf( const char *format [,argument]... );
    char buf[512] = ;
      sscanf("123456 ", "%s", buf);
      printf("%s/n", buf);
      结果为:123456
    sscanf("123456 ", "%4s", buf);
      printf("%s/n", buf);
      结果为:1234
    sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf);
      printf("%s/n", buf);
      结果为:123456abcdedf

     就是类似于正则表达式!

    sprinf()学习:http://www.runoob.com/cprogramming/c-function-sprintf.html

    C 库函数 int sprintf(char *str, const char *format, ...) 发送格式化输出到 str 所指向的字符串。

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
       char str[80];
    
       sprintf(str, "Pi 的值 = %f", M_PI);
       puts(str);
       
       return(0);
    }
    
    Pi 的值 = 3.141593

    应该是从后往前看,先将第三个参数第二个参数的格式赋值,然后再整体赋值给第一个参数。

    当然第一个参数和上一个函数一样都是字符串。

    int main()
    {
        string s;
        char a[50];
        float temp;
        cin>>s;
        sscanf(s.c_str(),"%f",&temp);
        sprintf(a,"%.2f",temp);
        cout<<s<<'
    ';
        cout<<a<<'
    ';
        cout<<temp;
        return 0;
    }

    学习了!

    柳神的代码真的是太厉害了。

    多复习!

  • 相关阅读:
    【模板】Sparse-Table
    UVa 11235 Frequent values
    【模板】树状数组
    UVa 1428 Ping pong
    数学技巧
    UVa 11300 Spreading the Wealth
    UVa 11729 Commando War
    UVa 11292 Dragon of Loowater
    POJ 3627 Bookshelf
    POJ 1056 IMMEDIATE DECODABILITY
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9947535.html
Copyright © 2011-2022 走看看