当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

VR全景展打造专属元宇宙观展空间

Kotlin-Flow common encapsulation class: the use of StateFlow

【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability

【Harmony OS】【FAQ】鸿蒙问题合集1

接口和抽象

Jmeter 模拟多用户登录的两种方法

接口管理工具YApi怎么用?颜值高、易管理、超好用

Common lipophilic cell membrane dyes DiO, Dil, DiR, Did spectrograms and experimental procedures

阿里云对象存储oss私有桶生成链接

三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
随机推荐
链动2+1模式简单,奖励结构丰厚,自主裂变?
c语言结构体中的冒泡排序
js garbage collection mechanism
Presto installation and deployment tutorial
CobalStrike(CS)基础超级详细版
PotPlayer实现上班摸鱼电视自由
Interface testing framework combat (3) | JSON request and response assertion
Kotlin-Flow common encapsulation class: the use of StateFlow
How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use
DFS对剪枝的补充
Talking about GIS Data (5) - Geographic Coordinate System
接口测试框架实战(二)| 接口请求断言
在树莓派上搭建属于自己的网页(1)
接口测试框架实战(四)| 搞定 Schema 断言
传统企业如何转型社交电商,泰山众筹的玩法有哪些?
IO进程线程->线程->day5
Shell conditional statement judgment
Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)
Harmony OS Date ano UI 】 【 】 the basic operation
Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface