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 [−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:

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

题意理解

基本要求很简单,求平均数。关键要用合适的方法判断输入是否合法。合法的要求是范围在[-1000, 1000]内的小数点后不超过两位的实数。

解题思路

这类题逻辑性不强,没有处理思路的时候,建议直接参考其他人的代码,从中学习方便处理输入判断的函数和方法。参考柳婼代码,用到了sscanf. 和 sprintf 来处理。只需明白sscanf可以将字符串中的内容按照指定格式分拆出来存储在变量变量内,注意变量前加&符号,ssprintf可以向在标准输出一样将输出存储在指定的字符串里。
所以为了判断字符串s是否合法,可以先将s中的最前面的double类型的数择出暂存在temp变量中,然后再把temp按照保留小数点两位的数字的格式暂存在num字符串中。
这时num字符串里是期待的合法的标准格式,若s合法,则s中的每一个字符都和num中的前几位字符相同。若s中有和标准的格式中不同的地方,那么s必然不合法。可能的情况包括s为字母,s多了小数点,s小数点还有超过两位等。例如aaa, 这时sscanf不会把aaa存入到temp中, temp中的值为空或者为上一个暂存double数,所以spring也不会改变num的值,这时num肯定和s不一致,判定s为非法。再例如,若s为7.123, 那么temp为7.12,num为7.12,比较s的所有位和num中的前5位,s 中第五位‘3’ 和num中第五位‘\0’不相等,也会判定s为非法。

注意事项

  1. 判定s非法的for循环要以s的长度结束
  2. 快速了解sscanf 和 sprintf的用法可以直接看给出的菜鸟教程链接的例子

完整代码

#include <cstdio>
#include <string.h>
using namespace std;
int main()
{
    int n;
    double sum = 0;
    int cnt = 0;
    char s[50],num[50];//char* s 是错误写法
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%s", s);
        double temp;
        sscanf(s,"%lf", &temp);//在字符串s中读取第一个格式为double的数, s是待测定的格式
        sprintf(num,"%.2f", temp);//将读取到的第一个double数格式化后保存在num字符串中, num是合法目标格式
        // 当且仅当s合法的时候,s的和num里的每一个字符都相等,不然,s不合法
        int flag = 0;
        for(int j = 0; j < strlen(s); j++){//注意合法的s小数点后的位数是小于等于temp的位数的,所以这里要以s的长度结尾
            if(s[j]!=num[j])
                flag = 1;
        }
        if(flag || temp < -1000.0 || temp > 1000.0){
            printf("ERROR: %s is not a legal number\n",s);
        }else{
            sum += temp;
            cnt++;
        }
    }
    if(cnt == 0){
        printf("The average of 0 numbers is Undefined");
    }else if(cnt == 1){
        printf("The average of 1 number is %.2f", sum);
    }else{
        printf("The average of %d numbers is %.2f", cnt, sum/cnt);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值