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

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()

 1 // 1054.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include<iostream>
 6 #include<string>
 7 #include<cctype>
 8 #include<typeinfo>
 9 #include<algorithm>
10 #include<iomanip>
11 #include<cmath>
12 #include<cstdlib>
13 
14 using namespace std;
15 
16 int judge(string& str);
17 double change_type(string& str);
18 double str_to_double(string& str, int flag, int size);
19 double str_to_int(string& str, int size);
20 
21 int main()
22 {
23     string temp;
24     double t,sum=0;
25     int i, N,num=0;
26 
27     cin >> N;
28 
29     for (i = 0; i < N; ++i)
30     {
31         cin >> temp;
32 
33         if (judge(temp))
34         {
35             //t = change_type(temp);
36             t = atof(&temp[0]);
37 
38             if (t >= -1000 && t<=1000)
39                 ++num,sum += t;
40             else
41                 cout << "ERROR: " << temp << " is not a legal number" << endl;
42         }
43         else
44             cout << "ERROR: " << temp << " is not a legal number" << endl;
45     }
46 
47     if (num == 0)
48         cout << "The average of 0 numbers is Undefined" << endl;
49     else if (num==1)
50         cout << "The average of 1 number is "
51         << fixed << setprecision(2) << (sum / num) << endl;
52     else
53         cout << "The average of " << num << " numbers is " 
54              <<fixed<<setprecision(2)<< (sum / num) << endl;
55 
56     return 0;
57 }
58 
59 //判断是否是合法的数字,不包括判断范围
60 int judge(string& str)
61 {
62     int size = str.size(),flag=0,num=-1,flag1=0;
63 
64     //数字留下
65     for (int i = 0; i < size; ++i)
66     {
67         if ((isdigit(str[i]) || str[i] == '-' || str[i] == '.'))//不包含非法字符
68         {
69             if (str[i] == '-'&&i != 0)//-号必须在第一位
70                 return 0;
71 
72             if (str[i] == '.')
73             {
74                 ++flag;
75 
76                 if (flag > 1||i==0)//.号最多一个且不能在第一位
77                     return 0;
78             }
79 
80             if (flag > 0)//小数位不超过两位
81             {
82                 ++num;
83 
84                 if (num > 2)
85                     return 0;
86             }
87         }
88         else
89             return 0;
90     }
91     
92     return 1;
93 }


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

//将string转换为实数型
double change_type(string& str)
{
    int i, size = str.size();

    //求出下标
    for (i = 0; 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 = 0,j=1;

    while (--size >= 0)
    {
        if (isdigit(str[size]))
        {
            num += static_cast<int>(str[size] - 48) * j;

            j *= 10;
        }
    }

    return str[0]=='-' ? -num : num;
}

//字符型转换为实数型
double str_to_double(string& str, int flag, int size)
{
    double j = pow(10, flag - size+1),num=0;

    for (int i = size - 1; i >= 0; --i)
    {
        if (isdigit(str[i]))
        {
            num+=static_cast<int>(str[i] - 48)*j;

            j *= 10;
        }
    }

    return str[0] == '-' ? -num : num;
}
 
 
原文地址:https://www.cnblogs.com/cdp1591652208/p/7262829.html