zoukankan      html  css  js  c++  java
  • 1054. 求平均值 (20)

    1054. 求平均值 (20)

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

    输入格式:

    输入第一行给出正整数N(<=100)。随后一行给出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 #include<iostream>
     2 #include<cstdlib>
     3 #include<cstdio>
     4 #include<cstring>
     5 using namespace std;
     6 int main(){
     7     char *num[105];
     8     int N,count=0;
     9     double average=0;
    10     cin>>N;
    11     for(int i=0;i<N;i++){
    12         bool isNumber=true;     //判断是否为合法数字
    13         int point=0;            //监视小数点的个数
    14         int point_=0;           //监视小数点之后的位数
    15         num[i] = (char *)malloc(10*sizeof(char)); //申请空间
    16         cin>>num[i];
    17         int len=strlen(num[i]);
    18         double temp=atof(num[i]);//将字符串转换成数字,具体用法自行百度
    19         if(temp<-1000||temp>1000)   isNumber=false;
    20         for(int j=0;j<len;j++){  //对每一个元素的各个字符进行判断
    21             if(point==1) point_++;  
    22             if(num[i][j]=='-'&&j!=0){//如果含有-,则-只有一个且在第一位
    23                 isNumber=false;
    24                 break;
    25             }
    26             if(num[i][j]!='.'){     
    27                 if((num[i][j]<'0'||num[i][j]>'9')&&(num[i][j]!='-')){
    28                     isNumber=false;
    29                     break;
    30                 }       
    31             }else {             //如果是小数点
    32                 point++;
    33             }
    34             if(point_>2||point>1){//如果小数点大于一个或者小数位数多余2
    35                 isNumber=false;
    36                 break;
    37             }
    38         }
    39         if(!isNumber)
    40             cout<<"ERROR: "<<num[i]<<" is not a legal number"<<endl;
    41         else {
    42             count++;
    43             average+=temp;
    44         }
    45     }
    46     if(count==1)
    47         printf("The average of 1 number is %.2lf",average);
    48     else if(count==0)
    49         printf("The average of 0 numbers is Undefined");
    50     else
    51         printf("The average of %d numbers is %.2lf",count,average/count);
    52     return 0;
    53 }
  • 相关阅读:
    Censored! POJ
    POJ
    HDU
    ionic中的生命周期函数
    ionic项目相关的操作命令
    数组的join()函数操作
    pop()实现逐个删除数组最后一位并输出
    CSS制作彩虹效果
    ionic2 页面加载时图片添加的问题
    升级ionic版本后,创建新项目报Error Initializing app错误解决
  • 原文地址:https://www.cnblogs.com/cqxhl/p/6183883.html
Copyright © 2011-2022 走看看