PAT甲级-1108 Finding Average (20 分)
发布日期:2022-02-10 08:11:01
浏览次数:16
分类:技术文章
本文共 2920 字,大约阅读时间需要 9 分钟。
题目:
分析:模拟,判断是否为题目要求的合法序列。第一次好像是参考别人代码写的,第二次自己写的比较复杂
1、
#include#include #include #include #include #include #include #include #include
2、
#include#include #include #include #include using namespace std;int n,m,k;int main(){ cin>>n; int cnt = 0; double ans = 0; while(n--) { string s;cin>>s; int len = s.size(); int d = -1;//小数点在数组中的位置 int flag = 0;//判断是否合法 for(int i = 0 ; i < len ;i++) { if(s[i] == '.'){ d = i; break; } } if(d == -1)//没有小数点 { for(int i = 0; i < len;i++) { if(i == 0 && s[i]== '-')continue; if(s[i] < '0' || s[i] > '9'){ flag = 1; break; } } if(!flag && stod(s) >= -1000 && stod(s) <= 1000){ ans += stod(s); cnt++; } else printf("ERROR: %s is not a legal number\n",s.c_str()); } else//有小数点 { for(int i = 0; i < d;i++) { if(i == 0 && s[i]== '-')continue; if(s[i] < '0' || s[i] > '9'){ flag = 1; break; } } if(len - d - 1 > 2)//小数点位数 flag = 1; for(int i = d + 1; i < len ;i++) { if(s[i] < '0' || s[i] > '9'){ flag = 1; break; } } if(!flag && stod(s) >= -1000 && stod(s) <= 1000){ ans += stod(s); cnt++; } else printf("ERROR: %s is not a legal number\n",s.c_str()); } } if(cnt == 1) printf("The average of %d number is %.2f",cnt,ans); else if(cnt == 0) printf("The average of 0 numbers is Undefined"); else printf("The average of %d numbers is %.2f",cnt,ans/cnt); return 0;}
转载地址:https://blog.csdn.net/qq_43567222/article/details/113846730 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月12日 10时56分58秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
[YoLoV3目标检测实战] keras+yolov3训练自身口罩检测数据集
2019-04-26
[实战]200类鸟类细粒度图像分类
2019-04-26
【实战】英文垃圾短信分类
2019-04-26
FFmpeg初遇见_基本命令
2019-04-26
Urbansound8k声音分类深度学习实战
2019-04-26
pytorch版本下的yolov3训练实现火焰检测
2019-04-26
yolov4训练自己的数据集实现安全帽佩戴检测
2019-04-26
EfficientDet训练自己的数据集实现抽烟检测
2019-04-26
【工具篇】10分钟快速上手git与github
2019-04-26
【开发篇】10分钟快速上手spring boot
2019-04-26
【开发篇】10分钟快速spring boot+react前后端分离
2019-04-26
【开发篇】10分钟快速上手spring boot+mybatis增删改查
2019-04-26
【Leetcode刷题篇】leetcode203 移除链表元素
2019-04-26
【Leetcode刷题篇】leetcode108 将有序数组转换为二叉搜索树
2019-04-26
【Leetcode刷题篇】leetcoe109 有序链表转换二叉搜索树
2019-04-26
【Leetcode刷题篇】leetcode938 二叉搜索树的范围和
2019-04-26
【Leetcode刷题篇】leetcode235 二叉搜索树的最近公共祖先
2019-04-26
【Leetcode刷题篇】leetcode236 二叉树的最近公共祖先
2019-04-26
【Leetcode刷题篇】leetcode230 二叉搜索树中第K小的元素
2019-04-26
【Leetcode刷题篇】leetcode173 二叉搜索树迭代器
2019-04-26