[TOC]
### 題目描述
1054. 求平均值 (20)
本題的基本要求非常簡單:給定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>
#define DOT 1
#define SIGN 2
#define INT 4
#define FLOAT 8
#define LARGE 16
int check(char *buf)
{
int flag=0,i,count,temp;
char dot;
for(i=0;buf[i];i++)
{
switch(buf[i])
{
case '.':
if(flag&DOT)
{
return 0;
}
else
{
flag=flag|DOT;
}
break;
case '+':
case '-':
if(flag&SIGN||flag&DOT)
{
return 0;
}
else
{
flag=flag|SIGN;
}
break;
default:
if(!(buf[i]>='0'&&buf[i]<='9'))
{
return 0;
}
}
}
for(i=0;buf[i]!='.'&&buf[i]!=0;i++)
{
;
}
count=0;
for(i;buf[i];i++)
{
count++;
}
if(count>3)
{
return 0;
}
return 1;
/* for(i=0;buf[i]!='.'&&buf[i]!=0;i++)
{
;
}
dot=buf[i];
buf[i]=0;
sscanf(buf,"%d",&temp);
buf[i]=dot;
if(temp>1000||temp<-1000)
{
return 0;
}
if(buf[i]==0)
{
return 1;
}
if(temp==1000||temp==-1000)
{
flag=flag|LARGE;
}
temp=0;
buf[i]='0';
sscanf(buf+i,"%d",&temp);
buf[i]=dot;
count=0;
for(i++;buf[i];i++)
{
count++;
}
if(count>2)
{
return 0;
}
if((flag&LARGE)&&temp==0||temp<100&&(flag&LARGE)==0)
{
return 1;
}
else
{
return 0;
}*/
}
int main()
{
int n,i,total=0;
double sum=0,temp;
char buf[101];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",buf);
if(check(buf))
{
sscanf(buf,"%lf",&temp);
if(temp<=1000.00&&temp>=-1000.00)
{
sum+=temp;
total++;
}
else
{
printf("ERROR: %s is not a legal number\n",buf);
}
}
else
{
printf("ERROR: %s is not a legal number\n",buf);
}
}
if(total==0)
{
printf("The average of 0 numbers is Undefined\n");
}
else if(total==1)
{
printf("The average of %d number is %.2lf\n",total,sum);
}
else
{
printf("The average of %d numbers is %.2lf\n",total,sum/total);
}
}
```
- 大賽說明
- PAT乙集題庫
- 模板
- 1001.害死人的3n+1 c
- 1002.寫出這個數 c 02
- 1003.我要通過(20)c03
- 1004.成績排名 C 04
- 1005.繼續(3n+1)猜想 (25) c05
- 1006. 換個格式輸出整數 (15) c06
- 1007.素數對猜想 (20) C 07
- 1008. 數組元素循環右移問題 (20) c08
- 1009.說反話(20)c09
- 1010.一元多項式求導 (25) c10
- 1011. A+B和C(15)c11
- 1012.數字分類 C12
- 1013.數素數(20) C 13
- 1014.福爾摩斯的約會(20)c 14
- 1015.德才論c15
- 1016.部分A+B C 16
- 1018.錘子剪刀布c18
- 1023 組個最小數(20) C23
- 1024 科學計數法(20) C24
- 1025.反轉鏈表(25)C25
- 1026.程序運行時間(15)c 26
- 1027. 打印沙漏(20) C 27
- 1028 人口普查(20) C 01
- 1029. 舊鍵盤(20) C 01
- 1030.完美數列 C 04
- 1031. 查驗身份證(15) c05
- 1032. 挖掘機技術哪家強(20) c06
- 1033. 舊鍵盤打字(20) C 07
- 1034. 有理數四則運算 08
- 1035.插入和歸并 C 09
- 1036.跟奧巴馬一起編程(15)C18
- 1037.在霍格沃茨找零錢C 11
- 1039.到底買不買 C 13
- 1041.考試座位號(15)C03
- 1042。字符統計 C16
- 1044.火星數字 C 15
- 1046. 劃拳(15) C 01
- 1047.編程團體賽C18
- 1051.復數乘法(15) c24
- 1054. 求平均值 (20) C
- 1036. 跟奧巴馬一起編程(15) c10
- 1055.集體照 C 09
- 1050. 螺旋矩陣
- C語言
- assert.h
- stdio.h
- Operations on files
- File access
- Formatted input/output
- Character input/output
- Direct input/output
- File positioning
- Error-handling
- Macros
- 數據結構
- 算法
- 5月8日測試題目
- 喬棟
- 01申彥棟
- 02方常吉
- 03王永明
- 04劉果靜
- 05原曉曉
- 09牛錦江
- 10李澤路
- 11葛建輝
- 12程才耀
- 13王旭昕
- 16田鵬
- 15李新奇
- 24王翡
- 18高捷
- 07耿生年
- 05王進
- 25閆鑫炎
- 5月9日測試題目
- 熱身題庫
- L1-1
- L1-2
- L1-3
- L1-4
- L1-5
- L1-6
- L1-8
- L1-7
- L2-1
- L2-2
- L2-3
- L2-4
- L3-1
- L3-2
- L3-3