1054 求平均值 (20)(20 分)
本题的基本要求非常简单:给定N个实数,计算它们的平均值。但复杂的是有些输入数据可能是非法的。一个“合法”的输入是[-1000,1000]区间内的实数,并且最多精确到小数点后2位。当你计算平均值的时候,不能把那些非法的数据算在内。
输入格式:
输入第一行给出正整数N(<=100)。随后一行给出N个实数,数字间以一个空格分隔。
输出格式:
对每个非法输入,在一行中输出“ERROR: X is not a legal number”,其中X是输入。最后在一行中输出结果:“The average of K numbers is Y”,其中K是合法输入的个数,Y是它们的平均值,精确到小数点后2位。如果平均值无法计算,则用“Undefined”替换Y。如果K为1,则输出“The average of 1 number is Y”。
输入样例1:
7
5 -3.2 aaa 9999 2.3.4 7.123 2.35
输出样例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
输入样例2:
2
aaa -9999
输出样例2:
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined
第一遍没有做出来,有思路,但是我写的代码不想改了,因为贼乱,if—else 用的太多,这也是一个教训,代码结构混乱,导致难以修改,这时应该做函数封装
我的代码:不对
第二份的代码封装的很好,但是思路也是挺麻烦.
第三份柳神 的代码,巧妙用sscanf和ssprintf 解决问题
sscanf(
#include <iostream>
#include <ctype.h>
#include<stdlib.h>
using namespace std;
int find_char(string s,char c)
{
int n = 0,position = -1;
while((position = s.find(c,position+1)) != s.npos)
{
n++;
}
return n;
}
bool isNum(string s2)
{
int i =0;
if(s2[0] =='-')
s2[0] = '0';
s2[s2.find('.')] = '0';
while(isdigit(s2[i++]));
return i-1 == s2.length();
}
int main()
{
int n = 0,num = 0,jianhao = 0,point = 0;
string s;
double sum = 0.0;
cin >> n;
for(int i = 0;i < n;i++)
{
cin >> s;
jianhao = find_char(s,'-');
point = find_char(s,'.');
if(jianhao == 0 || jianhao == 1){
if(point == 0 || ((point == 1) && (s.length() - s.find('.') - 1 <= 2)))
{
if(isNum(s))
{
double tmp = atof(s.c_str());
if(tmp >= -1000 && tmp <= 1000)
{
sum += tmp;
num ++ ;
}
else
{
cout << "ERROR: "<<s<< " is not a legal number" << endl;
}
}
else{
cout << "ERROR: "<<s<< " is not a legal number" << endl;
}
}
else{
cout << "ERROR: "<<s<< " is not a legal number" << endl;
}
}
else{
cout << "ERROR: "<<s<< " is not a legal number" << endl;
}
}
if(sum == 0)
{
cout << "The average of 0 numbers is Undefined" <<endl;
}
else{
printf("The average of %d numbers is %.2lf",num,sum/(double)num);
}
}
if…else 太多,而且思路过于简单,代码太混乱,参考别人的代码如下:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cctype>
using namespace std;
int n;
char str[101][101];
bool check(char s[]){
int i = 0;
if(s[0]=='-'){
i++;
}
for(;s[i]&&s[i]!='.';i++){
if(!isdigit(s[i])){
return false;
}
}
if(s[i]=='.'){
for(int j=i+1;s[j];j++){
if(!isdigit(s[j])||j-i>2){
return false;
}
}
}
double a = fabs(atof(s));
if(a>1000.0){
return false;
}
return true;
}
void solve(){
int ans = 0;
double sum = 0;
for(int i=0;i<n;i++){
if(check(str[i])){
ans++;
sum += atof(str[i]);
}else{
printf("ERROR: %s is not a legal number\n",str[i]);
}
}
if(ans){
if(ans==1){
printf("The average of 1 number is %.2lf\n",sum);
}else{
printf("The average of %d numbers is %.2f\n",ans,sum/ans);
}
}else{
printf("The average of 0 numbers is Undefined\n");
}
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",str[i]);
}
solve();
return 0;
}
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int main() {
int n, cnt = 0;
char a[50], b[50];
double temp, 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;
}