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个数字串,统计出其中合法的数字串的平均值。
合法的数字串满足的条件
- 不包含除了数字和"-"以及"."以外的字符
- 小数点的个数不能大于1个,并且小数点可以出现在数字串末尾,但不能出现在数字串首
- "-"只能出现在字符串首位
- 对于浮点数,它的小数位不能超过2位
- 数字的大小在[-1000,1000]之内
注意事项:
1.
若最终求出的合法数字个数为1时,输出结果中的number不加s,若个数为其他数时应加s
例如合法数字个数为3时
The average of 3 numbers is Y
合法数字为1时
The average of 3 number is Y
方法一:
#include<bits/stdc++.h>
using namespace std;
int main()
{
string t;
int n,i,num,d,j,flag,numd;
double sum=0;
cin>>n;
num=0; // 有效数字的个数
for(i=1; i<=n; i++)
{
cin>>t; //输入字符串
numd=0; //记录小数点的个数
flag=1; //记录有效数字的正负
d=t.length();
//判断字符串是否含有非法字符
for(j=0; j<t.length(); j++)
{
if(j==0&&t[j]=='-')
{
flag=-1;
continue;
}
else if(t[j]=='.')
{
d=j; //记录小数点的位置
numd++;
}
else if(t[j]<'0'||t[j]>'9')
{
break;
}
}
//若字符串首为小数点 则不合法
if(t[0]=='.')
{
cout<<"ERROR: "<<t<<" is not a legal number"<<endl;
continue;
}
//若存在非法字符,或者小数点个数大于1
if(j!=t.length()||numd>=2)
{
cout<<"ERROR: "<<t<<" is not a legal number"<<endl;
continue;
}
//计算小数位个数
int xiao = t.length()-1-d;
//如果小数位大于2则不合法
if(xiao>2)
{
cout<<"ERROR: "<<t<<" is not a legal number"<<endl;
}
else
{
double ss = 0;
for(j=0; j<t.length(); j++)
{
if(t[j]!='.'&&t[j]!='-')
{
ss=ss*10+t[j]-'0';
}
}
if(d!=t.length())
{
ss=ss*flag/pow(10,xiao); //转为小数
}
else
{
ss=ss*flag;
}
if(ss>=-1000&&ss<=1000)
{
sum+=ss;
num++; //合法数字加一
}
else
{
cout<<"ERROR: "<<t<<" is not a legal number"<<endl;
}
}
}
if(num==0)
{
printf("The average of 0 numbers is Undefined\n");
}
else if(num==1)
{
printf("The average of %d number is %.2lf\n",num,sum/num);
}
else
{
printf("The average of %d numbers is %.2lf\n",num,sum/num);
}
return 0;
}
方法二:
使用下列两个函数可以快速解决这道题
sscanf() – 从一个字符串中读进与指定格式相符的数据
例如sscanf(a, "%lf", &temp);
它的作用是将字符串a中的符合浮点数格式的部分提取出来放到字符串temp中
例如a为1.23.14
前面的1.23符合浮点数格式。而后面的.14由于前面已经出现过小数点了,因此被判定为不符合浮点数格式,因此.14被丢弃
最终temp="1.12"
sprintf() – 字符串格式化命令,主要功能是把格式化的数据写入某个字符串中
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main() {
int n, cnt = 0;
char a[50], b[50];
double temp = 0.0, sum = 0.0;
cin >> n;
for(int i = 0; i < n; i++) {
scanf("%s", a);
sscanf(a, "%lf", &temp);
sprintf(b, "%.2f",temp);
int flag = 0;
for(int j = 0; j < strlen(a); j++)
if(a[j] != b[j]) flag = 1;
if(flag || temp < -1000 || temp > 1000) {
printf("ERROR: %s is not a legal number\n", a);
continue;
} else {
sum += temp;
cnt++;
}
}
if(cnt == 1)
printf("The average of 1 number is %.2f", sum);
else if(cnt > 1)
printf("The average of %d numbers is %.2f", cnt, sum / cnt);
else
printf("The average of 0 numbers is Undefined");
return 0;
}
本文介绍如何编写代码来计算给定的一组数字的平均值,同时忽略非法输入如非数字字符、超出范围或格式错误的数值。通过示例展示了如何处理小数点、负号和范围限制,确保结果准确到两位小数。
480

被折叠的 条评论
为什么被折叠?



