PATA1108_测试点3巨坑(难度:⭐️⭐️⭐️)

测试点3应该是类似于大于1000然后还有个小数点的,例如:1001.

按照题目要求.42或者是42.都是合法的

我原来判断小数点都默认把带有小数点的树都在合法范围内,没有判断是不是在[-1000,1000]内

测试点三就一直过不去😂;

#include <bits/stdc++.h>
using namespace std;
bool judge (string s) {
    int cntClum = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] >= 65 && s[i] <= 122)
            return false;
        if (s[i] == '.') cntClum++;
    }
    if (cntClum > 1) return false;
    int pos = s.find('.');
    if (pos < s.size()) {
        if (s.size() - pos > 3) return false;
        double temp = stod(s);
        if (temp > 1000 || temp < -1000) {        //坑就在这
            return false;
        }
    }
    else {
        double temp = stod(s);
        if (temp > 1000 || temp < -1000) {
            return false;
        }
    }
    return true;
}
int main() {
    int n, cnt = 0;
    string s;
    double sum = 0.0, temp;
    scanf ("%d", &n);
    for (int i = 0; i < n; i++) {
        cin >> s;
        if (judge(s)) {
            temp = stod(s);
            sum += temp;
            cnt++;
        }
        else {
            printf ("ERROR: %s is not a legal number\n", s.c_str());
        }
    }
    if (cnt == 0) printf ("The average of 0 numbers is Undefined");
    else if (cnt == 1) printf ("The average of %d number is %.2f", cnt, sum / (1.0 * cnt));
    else  printf ("The average of %d numbers is %.2f", cnt, sum / (1.0 * cnt));
    
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值