顽强的小白
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
题目解析
处理字符串的问题,只要合法的数字。
思路:
- 首先判断字符串里面是不是只有数字、负号和小数点
- 再来判断是不是有小数点,是不是只有一个
- 如果有小数点,判断小数点后是不是不超过两位,同时要把字符串转换成数字
- 最后判断数字是不是在给定范围内
注意点:
- 小数点可以在最后,例如 89.
- 没有 ‘+’号的例子
- 注意只有一个合法数字的输出是单数的 number 但是仍然要用补齐小数点后两位的方法输出
- 注意没有合法数字的输出
代码实现
#include <cstdio>
#include <iostream> //它和下面这个一般是配套使用
#include <string> //为了用 string 的 find 函数 没有用字符串
#include <algorithm>
using namespace std;
const int maxn=105;
double sum=0;
int cnt=0;
bool judge(string now){
for(int i=0;i<now.size();++i){ //先判断是不是只有合法的元素,过滤掉字母
if((now[i]<='9'&&now[i]>='0')||now[i]=='-'||now[i]=='.'){
continue;
}else return false;
}
int i=0;
if(now[0]=='-') i++;
int front=0,rear=0,pos;
double num=0;
if(now.find('.')!=string::npos ){ //找到一个小数点
pos=now.find('.',i);
if(now.find('.',pos+1)!=string::npos){ //不止包含一个小数点
return false;
}
if(now.size()-1-pos>2) return false; //判断小数点后的位数
for(int j=i;j<pos;++j){
front*=10;
front+=now[j]-'0';
}
for(int j=pos+1;j<now.size();++j){
rear*=10;
rear+=now[j]-'0';
}
if(now.size()-1-pos==1){
num=front+rear/10.0;
}else if(now.size()-1-pos==2){
num=front+rear/100.0;
}else if(now.size()-1-pos==0){
num=front+rear;
}
}else{ //如果没有小数点,整数
for(int j=i;j<now.size();++j){
num*=10;
num+=now[j]-'0';
}
}
if(now[0]=='-') num=-num;
if(num<=1000&&num>=-1000){
//printf("%.2lf\n",num);
sum+=num;
cnt++;
return true;
}
else
return false;
}
int main(){
int n;
scanf("%d",&n);
string now;
for(int i=0;i<n;++i){
cin>>now;
if(judge(now)==false){
cout<<"ERROR: "<<now<<" is not a legal number"<<endl;
}
}
if(cnt>1)
printf("The average of %d numbers is %.2lf\n",cnt,sum/cnt);
else if(cnt==1){
printf("The average of 1 number is %.2lf\n",sum);
}else
printf("The average of 0 numbers is Undefined\n");
return 0;
}