PAT 甲级 1108 Finding Average (20 分)
思路:
1.不是数字的位置只能在第一个且为负号,或者在字符串的倒数第一、二、三位置、或者不存在;
2.上面的方法也可以用sscanf
或者sprintf
进行判定;
3.数字要在规定区间内;
4.按.2f
输出就好了;
代码:
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
bool isLegal(string s){int pos=s.find('.'),len=s.length();if(pos==len-1||pos==len-2||pos==len-3||pos==string::npos){for(int i=0;i<len;i++)if(!isdigit(s[i])&&i!=pos&&(i||s[i]!='-')) return false;double n=stod(s);if(n>1000||n<-1000) return false;return true;}return false;
}
int main(){int n,cnt=0;double sum=0;cin>>n;for(int i=0;i<n;i++){string s;cin>>s;if(!isLegal(s)) printf("ERROR: %s is not a legal number\n",s.c_str());else{sum+=stod(s);cnt++;}}if(cnt>1) printf("The average of %d numbers is %.2f",cnt,sum/(cnt*1.0));else if(cnt==1) printf("The average of 1 number is %.2f",sum);else printf("The average of 0 numbers is Undefined");return 0;
}
PAT 甲级 1108 Finding Average (20 分)相关推荐
- PAT甲级1108 Finding Average :[C++题解]stof、字符串变成浮点数、try和catch捕获异常、C++语法题
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: C++中有函数stoi表示把string 变成int,还有函数stof,表示把string变成float.如果是合法数字的话,sto ...
- PAT甲级 -- 1050 String Subtraction (20 分)
Given two strings S1 and S2, S=S1−S2 is defined to be the remaining string after taking ...
- 1108. Finding Average (20)-PAT甲级真题
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- 【PAT甲级】11077 Kuchiguse (20 分) Java
题目 Java题解(最后一个测试用例没通过) import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...
- PAT甲级 -- 1041 Be Unique (20 分)
Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...
- C++ PAT甲级 1050 String Subtraction (20分)
Given two strings S1 and S2, S=S1−S2is defined to be the remaining string aftertaking all the cha ...
- PAT甲级1100 Mars Numbers (20 分)题解
\quad这个题稍微有点麻烦,需要分别处理数字转火星文和火星文转数字两种情况.不过数字最高两位,处理起来分别讨论即可.程序如下: #include <iostream> using nam ...
- PAT甲级1103 Integer Factorization (30 分):[C++题解]背包问题,DP解法
文章目录 题目分析 题目链接 题目分析 分析 把N(样例中N=169)看成背包的体积:把k(样例中k=5)看成背包能承的重量.把这道题转化为二维完全背包问题.由于数据范围给出的次幂P∈[2,7],那么 ...
- PAT甲级1072 Gas Station (30 分):[C++题解]dijkstra算法、最短路
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 所有的dist[ ]都≤Ds:最小的dist[ ]最大; dist[ ] 总和最大. 由于加油站是字符,为了简单起见,将m个加油站编 ...
最新文章
- phaser.min.js_如何使用Phaser 3,Express和Socket.IO构建多人纸牌游戏
- BUUCTF Dig the way
- 关于类与对象操作的细节与注意事项(c++细节篇三)
- 【推荐】BREW中 - 显示和图像
- js中解析json字符串
- OJ1070: 小汽车的位置(C语言)
- python中回归拟合图_seaborn库:线性回归拟合图
- echarts年龄饼图_解决echarts饼图显示百分比,和显示内容字体及大小
- 掌握 Ajax,第 1 部分: Ajax 入门简介
- 设计模式笔记之二(工厂模式)
- java基础——自动装箱与拆箱
- 知乎高赞的学习网站,建议收藏
- 我为什么读博, 以及我为什么不读博?这是个问题!
- 去除取消WPS的广告推送、WPS热点以及推荐软件等骚扰功能
- cocos2d-x lua 框架中 self.super.ctor(self, app) 和 self.super:ctor(app) 的区别
- java 动态线程池_线程池的参数动态调整
- 【论文精读】A Survey on Deep Learning for Named Entity Recognition
- VR全景拍摄怎么设置相机
- log4cpp源码阅读:Appender组件学习
- 使用正则批量修改文件名
热门文章
- csrf漏洞防御方案_CSRF原理实战及防御手段
- adobe android 动画,Lottie - Android 动画详解
- IntelliJ IDEA 中如何查看一个类的所有继承关系,包括父类与子类
- u盘文件变成快捷方式怎么恢复,恢复U盘文件的五种方法
- 程序员直男也有的浪漫,快来给朋友制作一款定时微信推送服务吧
- 用计算机亩换算成平方,亩数和平方换算(平方米换算亩计算器)
- 【经验】不摸鱼的时光
- ubuntu QQ安装 网易云音乐 rar文件 截屏软件Shutter 图片编辑pinta 文字软件typora 视频播放器smplayer
- MyBatis-Plus DQL与其他知识点
- 爱德华索普与西蒙斯:量化投资的那些传奇们