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,最小:若给定个数为奇数时, ...
随机推荐
- 关于Unity的组件和作用
一.Transform组件 整个场景由节点树组成. 节点+Transform组件,每个Transform有自己的孩子Transform,由Transform组成Transform树,而每个Transf ...
- 察看下列JSP内容
察看下列JSP内容 <html><body> <% for (int i=0;i<3;i++){ %> out.print(i*2); <% } %&g ...
- 最新Android面试题集锦
近期由于某些原因想换工作,整理一下个人认为面试中还比較值得记录的一些题目,给须要找这方面工作的人一个借鉴. 下面基本仅仅记录题目或者大概答案,假设大家有比較具体的解答或者比較好的面试题木,希望各位看到 ...
- GitHub Permission to <<repository>> denied to <<username>>
I kept receiving a 403 error saying my usual username couldn’t access the repository with my usual a ...
- JSON美化输出
echo '{"a": 1, "b": 2}' | python -m json.tool 转自: http://blog.csdn.net/chosen0ne ...
- 360破解大赛crackme分析--之3DES解密附加数据
具体的分析这里有.本人仅仅是对这里面有趣的算法进行了一些学习 分析链接 这次是逆向的使用3DES解密的过程中的内容: 使用微软的crypt库 使用3DES解密程序中的附加数据 代码: VOID enc ...
- char[]与TCHAR[]互相转换引发的一个问题!
软件的一个驱动由于开发的年代比较久一些,使用的是非Unicode编码,而当前新的软件使用的是Unicode编码,于是将非Unicode驱动用于Unicode软件上时,就出现了问题! 问题就出现在非 ...
- Android动态禁用或开启屏幕旋转工具
package com.gwtsz.gts2.util; import android.content.Context; import android.provider.Settings; impor ...
- Python 正则表达式贪婪模式
贪婪模式也就是我们使用 .* 匹配任意字符时会尽可能长地向后匹配,如果我们想阻止这种贪婪模式,需要加个问号,尽可能少地匹配,如下例子: In []: import re In []: html = ' ...
- iOS 7 SDK: 如何使用后台获取(Background Fetch)
本文转载至 http://www.cocoachina.com/applenews/devnews/2013/1114/7350.html 本文主要教你如何使用iOS 7 SDK多任务处理API--B ...