PAT A1108 Finding Average (20 分)——字符串,字符串转数字
The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−1000,1000] and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then N numbers are given in the next line, separated by one space.
Output Specification:
For each illegal input number, print in a line ERROR: X is not a legal number
where X
is the input. Then finally print in a line the result: The average of K numbers is Y
where K
is the number of legal inputs and Y
is their average, accurate to 2 decimal places. In case the average cannot be calculated, output Undefined
instead of Y
. In case K
is only 1, output The average of 1 number is Y
instead.
Sample Input 1:
7
5 -3.2 aaa 9999 2.3.4 7.123 2.35
Sample Output 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
Sample Input 2:
2
aaa -9999
Sample Output 2:
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined
#include <stdio.h>
#include <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
const int maxn = ;
int n;
double res=;
bool isint(char c){
if(c>='' && c<='')return true;
else return false;
}
bool isnum(string s){
res=;
if(s[]!='-' && s[]!='+' && (s[]<'' || s[]>'')) return false;
int flag=;
if(s[]=='-'){
flag=-;
s.erase(,);
}
else if(s[]=='+'){
s.erase(,);
}
int fp=,np=;
for(int i=;i<s.length();i++){
if(isint(s[i])){
res=res*+s[i]-'';
}
else if(s[i]=='.' && np==){
np++;
fp=i;
}
else{
flag=;
break;
}
}
if(flag==)return false;
if(np==){
int xiao = s.length()-fp-;
if(xiao>) return false;
res = res / pow(,xiao);
}
if(res>) return false;
//printf("%f %d\n",res,flag);
res = res*flag;
//printf("%f %d\n",res,flag);
return true;
} int main(){
scanf("%d",&n);
int cnt=;
double total=;
for(int i=;i<=n;i++){
string s;
cin>>s;
//res=0;
if(isnum(s)){
cnt++;
total = total + res;
//printf("cnt: %d total: %f\n",cnt,total);
}
else{
printf("ERROR: %s is not a legal number\n",s.c_str());
}
}
if(cnt==)printf("The average of 0 numbers is Undefined\n");
else if(cnt!=)printf("The average of %d numbers is %.2f\n",cnt,total/cnt);
else printf("The average of %d number is %.2f\n",cnt,total/cnt);
}
注意点:字符串转数字的判断,按条件一个个判断就好了,注意输入输出时的double要写对。测试点3是只有一个数的情况,1个数时number没有s,没注意所以错了
PAT A1108 Finding Average (20 分)——字符串,字符串转数字的更多相关文章
- PAT甲级——1108.Finding Average (20分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- 【PAT甲级】1108 Finding Average (20分)
题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...
- PAT Advanced 1108 Finding Average (20 分)
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- Day 007:PAT训练--1108 Finding Average (20 分)
话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...
- 【刷题-PAT】A1108 Finding Average (20 分)
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- pat 1108 Finding Average(20 分)
1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...
- PTA 说反话-加强版(20 分)(字符串处理)
说反话-加强版(20 分) 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过500 000的字符串.字符串由若干单词和若干空 ...
- PAT 1108 Finding Average [难]
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Person ...
随机推荐
- 【Redis】5、Redis事务处理
MULTI .EXEC .DISCARD 和WATCH 是 Redis 事务的基础 1.MULTI 命令用于开启一个事务,它总是返回 OK .MULTI 执行之后,客户端可以继续向服务器发送任意多条 ...
- Matlab diag的用法
X = diag(v,k) 以向量v的元素作为矩阵X的第k条对角线元素,当k=0时,v为X的主对角线:当k>0时,v为上方第k条对角线 几个例子: 当k> v=[1 2 3]; >& ...
- 在UWP中实现自己的MVVM设计模式
其实写这篇博文的时候我是拒绝的,因为这牵扯到一个高大上的东西——"框架".一说起这个东西,很多朋友就感觉有点蒙了,尤其是编程新手.因为它不像在代码里面定义一个变量那么显而易见,它是 ...
- 网页三剑客:HTML+CSS+JavaScript 之 HTML的概述
HTML(超文本标记语言) 什么是HTML? HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言: HyperText Markup Language HTML 不是一种编程语言,而 ...
- 对GIL的一些理解
GIL:全局解释器锁 GIL设计理念与限制: python的代码执行由python虚拟机(也叫解释器主循环,CPython版本)来控制,python在设计之初就考虑到在解释器的主循环中,同时只有一个线 ...
- opencv学习网站[国外网站]+ 各种学习资料
1.Learn OpenCV 使用C++和python和opencv结合编写的教程,上面有丰富的小实验,并且有完整的源码. 2.pyimagesearch python+opencv编写的实例教程,有 ...
- Ubuntu 中卸载软件的几种命令
1.在终端里 apt-get安装的软件:安装软件sudo apt-get install softname1 softname2softname3--卸载软件 sudo apt-get remove ...
- Nodejs 安装 on centos7
本文演示如何在CentOS7上安装Nodejs. 1 准备工作 1.1 浏览器访问安装包下载地址: https://nodejs.org/dist/ 找到需要安装的版本,以8.11.3版本为例,地址为 ...
- 洗礼灵魂,修炼python(9)--灵性的字符串
python几大核心之——字符串 1.什么是字符串 其实前面说到数据类型时说过了,就是带有引号的参数,“”引号内的一切东西就是字符串,字符串又叫文本. 2.创建字符串的两种方式: 3.字符串的方法: ...
- CRM 各种类型字段的 赋值 取值
//lookupRecordEntity["new_lead"] = new EntityReference(entity.LogicalName, entity.Id); 获取时 ...