PAT A1108 Finding Average (20point(s))

Scroll Down

The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−1000,1000] and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then N numbers are given in the next line, separated by one space.

Output Specification:

For each illegal input number, print in a line ERROR: X is not a legal number where X is the input. Then finally print in a line the result: The average of K numbers is Y where K is the number of legal inputs and Y is their average, accurate to 2 decimal places. In case the average cannot be calculated, output Undefined instead of Y. In case K is only 1, output The average of 1 number is Y instead.

Sample Input 1:

7
5 -3.2 aaa 9999 2.3.4 7.123 2.35

Sample Output 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

Sample Input 2:

2
aaa -9999

Sample Output 2:

ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined

题意

判断输入的字符串是否为合法数字(在-1000到1000内,最多保留两位数字),并输出合法数字平均值

分析

如果要自己编写判断算法会比较耗时间,而且也不好调试,所以解答本题时可以使用sprintf和sscanf,用sscanf得到格式化后的数字num,再用sprinf格式化得到新的字符串,与原字符串比较即可知道是否合法。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>

using namespace std;

int main()
{
	int n, cnt = 0;
	char inputString[30], buff[30];
	scanf("%d", &n);
	double num = 0, sum = 0;
	for (int i = 0; i < n; i++) {
		scanf("%s", inputString);
		sscanf(inputString, "%lf", &num);
		sprintf(buff, "%.2f", num);
		bool flag = false;
		for (int j = 0; j < strlen(inputString); j++)
			if (inputString[j] != buff[j]) {
				flag = true;
				break;
			}
		if (flag || num < -1000 || num>1000) {
			printf("ERROR: %s is not a legal number\n", inputString);
			continue;
		}
		else {
			sum += num;
			cnt++;
		}
	}
	if (cnt == 1) printf("The average of 1 number is %.2f", sum);
	else if (cnt) printf("The average of %d numbers is %.2f", cnt, sum / cnt);
	else printf("The average of 0 numbers is Undefined");
	return 0;
}

不使用sprintf和sscanf

int main()
{
	int n, count = 0, dec = 1;
	bool dotFlag = false;
	bool negFlag = false;
	bool invalid = false;
	double sum = 0;
	string in;
	cin >> n;
	for (int i = 0; i < n; i++) {
		double dig = 0;
		cin >> in;
		// 负数
		for (auto j = 0; j < in.size(); j++) {
			if ('9' >= in[j] && in[j] >= '0') {
				if (!dotFlag) dig = 10.0 * dig + (in[j] - '0');
				else if (dec <= 2) dig = dig + (in[j] - '0') / pow(10, dec++);
				else {
					invalid = true;
					break;
				}
			}
			else if (in[j] == '-' && j == 0) negFlag = true;
			else if (!dotFlag && in[j] == '.' && j >= 1) dotFlag = true;
			else {
				invalid = true;
				break;
			}
		}
		if (dig > 1000 || dig < -1000) invalid = true;
		if (invalid) printf("ERROR: %s is not a legal number\n", in.c_str());
		else {
			if (negFlag) dig = -dig;
			sum += dig;
			count++;
		}
		invalid = false;
		negFlag = false;
		dec = 1;
		dotFlag = false;
	}
	if (count == 0) printf("The average of 0 numbers is Undefined");
	else if (count == 1) printf("The average of 1 number is %.2f", sum);
	else printf("The average of %d numbers is %.2f", count, sum / count);
	return 0;
}