PAT (Basic Level) Practice (中文)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

题目要求

作者     			CHEN, Yue
单位 浙江大学
代码长度限制 16 KB
时间限制 200 ms
内存限制 64 MB

解题思路

// 本题的难点在于如何判断输入的数字的合法性。
// 参考了一个大佬的代码,以及阅读网上的题解得到如下最简洁的办法 利用 sscanf() 和 sprintf() 两个函数。 sscanf( str,"%lf",&n );把字符数组 str 中的内容以 %lf 的格式 写到 n 中,从左至右 。
其写入时,非数值类型的字符,不会写入到 n 中
例如 sscanf( "7a" , "%lf" , &temp) ,则 temp 中储存的是数字 7 . sprintf( str,"%.2f",n ); 把 n 中的内容以 %.2f 的格式写到字符数组 str 中,从右至左 。

无奈的坑点

坑点 1 就是 数组开大一点!

本来呢,以为题目中规定的数据在 [-1000 , 1000] 之间 数据不会太大。但是我开 20 大小的数组竟然越界!!!

坑点 2 ,也是我觉得最无语的,当数组大小满足要求了,竟然有 测试点 3 显示答案错误

原因竟然是 当只有 1 个合法的数字的时候, numbers 不能加 s !!!!!!

完整代码

#include<bits/stdc++.h>
using namespace std ;
char str[50],str2[50] ;
int main(){
int n , cnt = 0 ;
cin >> n;
double temp = 0.0 , sum = 0.0 ;
for(int i = 0 ; i < n ; i ++){
scanf("%s" , str);
sscanf(str , "%lf" , &temp) ;
sprintf(str2 , "%.2f" , temp) ;
int flag = 0 ;
for(int j = 0 ; j < strlen(str) ; j ++){
if(str[j] != str2[j]){
flag = 1 ;
break ;
}
}
if(flag || temp < -1000 || temp > 1000){
printf("ERROR: %s is not a legal number\n" , str) ;
continue ;
}else{
sum += temp ;
cnt ++ ;
}
}
if(cnt == 1)
printf("The average of 1 number is %.2f\n" , sum) ;
else if(cnt > 1)
printf("The average of %d numbers is %.2f\n" , cnt , sum / cnt) ;
else
printf("The average of 0 numbers is Undefined\n") ;
return 0 ;
}

其他博客

PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 凌宸1642的更多相关文章

  1. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

  2. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...

  3. PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...

  4. PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642 题目描述: Given a non-negative integer N ...

  5. PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...

  6. PAT (Basic Level) Practice (中文)1078 字符串压缩与解压 (20 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1078 字符串压缩与解压 (20 分) 凌宸1642 题目描述: 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一 ...

  7. PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...

  8. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  9. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...

随机推荐

  1. p5.js

    p5.js p5.j​​s是一个用于创意编码的JavaScript库,其重点是使艺术家,设计师,教育者,初学者以及其他任何人都可以访问并包含所有编码! https://p5js.org/ https: ...

  2. js binary search algorithm

    js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...

  3. React Hooks in depth

    React Hooks in depth React Hooks https://reactjs.org/docs/hooks-rules.html https://www.npmjs.com/pac ...

  4. 阿里面试这样问:redis 为什么把简单的字符串设计成 SDS?

    2021开工第一天,就有小伙伴私信我,还给我分享了一道他面阿里的redis题(这家伙绝比已经拿到年终奖了),我看了以后觉得挺有意思,题目很简单,是那种典型的似懂非懂,常常容易被大家忽略的问题.这里整理 ...

  5. 【SVM】kaggle之澳大利亚天气预测

    项目目标 由于大气运动极为复杂,影响天气的因素较多,而人们认识大气本身运动的能力极为有限,因此天气预报水平较低,预报员在预报实践中,每次预报的过程都极为复杂,需要综合分析,并预报各气象要素,比如温度. ...

  6. Spark和Spring整合处理离线数据

    如果你比较熟悉JavaWeb应用开发,那么对Spring框架一定不陌生,并且JavaWeb通常是基于SSM搭起的架构,主要用Java语言开发.但是开发Spark程序,Scala语言往往必不可少. 众所 ...

  7. eclipse修改默认的代码注释

    在使用Eclipse编写Java代码时,自动生成的注释信息都是默认是使用的当前登录系统用户名,实际上是可以修改的. 选择Window → Preference → Java → Code Style  ...

  8. SpringBoot使用谷歌方式生成图片验证码

    1.新建一个springboot的项目 2.导入坐标 <dependency> <groupId>com.github.penggle</groupId> < ...

  9. 解决异常: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7:generate failed: Cannot instantiate object of type tk.mybatis.mapper.generator.MapperPlugin -> [Help 1]

    mybatis-generator整合通用mapper使用generator插件生成model.mapper时报错: 产生以下错误:↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 解决办法: ...

  10. LeetCode392. 判断子序列

    原题链接 1 class Solution: 2 def isSubsequence(self, s: str, t: str) -> bool: 3 lens,lent = len(s),le ...