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,最小:若给定个数为奇数时, ...
随机推荐
- Httpclient远程调用WebService示例(Eclipse+httpclient)
package cn.com.taiji.pos.httpserver; import java.io.BufferedInputStream;import java.io.ByteArrayOutp ...
- DP - 字符混编
字符混编 Problem's Link ---------------------------------------------------------------------------- Mea ...
- KMP + 求最小循环节 --- POJ 2406 Power Strings
Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...
- php 转码
//$names = iconv("UTF-8", "gb2312", $name); //等同于javascript encodeURI("电影&q ...
- MD5的各种实现——也是醉了
MD5即Message-Digest Algorithm 5(信息-摘要算法5).用于确保信息传输完整一致. 是计算机广泛使用的杂凑算法之中的一个(又译摘要算法.哈希算法),主流编程语言普遍已有MD5 ...
- LoadRunner学习---脚本编写(4)(比较重要)
今天接着来翻译http://www.wilsonmar.com/中关于LoadRunner脚本编写部分,下面该翻译脚本编写中一些比较重要的部分了. Web用户Action 在VuGen中,脚本产生的默 ...
- C语言跳出循环
使用while或for循环时,如果想提前结束循环(在不满足结束条件的情况下结束循环),可以使用break或continue关键字. break关键字 在<C语言switch语句>一节中,我 ...
- Hibernate的多对多映射关系
example: 老师(teacher)和学生(Student)就是一个多对多的关系吧?老师可以有多个学生,学生也可以由多个老师,那在Hibernate中多对多是怎样实现的呢?? 在Hibernate ...
- Code Forces 22B Bargaining Table
B. Bargaining Table time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- java面试基础题------》Java 中的父子类静态代码块,代码块,构造方法执行顺序
4.指出下面程序的运行结果. class A { static { System.out.print("1"); } public A() { System.out.print(& ...