zoukankan      html  css  js  c++  java
  • 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 [−] 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 (≤). 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

    AC【找平均数】三部曲:1、[ 判断合法 ]    2、[ 记录合法 ]   3、[ 分类输出 ]

     1 #include<bits/stdc++.h>
     2 using namespace std; //legal  [-1000,1000]  2 decimal 
     3 
     4 bool isLegal(string &s)
     5 {
     6     for(int i = 0; i < s.length(); i++) 
     7     {
     8         if(s[i] >= 'A' && s[i] <= 'z') return false;
     9         if(s[i] == '.' && (s.length() - i - 1) > 2) return false;
    10     }
    11     double d = stod(s); 
    12     if(d > 1000 || d < -1000) return false;
    13     return true;
    14 }
    15 
    16 int main()
    17 {// illegal input number, print in a line ERROR: X is not a legal number
    18     int n, cnt = 0; cin >> n;
    19     string s; double total = 0.0;
    20     
    21     for(int i = 0; i < n; i++)
    22     {
    23         cin >> s;
    24         if(isLegal(s))
    25         {
    26             cnt++; double d = stod(s);
    27             total += d;
    28         }
    29         else cout << "ERROR: " << s << " is not a legal number" << endl;
    30     }
    31     
    32     if(cnt == 0) cout << "The average of 0 numbers is Undefined";
    33     else if(cnt == 1) printf("The average of 1 number is %.2lf", total);
    34     else printf("The average of %d numbers is %.2lf", cnt, total/cnt);
    35  
    36     return 0;
    37 }
    View Code

    Tips :  

    int num = stoi(s);       // string 转 int
    double num = stod(s);      // string 转 float
    float num = stof(s);        // string 转 double
    string s = to_string(num);     //  int 转 string

    bool isLegal(string &s)    //判断 string 是否合法(符合题意)
    {
      for(int i = 0; i < s.length(); i++)
      {
        if(s[i] >= 'A' && s[i] <= 'z') return false;
        if(s[i] == '.' && (s.length() - i - 1) > 2) return false;
      }
      double d = stod(s);
      if(d > 1000 || d < -1000) return false;
      return true;
    }

     "只要你不停止,走慢一点没关系"

  • 相关阅读:
    streamreader
    python编码问题总结
    linux rz上传文件及出错解决方案
    使用linux远程登录另一台linux
    利用Django实现RESTful API(一)
    windows下搭建vue开发环境
    windows下python虚拟环境virtualenv安装和使用
    SonarQube的安装、配置与使用
    windows下安装Mysql(图文详解)
    静态代码分析工具sonarqube+sonar-runner的安装配置及使用
  • 原文地址:https://www.cnblogs.com/kamisamalz/p/13403506.html
Copyright © 2011-2022 走看看