zoukankan      html  css  js  c++  java
  • PAT乙级1054-----求平均值 (20分)

    1054 求平均值 (20分)

    本题的基本要求非常简单:给定 N 个实数,计算它们的平均值。但复杂的是有些输入数据可能是非法的。一个“合法”的输入是 [−] 区间内的实数,并且最多精确到小数点后 2 位。当你计算平均值的时候,不能把那些非法的数据算在内。

    输入格式:

    输入第一行给出正整数 N(≤)。随后一行给出 N 个实数,数字间以一个空格分隔。

    输出格式:

    对每个非法输入,在一行中输出 ERROR: X is not a legal number,其中 X 是输入。最后在一行中输出结果:The average of K numbers is Y,其中 K 是合法输入的个数,Y 是它们的平均值,精确到小数点后 2 位。如果平均值无法计算,则用 Undefined 替换 Y。如果 K 为 1,则输出 The average of 1 number is Y

    输入样例 1:

    7
    5 -3.2 aaa 9999 2.3.4 7.123 2.35
    
     

    输出样例 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
    
     

    输入样例 2:

    2
    aaa -9999
    
     

    输出样例 2:

    ERROR: aaa is not a legal number
    ERROR: -9999 is not a legal number
    The average of 0 numbers is Undefined

    思路:
    1.第一个字符要么是数字要么是负号
    2.不能有字母(即字符范围是'0'----'9'以及'.'和'-')
    3.小数点不能有两个,且小数点可以在首位也可以在末尾
    4.小数点后最多2位数字
    5.数字范围是[-1000,1000]

    特别要注意:
    测试点2的 1 number 不要写成 1 numbers 不能有s 巨坑

    首次通过代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 int main(){
     5     int sum;
     6     char a[1000];
     7 //    int dot=0;
     8 //    int p_num=1;
     9 //    int dec=0;//小数点
    10 //    int after_dot=0;//小数点后有几位
    11     float answer=0;
    12     int num_sum=0;
    13     scanf("%d",&sum);
    14     for(int i=0;i<sum;i++){
    15      scanf("%s",a);
    16      int b=10;//小数点后几位权重 
    17      float num=0;//当前的数字 
    18      int flag=1;//1表示是数字,0表示非法 
    19      int p_num=1;//1表示正数,0表示负数 
    20      int dec=0;//是否有小数点
    21      int after_dot=0;//小数点后有几位
    22      for(int j=0;j<strlen(a);j++){
    23          if(a[j]=='-'&&j==0) p_num=0;
    24          else if(a[j]=='.'){
    25              if(dec==1) {
    26              printf("ERROR: %s is not a legal number
    ",a);
    27              flag=0;
    28              break;
    29              }
    30              dec=1;
    31          }
    32          else if(a[j]>='0'&&a[j]<='9') {
    33              //若在小数点前
    34            if(!dec){
    35              num=num*10+a[j]-'0';
    36            }
    37            else {
    38              after_dot++;
    39              //小数点后数字大于2个
    40              if(after_dot>2) {
    41                printf("ERROR: %s is not a legal number
    ",a);
    42                flag=0;
    43                break;
    44              }
    45              num=num+(float)(a[j]-'0')/b;
    46              b*=10;
    47            }
    48            //数字大于1000
    49            if(num>1000){
    50              printf("ERROR: %s is not a legal number
    ",a);
    51              flag=0;
    52              break;
    53            } 
    54          }
    55          else {
    56               printf("ERROR: %s is not a legal number
    ",a);
    57               flag=0;
    58               break;
    59          }
    60      }
    61      if(flag){
    62      if(p_num) {
    63          answer+=num;
    64      }
    65      else {
    66          answer-=num;
    67      }
    68      num_sum++;
    69      }
    70      
    71     }
    72     
    73     if(num_sum==0){
    74         printf("The average of 0 numbers is Undefined");
    75     }
    76     else if(num_sum>1){
    77         printf("The average of %d numbers is %.2f",num_sum,answer/num_sum);
    78     }
    79     else {
    80         printf("The average of 1 number is %.2f",answer/num_sum);
    81     }
    82     return 0;
    83 }
    View Code

    参考:

    FROM:https://blog.csdn.net/qq_38263123/article/details/82056788



  • 相关阅读:
    Sprint第二个冲刺(第八天)
    Sprint第二个冲刺(第七天)
    Sprint第二个冲刺(第六天)
    Sprint第二个冲刺(第五天)
    Sprint第二个冲刺(第四天)
    Sprint第二个冲刺(第三天)
    Sprint第二个冲刺(第二天)
    Sprint第二个冲刺(第一天)
    新冲刺Sprint3(第三天)
    新冲刺Sprint3(第二天)
  • 原文地址:https://www.cnblogs.com/a982961222/p/12368508.html
Copyright © 2011-2022 走看看