PAT 1108 Finding Average [难]
1108 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
题目大意:给出n个数,可能不是合法的,给出那些合法的数的平均值。
//我的天哪,这个题也太难了吧,难死我了,改了好多遍我为什么就是写不对?越改通过的测试点越少。。绝望。
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stdio.h>
#include <queue>
#include<cmath>
#include <vector>
#include<set>
using namespace std; //float toNum(string s){
// //有小数怎么办?有点绝望,好像没有做过这样的题目。
// int pos=s.find(".");
// float num=0;
// for(int i=0;i<pos;i++){
// num=num*10+(s[i]-'0');
// n=atof()
// }
//
//} int main()
{
int n;
cin>>n;
string s;
vector<float> legal;
vector<string> nolegal;
for(int i=; i<n; i++)
{
cin>>s;
bool flag=true;
for(int j=; j<s.size(); j++) //判断是否是字母
{
if(!(s[j]>=''||s[j]<='')||s[j]!='.'||!s[j]!='-')//只能是这几种。
{
flag=false;
break;//比如2..0这样的算是合法数字吗?这就很尴尬,我感觉不是。
}
if((s[j]=='-'&&j)|(s[j]=='-'&&j==&&s.size()==)){//加上这个,并没有通过1和4测试点。
flag=false;break;
}
}
if(s.find(".")!=string::npos)
{
int pos=s.find(".");
if(s.size()->||pos==)//如果小数点出现在第一个也是不合法的,加上这个之后还是没通过14测试点。
{
flag=false;
}
if(s.find(".",pos+)!=string::npos)
{
flag=false;
}
}
float numf;
if(flag)
{
numf=atof(s.c_str());
if(numf>1000.0||numf<-1000.0)
{
nolegal.push_back(s);
}
else
{
legal.push_back(numf);
}
}
else
{
nolegal.push_back(s);
} }
for(int i=; i<nolegal.size(); i++)
{
printf("ERROR: %s is not a legal number\n",nolegal[i].c_str());
}
if(legal.size()==)
{
printf("The average of 0 numbers is Undefined");
}
else if(legal.size()==)
{
printf("The average of 1 number is %.2f",legal[]);
}
else
{
float sum=;
int size=legal.size();
for(int i=; i<size; i++)
sum+=legal[i];
printf("The average of %d number is %.2f",size,sum/size);
}
return ;
}
//1,没有判断-是否在首位,如:1-6就视为合法了。。
//2.只有一个.时,是不合法的。。
//3.测试了-,发现自己的代码认为是合法的。。。绝望,好复杂。。。
//4.发现了要给十分严重的问题,就是如果它的非法输入不一定非得是字母。。。
//我也不知道这是为什么通不过。。。哭辽。
学习了柳神的代码:

参考:https://www.cnblogs.com/lanjianhappy/p/6861728.html
sscanf(输入的字符串,格式,输出到);
scanf是从键盘输入,而sscanf读入的是第一个参数,是个字符串。
发现这里第三个参数根本就没有类型限制!!!
Int sscanf( string str, string fmt, mixed var1, mixed var2 ... );
int scanf( const char *format [,argument]... );
char buf[] = ;
sscanf("123456 ", "%s", buf);
printf("%s/n", buf);
结果为:
sscanf("123456 ", "%4s", buf);
printf("%s/n", buf);
结果为:
sscanf("123456abcdedfBCDEF", "%[1-9a-z]", buf);
printf("%s/n", buf);
结果为:123456abcdedf
就是类似于正则表达式!
sprinf()学习:http://www.runoob.com/cprogramming/c-function-sprintf.html
C 库函数 int sprintf(char *str, const char *format, ...) 发送格式化输出到 str 所指向的字符串。
#include <stdio.h>
#include <math.h> int main()
{
char str[]; sprintf(str, "Pi 的值 = %f", M_PI);
puts(str); return();
} Pi 的值 = 3.141593
应该是从后往前看,先将第三个参数第二个参数的格式赋值,然后再整体赋值给第一个参数。
当然第一个参数和上一个函数一样都是字符串。
int main()
{
string s;
char a[];
float temp;
cin>>s;
sscanf(s.c_str(),"%f",&temp);
sprintf(a,"%.2f",temp);
cout<<s<<'\n';
cout<<a<<'\n';
cout<<temp;
return ;
}

学习了!
柳神的代码真的是太厉害了。

多复习!
PAT 1108 Finding Average [难]的更多相关文章
- pat 1108 Finding Average(20 分)
1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...
- PAT 1108 Finding Average
The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...
- 1108 Finding Average (20 分)
1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...
- PAT (Advanced Level) 1108. Finding Average (20)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲题题解-1108. Finding Average (20)-字符串处理
求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...
- PAT Advanced 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甲级——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,最小:若给定个数为奇数时, ...
随机推荐
- HeadFirst jsp 08 无脚本JSP
web页面设计人员真的必须懂 java ? web页面人员可以很快学习 EL 语言. 目前不知道 EL 应用前景如何, 但是我们香港系统没有使用 EL. include 指令 include指令告诉容 ...
- python 处理抓取网页乱码
python 处理抓取网页乱码问题一招鲜 相信用python的人一定在抓取网页时,被编码问题弄晕过一阵 前几天写了一个测试网页的小脚本,并查找是否包含指定的信息. 在html = urllib2. ...
- jQuery 插件开发指南
jQuery凭借其简洁的API,对DOM强大的操控性,易扩展性越来越受到web开发人员的喜爱,经常有人询问一些技巧,因此干脆写这么一篇文章给各位jQuery爱好者,算是抛砖引玉吧. 那么首先我们来简单 ...
- PowerShell----Automatic_Variables(预定义变量)
以下这些变量是由powershell创建和维护的.ls Variable: 可以获取到所有默认的变量, 每个版本的Powershell可能有差异 $$包含会话所收到的最后一行中的最后一个令牌. $? ...
- 转载 --iOS实用小技巧(2)-生成txt文本
//不论是创建还是写入只需调用此段代码即可 如果文件未创建 会进行创建操作 - (void)writeToFileWithTxt:(NSString *)string{ dispatch_async( ...
- Java知识点梳理——集合
1.定义:Java集合类存放于java.util包,是存放对象的容器,长度可变,只能存放对象,可以存放不同的数据类型: 2.常用集合接口: a.Collection接口:最基本的集合接口,存储不唯一, ...
- forEach循环dom
大家都知道forEach是循环数组用的,而且很方便,可以丢掉for循环了,但是它不能循环Dom元素.其实我们可以利用call来完成forEach循环Dom; html结构: <ul class= ...
- FluentNhibernate 不支持存储过程
一直以为没有使用FN进行存储过程的操作,这次因为后台首页想统计下数据,就利用了存储过程,但在使用中却发现FN目前还不支持存储过程(点击查看官方),没有办法,只能利用Fluent Configurati ...
- hibernate中持久化对象的状态
持久化对象有以下几种状态: 临时对象(Transient): 在使用代理主键的情况下, OID 通常为 null 不处于 Session 的缓存中 在数据库中没有对应的记录 持久化对象(也叫”托管 ...
- 使用ganymed工具调用ssh2
需要引入ganymed-ssh2-build210.jar包. 其实很简单.所以直接贴代码,代码说话. package com.eshore.framework.util; import java.i ...