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

Bubble sort in c language structure

设计模式——组合模式、享元模式(Integer缓存)(结构型模式)

高可用 两地三中心

IO process thread -> thread -> day5

安装IIS服务(Internet信息服务(Internet Information Services,简写IIS,互联网信息服务)

Alienware上线首个数字时装AR试穿体验

2022/08/02 学习笔记 (day22) 多线程

CobalStrike(CS)基础超级详细版

Two ways to simulate multi-user login in Jmeter

Install PostgreSQL on Windows
随机推荐
User password encryption tool
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
GIS数据漫谈(六)— 投影坐标系统
Install PostgreSQL on Windows
力扣561. 数组拆分
【开发者必看】【push kit】推送服务服务典型问题合集2
odps的临时查询能在写sql的时候就给结果一个命名不?
unity2D横板游戏教程6-敌人AI以及受击动画
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
Bubble sort in c language structure
接口测试实战| GET/POST 请求区别详解
The problem that the rosbag tool plotjuggler cannot open rosbag
【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability
typescript40-class类的保护修饰符
用户密码加密工具
typescript46-函数之间的类型兼容性
c语言结构体中的冒泡排序
Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
社交电商:流量红利已尽,裂变营销是最低成本的获客之道
【Harmony OS】【ARK UI】Date 基本操作