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

WinForm的控件二次开发

Super handy drawing tool is recommended

UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors

社交电商如何做粉丝运营?云平台怎么选择商业模式?

MCM box model modeling method and source analysis of atmospheric O3

【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed

How to prepare for the test interface test data

typescript43-类型兼容性说明

Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8

Jmeter 模拟多用户登录的两种方法
随机推荐
[Fine talk] Using native js to implement todolist
建立树形结构
Harmony OS Date ano UI 】 【 】 the basic operation
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
User password verification
Interface Test Framework Practice (4) | Get Schema Assertion
表的创建、修改与删除
Interface testing framework of actual combat (2) | interface request assertion
Exception (abnormal) and Error (error) difference analysis
JS bottom handwriting
MCM box model modeling method and source analysis of atmospheric O3
c语言结构体中的冒泡排序
在竞争白热化的电商行业,链动2+1为什么还有企业在用
内部类、static关键字、final
shell脚本循环语句
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
业务表解析-余额系统
私域流量引流方法?分享购火爆的商业模式,你值得拥有