当前位置:网站首页>1054 求平均值 (20 分)
1054 求平均值 (20 分)
2022-08-03 05:09:00 【破烂摆烂人】
很难过 少学了sprintf和sccanf 读字符串靠手敲 不过满分 烧脑了
本题的基本要求非常简单:给定 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
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n ;
scanf("%d",&n) ;
int k = 0 ;
double sum = 0 , ave = 0 ;
char ch[50] ;
int i , j ;
for( i = 0 ; i < n ; i++ ){
scanf("%s",&ch) ;
int flag = 1 ;
double num = 0;
if(!(ch[0]=='-'||(ch[0]>='0'&&ch[9]<='9'))){
flag = 0 ;
}
for( j = 1 ; j < strlen(ch) ; j++ ){
if(!((ch[j]>='0'&&ch[j]<='9')||ch[j]=='.')){
flag = 0 ;
break ;
}
}
int flag1 = 0 ;
if(flag == 1){
int count = 0 ;
for( j = 1 ; j < strlen(ch) ; j++ ){
if( ch[j] == '.' ){
count++ ;
flag1 = 1 ;
}
}
if( count > 1){
flag = 0 ;
}
}
if(flag == 1){
if(flag1 == 0&&ch[0]!='-'){
for( j = 0 ; j < strlen(ch) ;j++ ){
num = num*10 + (ch[j] - '0') ;
}
}else if(flag1 == 0&&ch[0]=='-'){
for( j = 1 ; j < strlen(ch) ;j++ ){
num = num*10 + (ch[j] - '0') ;
}
num = 0 - num ;
}else if(flag1 == 1 && ch[0]!='-'){
int index ;
for( j = 0 ; j < strlen(ch) ; j++ ){
if(ch[j] == '.'){
index = j ;
break;
}
}
if(strlen(ch) - index > 3){
flag = 0 ;
}
for( j = 0 ; j < index ; j++ ){
num = num*10 + (ch[j] - '0') ;
}
for( j = index + 1 ; j < strlen(ch) ; j++ ){
num = num + (ch[j] - '0')*pow(10,index-j) ;
}
}else if(flag1 == 1 && ch[0]=='-'){
int index ;
for( j = 0 ; j < strlen(ch) ; j++ ){
if(ch[j] == '.'){
index = j ;
break;
}
}
if(strlen(ch) - index > 3){
flag = 0 ;
}
for( j = 1 ; j < index ; j++ ){
num = num*10 + (ch[j] - '0') ;
}
for( j = index + 1 ; j < strlen(ch) ; j++ ){
num = num + (ch[j] - '0')*pow(10,index-j) ;
}
num = 0 - num ;
}
if(!(num>=-1000&&num<=1000)) {
flag = 0 ;
}
}
if( flag == 1){
sum += num ;
k++ ;
}else{
printf("ERROR: %s is not a legal number\n",ch) ;
}
}
if( k == 0){
printf("The average of %d numbers is Undefined\n",k);
}else if(k > 1){
ave = sum/k ;
printf("The average of %d numbers is %.2f",k , ave);
}else if(k==1){
ave = sum/k ;
printf("The average of %d number is %.2f",k , ave);
}
return 0;
}
边栏推荐
- 接口管理工具YApi怎么用?颜值高、易管理、超好用
- 2022暑假牛客多校联赛第一场
- 13.
lt.647. Palindromic substring + lt.516. Longest palindrome subsequence - 三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
- 私域流量时代来临,电商企业如何布局?
- 接口和抽象
- Kotlin-Flow common encapsulation class: the use of StateFlow
- Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
- 【Harmony OS】【ARK UI】ETS 上下文基本操作
- rosbag工具plotjuggler无法打开rosbag的问题
猜你喜欢
BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier
【软件工程之美 - 专栏笔记】35 | 版本发布:软件上线只是新的开始
接口和抽象
2022/08/02 Study Notes (day22) Multithreading
typescript46-函数之间的类型兼容性
在树莓派上搭建属于自己的网页(2)
【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
Talking about GIS Data (6) - Projected Coordinate System
阿里云对象存储oss私有桶生成链接
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
随机推荐
数字孪生园区场景中的坐标知识
Get the Ip tool class
typescript49-交叉类型
js implements a bind function
接口测试框架实战(一) | Requests 与接口请求构造
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
MCM箱模型建模方法及大气O3来源解析
用户密码验证
Shell conditional statement judgment
Flink状态
探索性测试的概念及方法
Where is the value of testers
「短视频+社交电商」营销模式爆发式发展,带来的好处有什么?
业务表解析-余额系统
Talking about GIS Data (6) - Projected Coordinate System
Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
数字化时代,企业如何建立自身的云平台与商业模式的选择?
社交电商:链动2+1模式,为什么能在电商行业生存那么久?
presto安装部署教程
UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头