PAT 乙级 1054 求平均值 (20) C++版
1054. 求平均值 (20)
本题的基本要求非常简单:给定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;
- }
PAT 乙级 1054 求平均值 (20) C++版的更多相关文章
- 1054. 求平均值 (20)-PAT乙级真题
今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org ...
- PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 凌宸1642
PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 题目描述 本题的基本要求非常简单:给定 N 个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的 ...
- PAT 1054 求平均值 (20)(代码+思路+测试用例)
1054 求平均值 (20)(20 分) 本题的基本要求非常简单:给定N个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的.一个"合法"的输入是[-1000,1000]区 ...
- PAT-乙级-1054. 求平均值 (20)
1054. 求平均值 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题的基本要求非常简单:给定N个实 ...
- PAT(B) 1054 求平均值(Java)
题目链接:1054 求平均值 (20 point(s)) 题目描述 本题的基本要求非常简单:给定 N 个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的.一个"合法"的输 ...
- PAT 1054. 求平均值 (20)
本题的基本要求非常简单:给定N个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的.一个“合法”的输入是[-1000,1000]区间内的实数,并且最多精确到小数点后2位.当你计算平均值的时候, ...
- PAT 乙级 1047 编程团体赛(20) C++版
1047. 编程团体赛(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 编程团体赛的规则为:每个参赛队由若 ...
- PAT 乙级 1029 旧键盘(20) C++版
1029. 旧键盘(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 旧键盘上坏了几个键,于是在敲一段文字的 ...
- PAT 乙级 1043 输出PATest(20) C++版
1043. 输出PATest(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个长度不超过10000 ...
随机推荐
- PowerDesigner15 增加Domain域
第一步: 第二步: 点击此按钮,在弹出框中对Domain域打钩即可
- HTTP/2及HTTP/3特性
HTTP/2及HTTP/3特性 摘要: 学习 HTTP/2 与 HTTP/3. 前言 HTTP/2 相比于 HTTP/1,可以说是大幅度提高了网页的性能,只需要升级到该协议就可以减少很多之前需要做的性 ...
- eclipse + cdt
Window > Preferences > General > Appearance中设置主题颜色. Help > eclipse marketplace > find ...
- 安装,配置,启动FTP,SSH,NFS服务
1.安装,配置,启动FTP服务 sudo apt-get install vsftpd 修改vsftpd的配置文件/etx/vsftpd/.config,将下面几行前面的“#”去掉 #local_en ...
- python-基础-文件
一.文件操作 打开文件时,需要指定文件路径和以何等方式打开文件, 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 打开文件的模式有: r ,只读模式[默认模式 ...
- 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 ...
- oracle完全之dbf文件出现问题, ORA-01219
alter database datafile '/data/app/oradata/ora237/users01.dbf' offline drop; 强制删除该故障文件
- Server 2008 R2部署active directory服务器-ad域
一.部署AD域: 系统环境:Windows server 2008 R2 标准版 ip:172.16.1.149服务器开机后会自动弹出一个初始配置任务窗口(这个不要随便关闭,因为我不知道关闭后能在哪里 ...
- redis使用问题总结
1.redis使用过多内存导致其他进程无法正常运行情况: 解决方案:限制redis的最大使用内存,修改redis.conf中的maxmemory(一般不要超过空闲内存的3/5,如果不设置ma ...
- BackgroundWorker Class Sample for Beginners
Download source - 27.27 KB Introduction This article presents a novice .NET developer to develop a m ...