1108 Finding Average (20 分)【难度: 简单 / 模拟】
模拟 简单 20 难度 average
2023-09-11 14:15:52 时间
https://pintia.cn/problem-sets/994805342720868352/problems/994805360777347072
#include<bits/stdc++.h>
using namespace std;
int n,cnt;
double sum;
string s;
bool solve(string s)
{
int t=0;
string temp=s;
if(temp[0]=='-') temp=temp.substr(1);
if(!temp.size()) return false;
for(int i=0;i<temp.size();i++)
{
if(!isdigit(temp[i])&&temp[i]!='.') return false;//有其他的字符
if(temp[i]=='.') t++;
}
if(t>1) return false;//小数点个数大于1
if(t&&s.size()-s.find('.')-1>2) return false;//精度大于2
double a=stod(s);
if(a<-1000||a>1000) return false;
cnt++,sum+=a;
return true;
}
int main(void)
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>s;
if(!solve(s)) printf("ERROR: %s is not a legal number\n",s.c_str());
}
if(!cnt) printf("The average of 0 numbers is Undefined");
else if(cnt==1) printf("The average of 1 number is %.2lf",sum);
else printf("The average of %d numbers is %.2lf",cnt,sum/cnt);
return 0;
}
相关文章
- scrapy 模拟登陆
- 模拟BS服务器案例
- Java 使用Robot类模拟鼠标、键盘事件
- Go 数组模拟单项队列
- 1109 Group Photo (25 分)【难度: 一般 / 知识点: 模拟 排序】
- 1104 Sum of Number Segments (20 分)【难度: 一般 / 模拟 找规律】
- 1100 Mars Numbers (20 分)【难度: 一般 / 知识点: 模拟】
- 1058 A+B in Hogwarts (20 分)【难度: 简单 / 知识点: 模拟】
- 1050 String Subtraction (20 分)【难度: 简单 / 知识点:模拟】
- 1005 Spell It Right (20 分)【难度: 简单 / 知识点: 模拟】
- 08 线程状态 五大状态 流程图 线程方法 停止线程 代码 线程休眠 代码:模拟网络延时 代码:打印当前系统时间
- 【Push Kit】模拟服务端发送消息至客户端,测试消息发送功能(华为推送服务)
- HDU 5912 Fraction (模拟)
- Charles模拟数据
- Java反射及依赖注入简单模拟
- Qt编写的项目作品34-雷达模拟仿真工具(雨田哥作品)
- [数据结构]数组模拟队列操作
- Fiddler-008-简单模拟性能测试
- HTML-003-模拟IDE代码展开收起功能简单示例
- 简单模拟Spring的注入
- nyoj 33-蛇形填数 (循环,模拟)
- iOS开发 - 模拟苹果通讯录