• 求平均值


    题目:

    给定 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


    这题真是坑啊!!!一个输出调了一晚上,自闭了
    ╮(︶﹏︶)╭。

    这题主要就是运用 sscanf 和sprintf 两个函数,前者将字符串转换为指定类型的数值,后者将某一个数转换为指定类型后变成字符串“输出”到字符数组。
     1 #include <map>
     2 #include <stack>
     3 #include <queue>
     4 #include <cmath>
     5 #include <string>
     6 #include <limits>
     7 #include <cstdio>
     8 #include <cstdlib>
     9 #include <cstring>
    10 #include <iostream>
    11 #include <algorithm>
    12 #define Scc(c) scanf("%c",&c)
    13 #define Scs(s) scanf("%s",s)
    14 #define Sci(x) scanf("%d",&x)
    15 #define Sci2(x, y) scanf("%d%d",&x,&y)
    16 #define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z)
    17 #define Scl(x) scanf("%I64d",&x)
    18 #define Scl2(x, y) scanf("%I64d%I64d",&x,&y)
    19 #define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z)
    20 #define Pri(x) printf("%d
    ",x)
    21 #define Prl(x) printf("%I64d
    ",x)
    22 #define Prc(c) printf("%c
    ",c)
    23 #define Prs(s) printf("%s
    ",s)
    24 #define For(i,x,y) for(int i=x;i<y;i++)
    25 #define For_(i,x,y) for(int i=x;i<=y;i++)
    26 #define FFor(i,x,y) for(int i=x;i>y;i--)
    27 #define FFor_(i,x,y) for(int i=x;i>=y;i--)
    28 #define Mem(f, x) memset(f,x,sizeof(f))
    29 #define LL long long
    30 #define ULL unsigned long long
    31 #define MAXSIZE 105
    32 #define INF 0x3f3f3f3f
    33 const int mod=1e9+7;
    34 const double PI = acos(-1.0);
    35 
    36 using namespace std;
    37 
    38 int main()
    39 {
    40     int n;
    41     Sci(n);
    42     char s[MAXSIZE][MAXSIZE];
    43     int cnt=0;
    44     double sum=0;
    45     For_(h,1,n)
    46     Scs(s[h]);
    47     For_(j,1,n)
    48     {
    49         char ss[MAXSIZE];
    50         double tmp;
    51         sscanf(s[j],"%lf",&tmp);
    52         sprintf(ss,"%.2lf",tmp);
    53         int flag=1;
    54         int len=strlen(s[j]);
    55         For(i,0,len)
    56         if(s[j][i]!=ss[i])
    57         {
    58             flag=0;
    59             break;
    60         }
    61         if(!flag||tmp<-1000||tmp>1000)
    62             printf("ERROR: %s is not a legal number
    ",s[j]);
    63         else
    64         {
    65             cnt++;
    66             sum+=tmp;
    67         }
    68     }
    69     if(cnt==0)
    70         Prs("The average of 0 numbers is Undefined");//注意此时number后加s,测试点1
    71     else if(cnt==1)
    72         printf("The average of 1 number is %.2lf
    ",sum);//没有s
    73     else
    74         printf("The average of %d numbers is %.2lf
    ",cnt,sum/cnt);//有s,测试点2
    75     return 0;
    76 }
    View Code
     
  • 相关阅读:
    js获取当前日期
    Mysql错误1452
    数字输入框禁止输入字母
    laravel关联外键报错
    golang for range channel
    golang实现简单哈希表(拉链法解决冲突)
    K个一组反转链表(golang)
    golang 少见的语法问题(无用)
    golang实现循环队列
    数组中连续序列和最大值(循环数组)
  • 原文地址:https://www.cnblogs.com/hbhdhd/p/11379884.html
Copyright © 2020-2023  润新知