• 1108 Finding Average (PAT甲级)


    1. #include
    2. #include
    3. #include
    4. #include
    5. bool judge(std::string str){
    6. int cnt = 0;
    7. std::string s = ".";
    8. for(int i = 0; i < str.size(); ++i){
    9. if((str[i] < '0' || str[i] > '9') && str[i] != '.' && str[i] != '-'){
    10. return false;
    11. }
    12. if(str[i] == '.'){
    13. ++cnt;
    14. }
    15. }
    16. if(cnt > 1){
    17. return false;
    18. }
    19. int pos = str.find(s);
    20. if(pos != -1 && str.size() - pos > 3){
    21. return false;
    22. }
    23. if(std::stod(str) < -1000 || std::stod(str) > 1000){
    24. return false;
    25. }
    26. return true;
    27. }
    28. int main(){
    29. int N, cnt;
    30. double sum;
    31. std::string str;
    32. scanf("%d", &N);
    33. sum = 0;
    34. cnt = 0;
    35. for(int i = 0; i < N; ++i){
    36. std::cin >> str;
    37. if(!judge(str)){
    38. printf("ERROR: %s is not a legal number\n", str.c_str());
    39. } else{
    40. ++cnt;
    41. sum += std::stod(str);
    42. }
    43. }
    44. if(cnt == 0){
    45. printf("The average of %d numbers is Undefined", cnt);
    46. } else if(cnt == 1){
    47. printf("The average of 1 number is %.2f", sum);
    48. } else{
    49. printf("The average of %d numbers is %.2f", cnt, sum / cnt);
    50. }
    51. return 0;
    52. }

    参考柳婼解法:

    1. #include
    2. #include
    3. #include
    4. #include
    5. bool judge(std::string str){
    6. double a;
    7. char ss[50];
    8. sscanf(str.c_str(), "%lf", &a);
    9. sprintf(ss, "%.2f", a);
    10. for(int i = 0; i < str.size(); ++i){
    11. if(ss[i] != str[i]){
    12. return false;
    13. }
    14. }
    15. if(std::stod(str) < -1000 || std::stod(str) > 1000){
    16. return false;
    17. }
    18. return true;
    19. }
    20. int main(){
    21. int N, cnt;
    22. double sum;
    23. std::string str;
    24. scanf("%d", &N);
    25. sum = 0;
    26. cnt = 0;
    27. for(int i = 0; i < N; ++i){
    28. std::cin >> str;
    29. if(!judge(str)){
    30. printf("ERROR: %s is not a legal number\n", str.c_str());
    31. } else{
    32. ++cnt;
    33. sum += std::stod(str);
    34. }
    35. }
    36. if(cnt == 0){
    37. printf("The average of %d numbers is Undefined", cnt);
    38. } else if(cnt == 1){
    39. printf("The average of 1 number is %.2f", sum);
    40. } else{
    41. printf("The average of %d numbers is %.2f", cnt, sum / cnt);
    42. }
    43. return 0;
    44. }

    题目如下:

    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:

    1. 7
    2. 5 -3.2 aaa 9999 2.3.4 7.123 2.35

    Sample Output 1:

    1. ERROR: aaa is not a legal number
    2. ERROR: 9999 is not a legal number
    3. ERROR: 2.3.4 is not a legal number
    4. ERROR: 7.123 is not a legal number
    5. The average of 3 numbers is 1.38

    Sample Input 2:

    1. 2
    2. aaa -9999

    Sample Output 2:

    1. ERROR: aaa is not a legal number
    2. ERROR: -9999 is not a legal number
    3. The average of 0 numbers is Undefined
  • 相关阅读:
    yolov8 ValueError和RuntimeError
    第十三章总结
    leetcode 133. 克隆图
    Wireshark-win32-1.8.4 给winxp和win2003用
    在开发人力资源管理 HRM 系统?试试低代码
    C语言之位域(位段)入门详解
    layUI带搜索的选择框样式和官网显示不一致
    Docker常用命令
    岭回归与LASSO回归:解析两大经典线性回归方法
    Golang原理分析:切片(slice)原理及扩容机制
  • 原文地址:https://blog.csdn.net/linh2006/article/details/130903253