PTA 1108 Finding Average (20 分)
1108 Finding Average (20 分)
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
感悟
还是审题不认真,漏看了一句话卡了半个小时,看到密密麻麻的英文就忍不住跳着看.题不难,判断一下是不是合法数字,如果是合法数字再判断一下在不在规定区间内,然后按题意要求处理就好.
顺便记录一下stod()string转double,stoi()string转int,str.c_str()string转字符串,atoi()int转字符串,atof()float转字符串.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int cnt;
double res;
int check(string str)
{
int flen = 0,integer = 0, decimal = 0,flag1 = 0,flag2 = 0,flag3 = 0;
// flag1 判断有无前导符合 flag2 判断有无小数点 flag3 判断有无有效数字
for(int i = 0 ; i < str.length() ; i++)
{
if(str[i] >= '0' && str[i] <= '9' || str[i] == '.' || str[i] == '-')
{
// 前导正负号
if(str[i] >= '0' && str[i] <= '9') flag3 = 1;
if((str[i] == '-' || str[i] == '+' )&& !flag1)
{
flag1 = 1;
if(str[i] == '-') flag1 = -1;
continue;
} else if(str[i] == '-' || str[i] == '+' ) {
return 0;
}
// 小数点
if(str[i] == '.' && !flag2)
{
flag2 = 1;
continue;
} else if(str[i] == '.') {
return 0;
}
if(flag2) flen++,decimal *= 10, decimal += str[i] - '0';
else integer *= 10,integer += str[i] - '0';
} else {
return 0;
}
}
if(!flag3 || flen > 2 || integer > 1000 || integer == 1000 && decimal != 0) return 0;
return 1;
}
int main()
{
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
string str;
cin >> str;
if(!check(str))printf("ERROR: %s is not a legal number\n",str.c_str());
else {
double s = stod(str);
if(s > 1000 || s < -1000 ) continue;
cnt++,res += s;
}
}
if(!cnt) printf("The average of 0 numbers is Undefined\n");
else if(cnt == 1) printf("The average of 1 number is %.2lf",res);
else printf("The average of %d numbers is %.2lf\n",cnt,res/cnt);
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int cnt;
double res;
int check(string str)
{
int flen = 0,integer = 0, decimal = 0,flag1 = 0,flag2 = 0,flag3 = 0;
// flag1 判断有无前导符合 flag2 判断有无小数点 flag3 判断有无有效数字
for(int i = 0 ; i < str.length() ; i++)
{
if(str[i] >= '0' && str[i] <= '9' || str[i] == '.' || str[i] == '-')
{
// 前导正负号
if(str[i] >= '0' && str[i] <= '9') flag3 = 1;
if((str[i] == '-' || str[i] == '+' )&& !flag1)
{
flag1 = 1;
if(str[i] == '-') flag1 = -1;
continue;
} else if(str[i] == '-' || str[i] == '+' ) {
return 0;
}
// 小数点
if(str[i] == '.' && !flag2)
{
flag2 = 1;
continue;
} else if(str[i] == '.') {
return 0;
}
if(flag2) flen++,decimal *= 10, decimal += str[i] - '0';
else integer *= 10,integer += str[i] - '0';
} else {
return 0;
}
}
if(!flag3 || flen > 2 || integer > 1000 || integer == 1000 && decimal != 0) return 0;
return 1;
}
int main()
{
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
string str;
cin >> str;
if(!check(str))printf("ERROR: %s is not a legal number\n",str.c_str());
else {
double s = stod(str);
if(s > 1000 || s < -1000 ) continue;
cnt++,res += s;
}
}
if(!cnt) printf("The average of 0 numbers is Undefined\n");
else if(cnt == 1) printf("The average of 1 number is %.2lf",res);
else printf("The average of %d numbers is %.2lf\n",cnt,res/cnt);
return 0;
}