PAT (Basic Level) Practice (中文)1054 求平均值 (20 分)【测试点2+测试用例】

原题

1054 求平均值 (20 分)

解析

这道题是有点坑的。
一开始我测试点2一直过不了,然后就一直追加对数字的判断条件,结果最后发现居然是The average of 1 number is Y没有s,而The average of 0 numbers is Undefined又是有s的…= =
在测试过程中也想出了很多之前没想到的特例,我在这里给出一些供大家参考。
常规的有:aaa,1.999,1.1.1
特殊一点的有:.,-,-.,--,..,8-,.8,--8
特别注意8.是可行的!如果把这个判断错了的话,测试点3反而会出错!

我的夭寿代码

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main(){
    int n;
    cin>>n;
    vector<string> v;
    for(int i=0;i<n;i++){
        string s;
        cin>>s;
        v.push_back(s);
    }
    int legal=0;
    double sum=0;
    for(int i=0;i<n;i++){
        int dot=0;
        int cnt_minus=0;
        int dot_ind=-1;
        for(int j=0;j<v[i].size();j++){
            if((v[i][j]-'0'>9 || v[i][j]-'0'<0)&&!(v[i][j]=='.'||v[i][j]=='-')){ //出现了非数字
                //cout<<v[i]<<" is not a number"<<endl;
                dot=999;
                break;
            }
            else{
                double t;
                stringstream ss;
                ss<<v[i];
                ss>>t;
                if(t>1000 || t<-1000){
                    dot=999;
                }
            }
            if(v[i][j]=='.'){
                dot_ind=j;
                dot++;
            }
            if(v[i][j]=='-'){
                cnt_minus++;
            }
        }
        if(dot>1 || cnt_minus>1){
            printf("ERROR: %s is not a legal number\n",v[i].c_str());
            continue;
        }
        else if((dot>=1 && v[i].size()==1)||(cnt_minus>=1 && v[i].size()==1)){
            printf("ERROR: %s is not a legal number\n",v[i].c_str());
            continue;
        }
        else if(dot==1 && cnt_minus==1 && v[i].size()==2){
            printf("ERROR: %s is not a legal number\n",v[i].c_str());
            continue;
        }
        //dot_ind==v[i].size()-1 ||
        else if((dot_ind==0&&v[i].size()==1)){
            printf("ERROR: %s is not a legal number\n",v[i].c_str());
            continue;
        }
        else if(v[i].size()-dot_ind-1>2 && dot_ind!=-1){
            printf("ERROR: %s is not a legal number\n",v[i].c_str());
            continue;
        }
        else{
            legal++;
            double t;
            stringstream ss;
            ss<<v[i];
            ss>>t;
            sum+=t;
            //cout<<"sum+="<<t<<endl;
        }
    }
    //cout<<"sum="<<sum<<endl;
    if(legal>1){
        sum/=legal;
        printf("The average of %d numbers is %.2f\n",legal,sum);
    }
    else if(legal==1){
        printf("The average of %d number is %.2f\n",legal,sum);
    }
    else printf("The average of 0 numbers is Undefined\n");
}

找特例的时候心态炸了,代码写的很烂,见谅。

柳神代码

#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;
}

核心思想是用了sscanf和sprintf,很妙。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个大番茄z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值