PAT Basic 1054. 求平均值 (20)(C語言實現)
, CSDN內容暫時不更新(將來有計劃更新), 請前往連結檢視最新內容. 歡迎star 我的repo
題目本題的基本要求非常簡單:給定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
思路
先說說輸入:我們不清楚題目會給出多長的非法輸入,因此為了防止溢位,我們應該選擇一個安全的方式應對這種情況。
我的方法是先確定合法數字的最長長度:8個字元,那麼我們每次就先只讀8個字元。如果輸入流(stdin
)中的下一個字元不是空白字元,那麼這個數就絕對非法;如果是空白字元的話,再繼續討論接下來的問題。
(這個問題幾乎網上所有AC的方法都是用的一個大陣列來讀取,應該是PAT對這一點沒有嚴格檢查(要是有上千字元的輸入就不行了)。我只是出於健壯性考慮,照顧到了這一點)
(通過率0.19的題我一次AC了,先讓我激動一會兒 \(≧≦)/)
程式碼實現
- 讀取8個字元,使用了
scanf("%8s", str)
,就是讀到空白字元或者8個字元。 - 判斷是否是浮點,使用了函式
strtod(const char *, char **)
,這個函式的第二個引數是一個指標的地址,用來使該指標指向未用於轉換的第一個字元,我們可以根據這個指標是否為NULL
來判斷這個字串是否為浮點。
(e.g. 如第一個引數傳入字串2.3.4,第二個引數這個指標會指向第二個小數點) - 精度,找到小數點(有的話)的位置,看它距字串結尾有幾個字元就好了,不能大於3個。
P.S. 在網上看到了結合使用sscanf和sprintf的方法,相當的巧妙。
程式碼
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int count = 0, N;
double f, sum = 0;
/* Maxium scenario: -1000.00. So just need to read 8 chars(+ '\0' = 9) */
char s[9], *pEnd, *pDot, c;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%8s", s); /* Just read 8 chars */
c = ungetc(getchar(), stdin); /* If the next is non-white char, it is too long */
f = strtod(s, &pEnd); /* pEnd points to '\0' for a floating number */
pDot = strchr(s, '.'); /* find the decimal point */
if(!isspace(c) /* more than 8 chars */
|| *pEnd /* not floating number */
|| (f > 1000 || f < -1000) /* out of range */
|| (pDot && pDot - s < strlen(s) - 3)) /* precision too high */
{
printf("ERROR: %s", s);
/* this can avoid array overflow(we don't know how long input is) */
while(!isspace(c = getchar())) putchar(c);
printf(" is not a legal number\n");
}
else
{ /* legel number */
count++;
sum += f;
}
}
if(count == 0) printf("The average of 0 numbers is Undefined\n");
if(count == 1) printf("The average of 1 number is %.2lf", sum);
if(count >= 2) printf("The average of %d numbers is %.2lf", count, sum / count);
return 0;
}