PAT 乙级 1054 求平均值 (20) C++版

时间:2023-12-20 20:23:38

1054. 求平均值 (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

本题的基本要求非常简单:给定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

这题一定要看清楚题目

几种输出情况:

The average of 3 numbers is 1.38
The average of 1 number is 1.38  //因为这个调了一天一夜,过不了测试点3
ERROR: aaa is not a legal number
The average of 0 numbers is Undefined

注意:该题注意以下几个方面
1.-号只能在第一位
2.小数点不能在第一位
3.按理说小数点不能做最后一位,但是测试点4就是放在最后一位 本文运用了 isdigit()函数判断该字符是否为数字 头文件 #include<cctype>
C标准库提供了字符串转换为实数的函数 atof(字符串首地址)#include<cstdlib>
文中还是写出了转换函数change_type()
 // 1054.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include<iostream>
#include<string>
#include<cctype>
#include<typeinfo>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstdlib> using namespace std; int judge(string& str);
double change_type(string& str);
double str_to_double(string& str, int flag, int size);
double str_to_int(string& str, int size); int main()
{
string temp;
double t,sum=;
int i, N,num=; cin >> N; for (i = ; i < N; ++i)
{
cin >> temp; if (judge(temp))
{
//t = change_type(temp);
t = atof(&temp[]); if (t >= - && t<=)
++num,sum += t;
else
cout << "ERROR: " << temp << " is not a legal number" << endl;
}
else
cout << "ERROR: " << temp << " is not a legal number" << endl;
} if (num == )
cout << "The average of 0 numbers is Undefined" << endl;
else if (num==)
cout << "The average of 1 number is "
<< fixed << setprecision() << (sum / num) << endl;
else
cout << "The average of " << num << " numbers is "
<<fixed<<setprecision()<< (sum / num) << endl; return ;
} //判断是否是合法的数字,不包括判断范围
int judge(string& str)
{
int size = str.size(),flag=,num=-,flag1=; //数字留下
for (int i = ; i < size; ++i)
{
if ((isdigit(str[i]) || str[i] == '-' || str[i] == '.'))//不包含非法字符
{
if (str[i] == '-'&&i != )//-号必须在第一位
return ; if (str[i] == '.')
{
++flag; if (flag > ||i==)//.号最多一个且不能在第一位
return ;
} if (flag > )//小数位不超过两位
{
++num; if (num > )
return ;
}
}
else
return ;
} return ;
}

当然,想自己实现由string转换为double或者整型,添加如下三个函数即可

//将string转换为实数型
double change_type(string& str)
{
int i, size = str.size(); //求出下标
for (i = ; i < size; ++i)
if (str[i] == '.')
break; if (i == size)//整数时
return str_to_int(str, size);
else//小数时
return str_to_double(str, i, size);
} //字符型转换为整型
double str_to_int(string& str,int size)
{
double num = ,j=; while (--size >= )
{
if (isdigit(str[size]))
{
num += static_cast<int>(str[size] - ) * j; j *= ;
}
} return str[]=='-' ? -num : num;
} //字符型转换为实数型
double str_to_double(string& str, int flag, int size)
{
double j = pow(, flag - size+),num=; for (int i = size - ; i >= ; --i)
{
if (isdigit(str[i]))
{
num+=static_cast<int>(str[i] - )*j; j *= ;
}
} return str[] == '-' ? -num : num;
}