• 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;
    }

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

  • 相关阅读:
    ps cs5 gif 动画 分解
    api 生成方法
    AVAYA 交换机
    jQuery操作input值
    CMM3 软件升级
    ehcache 使用 缓存:健值,页面,Hibernate,监控
    网络封包分析软件Wireshark
    Hacking Windows 7 SP 1 Using Java Signed Applet Social Engineering Code Execution
    以太网历史
    转:web应用开发的发展方向
  • 原文地址:https://www.cnblogs.com/kamisamalz/p/13403506.html
Copyright © 2020-2023  润新知