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;
}
 
 

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. 实验吧—Web——WP之 简单的sql注入之2

    直接打开解题连接: 既然是SQL注入,那么我们就要构造注入语句了,这个就要有耐心一个一个去尝试了 输入语句 1'and 1=1 # 和 1'and/**/1=1/**/#后 对比一下,发现是过滤掉了空 ...

  2. 浅谈log4j-2

    //配置日志输出的定义,主要有三点:1:输出什么级别的日志信息,2:将日志信息输出到那里,3:输出的日志以什么格式展示 public static void main(String[] args) { ...

  3. Mybatis(七)-- LRU LFU 算法

    这篇博客主要介绍LRU LFU 算法,因为在Mybatis的缓存中会用到,所以放到这个系列中了.此外,这是我翻译的一篇文章,觉得原文已经写的很好了,所以就直接翻译一下,留作知识整理. 英文原文出处如下 ...

  4. 【java多线程】java8的流操作api和fork/join框架

    原文:https://blog.csdn.net/u011001723/article/details/52794455/ 一.测试一个案例,说明java8的流操作是并行操作 1.代码 package ...

  5. Stateful Kubernetes Applications Made Easier: PSO and FlashBlade

    转自:https://medium.com/@joshua_robinson/stateful-kubernetes-applications-made-easier-pso-and-flashbla ...

  6. What are long running processes?

    转自:https://blog.bernd-ruecker.com/what-are-long-running-processes-b3ee769f0a27 Some communities have ...

  7. Gravitee.io Access Management docker-compose运行

    Gravitee.io 官方提供的docker-compose 快速运行的方式 默认ui 账户 admin adminadmin 环境准备 docker-compose 文件 # # Copyrigh ...

  8. Coding kata: get the top two teams in one group

    In this week, we did a coding kata, the subject is to select the top two teams of football group mat ...

  9. oracle单词

    OEM Blackouts n. 黑朦:灯火管制(blackout的复数)Projection n. 投射:规划:突出:发射:推测premium adj. 高价的:优质的 ############## ...

  10. [转]Spring中property-placeholder的使用与解析

    我们在基于spring开发应用的时候,一般都会将数据库的配置放置在properties文件中. 代码分析的时候,涉及的知识点概要: NamespaceHandler 解析xml配置文件中的自定义命名空 ...