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:

  1. 7
  2. 5 -3.2 aaa 9999 2.3.4 7.123 2.35

输出样例1:

  1. ERROR: aaa is not a legal number
  2. ERROR: 9999 is not a legal number
  3. ERROR: 2.3.4 is not a legal number
  4. ERROR: 7.123 is not a legal number
  5. The average of 3 numbers is 1.38

输入样例2:

  1. 2
  2. aaa -9999

输出样例2:

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

这题一定要看清楚题目

几种输出情况:

  1. The average of 3 numbers is 1.38
  1. The average of 1 number is 1.38 //因为这个调了一天一夜,过不了测试点3
  1. ERROR: aaa is not a legal number
  1. The average of 0 numbers is Undefined
  2.  
  3. 注意:该题注意以下几个方面
    1.-号只能在第一位
    2.小数点不能在第一位
    3.按理说小数点不能做最后一位,但是测试点4就是放在最后一位
  4.  
  5. 本文运用了 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=;
  25. int i, N,num=;
  26.  
  27. cin >> N;
  28.  
  29. for (i = ; i < N; ++i)
  30. {
  31. cin >> temp;
  32.  
  33. if (judge(temp))
  34. {
  35. //t = change_type(temp);
  36. t = atof(&temp[]);
  37.  
  38. if (t >= - && t<=)
  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 == )
  48. cout << "The average of 0 numbers is Undefined" << endl;
  49. else if (num==)
  50. cout << "The average of 1 number is "
  51. << fixed << setprecision() << (sum / num) << endl;
  52. else
  53. cout << "The average of " << num << " numbers is "
  54. <<fixed<<setprecision()<< (sum / num) << endl;
  55.  
  56. return ;
  57. }
  58.  
  59. //判断是否是合法的数字,不包括判断范围
  60. int judge(string& str)
  61. {
  62. int size = str.size(),flag=,num=-,flag1=;
  63.  
  64. //数字留下
  65. for (int i = ; i < size; ++i)
  66. {
  67. if ((isdigit(str[i]) || str[i] == '-' || str[i] == '.'))//不包含非法字符
  68. {
  69. if (str[i] == '-'&&i != )//-号必须在第一位
  70. return ;
  71.  
  72. if (str[i] == '.')
  73. {
  74. ++flag;
  75.  
  76. if (flag > ||i==)//.号最多一个且不能在第一位
  77. return ;
  78. }
  79.  
  80. if (flag > )//小数位不超过两位
  81. {
  82. ++num;
  83.  
  84. if (num > )
  85. return ;
  86. }
  87. }
  88. else
  89. return ;
  90. }
  91.  
  92. return ;
  93. }
  1.  

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

  1. //将string转换为实数型
  2. double change_type(string& str)
  3. {
  4. int i, size = str.size();
  5.  
  6. //求出下标
  7. for (i = ; i < size; ++i)
  8. if (str[i] == '.')
  9. break;
  10.  
  11. if (i == size)//整数时
  12. return str_to_int(str, size);
  13. else//小数时
  14. return str_to_double(str, i, size);
  15. }
  16.  
  17. //字符型转换为整型
  18. double str_to_int(string& str,int size)
  19. {
  20. double num = ,j=;
  21.  
  22. while (--size >= )
  23. {
  24. if (isdigit(str[size]))
  25. {
  26. num += static_cast<int>(str[size] - ) * j;
  27.  
  28. j *= ;
  29. }
  30. }
  31.  
  32. return str[]=='-' ? -num : num;
  33. }
  34.  
  35. //字符型转换为实数型
  36. double str_to_double(string& str, int flag, int size)
  37. {
  38. double j = pow(, flag - size+),num=;
  39.  
  40. for (int i = size - ; i >= ; --i)
  41. {
  42. if (isdigit(str[i]))
  43. {
  44. num+=static_cast<int>(str[i] - )*j;
  45.  
  46. j *= ;
  47. }
  48. }
  49.  
  50. return str[] == '-' ? -num : num;
  51. }
  1.  
  1.  

PAT 乙级 1054 求平均值 (20) C++版的更多相关文章

  1. 1054. 求平均值 (20)-PAT乙级真题

    今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org ...

  2. PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 题目描述 本题的基本要求非常简单:给定 N 个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的 ...

  3. PAT 1054 求平均值 (20)(代码+思路+测试用例)

    1054 求平均值 (20)(20 分) 本题的基本要求非常简单:给定N个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的.一个"合法"的输入是[-1000,1000]区 ...

  4. PAT-乙级-1054. 求平均值 (20)

    1054. 求平均值 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题的基本要求非常简单:给定N个实 ...

  5. PAT(B) 1054 求平均值(Java)

    题目链接:1054 求平均值 (20 point(s)) 题目描述 本题的基本要求非常简单:给定 N 个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的.一个"合法"的输 ...

  6. PAT 1054. 求平均值 (20)

    本题的基本要求非常简单:给定N个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的.一个“合法”的输入是[-1000,1000]区间内的实数,并且最多精确到小数点后2位.当你计算平均值的时候, ...

  7. PAT 乙级 1047 编程团体赛(20) C++版

    1047. 编程团体赛(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 编程团体赛的规则为:每个参赛队由若 ...

  8. PAT 乙级 1029 旧键盘(20) C++版

    1029. 旧键盘(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 旧键盘上坏了几个键,于是在敲一段文字的 ...

  9. PAT 乙级 1043 输出PATest(20) C++版

    1043. 输出PATest(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个长度不超过10000 ...

随机推荐

  1. PowerDesigner15 增加Domain域

    第一步: 第二步: 点击此按钮,在弹出框中对Domain域打钩即可

  2. HTTP/2及HTTP/3特性

    HTTP/2及HTTP/3特性 摘要: 学习 HTTP/2 与 HTTP/3. 前言 HTTP/2 相比于 HTTP/1,可以说是大幅度提高了网页的性能,只需要升级到该协议就可以减少很多之前需要做的性 ...

  3. eclipse + cdt

    Window > Preferences > General > Appearance中设置主题颜色. Help > eclipse marketplace > find ...

  4. 安装,配置,启动FTP,SSH,NFS服务

    1.安装,配置,启动FTP服务 sudo apt-get install vsftpd 修改vsftpd的配置文件/etx/vsftpd/.config,将下面几行前面的“#”去掉 #local_en ...

  5. python-基础-文件

    一.文件操作 打开文件时,需要指定文件路径和以何等方式打开文件, 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 打开文件的模式有: r ,只读模式[默认模式 ...

  6. LeetCode - Convert BST to Greater Tree

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  7. oracle完全之dbf文件出现问题, ORA-01219

    alter database datafile '/data/app/oradata/ora237/users01.dbf' offline drop; 强制删除该故障文件

  8. Server 2008 R2部署active directory服务器-ad域

    一.部署AD域: 系统环境:Windows server 2008 R2 标准版 ip:172.16.1.149服务器开机后会自动弹出一个初始配置任务窗口(这个不要随便关闭,因为我不知道关闭后能在哪里 ...

  9. redis使用问题总结

    1.redis使用过多内存导致其他进程无法正常运行情况:      解决方案:限制redis的最大使用内存,修改redis.conf中的maxmemory(一般不要超过空闲内存的3/5,如果不设置ma ...

  10. BackgroundWorker Class Sample for Beginners

    Download source - 27.27 KB Introduction This article presents a novice .NET developer to develop a m ...