1. 程式人生 > >PAT 1054 求平均值 (20)(程式碼+思路+測試用例)

PAT 1054 求平均值 (20)(程式碼+思路+測試用例)

1054 求平均值 (20)(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

 PS:

       我的思路:在判斷一步步確定是否符合題目要求:

                          1、第一位必為數字或者負號(不能只有負號)

                         2、判斷字串從第二位開始的字元,只能出現數字或點,出現點時開啟小數位計數,若出現第二個小數點,說明此數不合法。

                          3、經過上面的篩選,到這裡的一定是數字,只要判斷是否符合題目要求即可

推薦測試用例:

in:
6
- -1.1 0.111 1.1.1 -1000 1000.1


out:
ERROR: - is not a legal number
ERROR: 0.111 is not a legal number
ERROR: 1.1.1 is not a legal number
ERROR: 1000.1 is not a legal number
The average of 2 numbers is -500.55

注意:如果有效數(count)只有一個,注意最後輸出語句裡的number和numbers
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
bool mydigit(char a) {
	return !(isdigit(a) || a == '.');
}
int pd(string s) {
	int point = count(s.begin(),s.end(),'.');		//記錄小數點
	int m = 0;		//記錄小數位數
	if (s.find('.') != string::npos) m = s.length() - s.find('.') - 1;
	if (!(s[0] == '-'&&s.length()>1) && !isdigit(s[0]))			//第一位必為數字或者負號(不能只有負號)
		return 0;
	if (m <= 2 && abs(stof(s)) <= 1000 && find_if(s.begin()+1, s.end(), mydigit) == s.end())			//必須保證在題目範圍內
		return 1;
	return 0;
}
int main() {
	int n, count = 0;
	double t, sum = 0;
	string str;
	cin >> n;
	while (n--) {
		cin >> str;
		if (pd(str)) {
			count++;
			sum += stof(str);
		}
		else 
			cout << "ERROR: " << str << " is not a legal number" << endl;		
	}
	cout << "The average of " << count;
	if (count == 1)						//看清題意
		cout << " number is ";
	else
		cout << " numbers is ";
	if (count)
		printf("%0.2lf", (double)sum / count);
	else
		cout << "Undefined";
	return 0;
}

歷史程式碼:

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int pd(string s) {
	int key = 0;		//記錄小數點
	int m = 0;		//記錄小數位數
	if (!(s[0] == '-'&&s.length()>1) && !isdigit(s[0]))			//第一位必為數字或者負號(不能只有負號)
		return 0;
	for (int i = 1; i < s.length(); i++) {
		if (isdigit(s[i])) {
			if (key) m++;			//小數位計數器
		}
		else if (s[i] == '.') {
			key++;		//開啟計數器
			if (key == 2)  //只能有一個小數點
				return 0;
		}
		else                 //出現其他符號返回0
			return 0;
	}
	if (m <= 2 && abs(atof(s.c_str())) <= 1000)			//必須保證在題目範圍內
		return 1;
	return 0;
}
int main() {
	int n, count = 0;
	double t, sum = 0;
	string str;
	cin >> n;
	while (n--) {
		cin >> str;
		if (pd(str)) {
			count++;
			sum += atof(str.c_str());
		}
		else {
			cout << "ERROR: " << str << " is not a legal number" << endl;
		}
	}
	cout << "The average of " << count;
	if (count == 1)						//看清題意
		cout << " number is ";
	else
		cout << " numbers is ";
	if (count)
		printf("%0.2lf", (double)sum / count);
	else
		cout << "Undefined";
	return 0;
}