当前位置:网站首页>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;
}
边栏推荐
- CobalStrike(CS)基础超级详细版
- Shell之条件语句
- 技术分享 | 接口自动化测试中如何对xml 格式做断言验证?
- 接口测试框架实战 | 流程封装与基于加密接口的测试用例设计
- Build your own web page on the Raspberry Pi (2)
- typescript47-函数之间的类型兼容性
- unity2D横板游戏教程6-敌人AI以及受击动画
- Apache DolphinScheduler版本2.0.5分布式集群的安装
- User password encryption tool
- 【Biotin Azide|cas:908007-17-0】Price_Manufacturer
猜你喜欢

接口测试框架实战 | 流程封装与基于加密接口的测试用例设计

Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data

【Harmony OS】【ARK UI】Date 基本操作

力扣561. 数组拆分

【Harmony OS】【ArkUI】ets开发 图形与动画绘制

Two ways to simulate multi-user login in Jmeter
![[Harmony OS] [ArkUI] ets development graphics and animation drawing](/img/36/f4c91f794b1321f11a24505d1617fb.png)
[Harmony OS] [ArkUI] ets development graphics and animation drawing

Concepts and Methods of Exploratory Testing

IO process thread -> thread -> day5

Shell之条件语句
随机推荐
【Harmony OS】【ArkUI】ets开发 图形与动画绘制
接口测试框架实战(二)| 接口请求断言
接口测试框架实战 | 流程封装与基于加密接口的测试用例设计
【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
Modified BiotinDIAZO-Biotin-PEG3-DBCO|diazo-biotin-tripolyethylene glycol-diphenylcyclooctyne
c语言结构体中的冒泡排序
[Harmony OS] [ARK UI] ETS context basic operations
数字孪生园区场景中的坐标知识
Ali cloud object storage oss private barrels to generate links
13.< tag-动态规划和回文字串>lt.647. 回文子串 + lt.516.最长回文子序列
在树莓派上搭建属于自己的网页(2)
软件开发的最大的区别是什么?
Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection
Detailed explanation of MOSN reverse channel
接口管理工具YApi怎么用?颜值高、易管理、超好用
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
【Harmony OS】【FAQ】鸿蒙问题合集1
Interface Test Framework Practice (4) | Get Schema Assertion
【生物素叠氮化物|cas:908007-17-0】价格_厂家