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 [难]的更多相关文章

  1. pat 1108 Finding Average(20 分)

    1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calcu ...

  2. PAT 1108 Finding Average

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  3. 1108 Finding Average (20 分)

    1108 Finding Average (20 分) The basic task is simple: given N real numbers, you are supposed to calc ...

  4. PAT (Advanced Level) 1108. Finding Average (20)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  5. PAT甲题题解-1108. Finding Average (20)-字符串处理

    求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include < ...

  6. PAT Advanced 1108 Finding Average (20 分)

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  7. 【PAT甲级】1108 Finding Average (20分)

    题意: 输入一个正整数N(<=100),接着输入一行N组字符串,表示一个数字,如果这个数字大于1000或者小于1000或者小数点后超过两位或者压根不是数字均为非法,计算合法数字的平均数. tri ...

  8. PAT甲级——1108.Finding Average (20分)

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  9. Day 007:PAT训练--1108 Finding Average (20 分)

    话不多说: 该题要求将给定的所有数分为两类,其中这两类的个数差距最小,且这两类分别的和差距最大. 可以发现,针对第一个要求,个数差距最小,当给定个数为偶数时,二分即差距为0,最小:若给定个数为奇数时, ...

随机推荐

  1. 32Mybatis_mybatis逆向工程自动生成代码

    实际工作中要用的.很重要! mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml ...

  2. 【BZOJ】1625: [Usaco2007 Dec]宝石手镯(01背包)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1625 太水了. #include <cstdio> #include <cstri ...

  3. 如何通过Keil将程序正确的下载进flash中

    前面介绍了一些创建工程和调试的基本步骤,在这里准备介绍一下如何正确的将Keil程序在仿真调试中下载到flash.这里再次涉及到了debug的窗口.   工具/原料   Keil uVision 4/5 ...

  4. fopen()函数文件模板中w,w+,a,a+的区别

    "w" 写入方式打开,将文件指针指向文件头并将文件大小截为零.如果文件不存在则尝试创建之. "w+" 读写方式打开,将文件指针指向文件头并将文件大小截为零.如果 ...

  5. [NOI2008] 志愿者招募[流量平衡]

    288. [NOI2008] 志愿者招募 ★★★★   输入文件:employee.in   输出文件:employee.out   简单对比时间限制:2 s   内存限制:512 MB [问题描述] ...

  6. github 's usage

    author:headsen  chen date: 2018-05-30   10:50:56 notice:This  article is created by headsen chen him ...

  7. oracle11g安装完成后修改字符集

    author : headsen chen date:2018-05-10  10:27:16 oracle11g完成安装后,由于默认安装的时候无法指定字符集,所以手动修改字符集和10g版本一样的字符 ...

  8. 第一个MapReduce的例子

    第一个MapReduce的例子 Hadoop Guide的第一个MapReduce的例子是处理气象数据的(数据来源ncdc),终于跑通了.总结一下步骤,安装hadoop不在本文中介绍 1 数据预处理 ...

  9. 初级Java面试题 - JavaSE篇

    p{font-size:18px;} li{font-size:18px;} 加入我的QQ群(701974765) 获取更多好用又好玩的软件,还有不定期发放的福利呦(- ̄▽ ̄)- Java基本数据类型 ...

  10. jQuery控制checkbox选中状态但是不显示选中

    问题描述:使用jQuery来控制checkbox的选中状态,但是第一次点击出现选中样式,之后点击可以看到checked的属性增加成功但是并没有选 中状态. 问题代码: function chooseA ...