美文网首页
1108 Finding Average(20 分)

1108 Finding Average(20 分)

作者: zjh3029 | 来源:发表于2018-09-02 14:34 被阅读0次

有两个测试点未通过不知道错在哪?

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
bool isNum(string data)
{
    if (data[0] == '-' || (data[0] >= '0'&&data[0] <= '9'))
    {
        string data1(data.begin() + 1, data.end());
        int cnt = 0;
        bool first = false;
        for (auto s : data1)
        {
            if (first == true)
            {
                cnt++;
            }
            if (cnt>2)
            {
                return false;
            }
            if (s >= '0'&&s <= '9')
            {
                continue;
            }
            else if (s == '.'&&first == false)
            {
                first = true;
            }
            else
            {
                return false;
            }
        }
        if (first == true && cnt == 0)//小数点后面没有数字错误
        {
            return false;
        }
        return true;
    }
    return false;
}
int main()
{
    int M, a;
    string str;
    cin >> M;
    int allcnt = 0;
    double sum = 0.0;

    for (int i = 0; i < M; i++)
    {
        cin >> str;
        bool flag = isNum(str);
        if (flag == false)
        {
            cout << "ERROR: " << str << " is not a legal number" << endl;
        }
        else
        {
            if (stod(str)>1000 || stod(str)<-1000)
            {
                cout << "ERROR: " << str << " is not a legal number" << endl;
            }
            else
            {
                sum += stod(str);
                allcnt++;
            }

        }
    }
    if (allcnt == 0)
    {
        cout << "The average of 0 numbers is Undefined" << endl;
    }
    else
    {
        string sout = " numbers is ";
        if (allcnt==1)
        {
            sout = " number is ";
        }
        cout << "The average of " << allcnt << sout << setiosflags(ios::fixed) << setprecision(2) << sum / allcnt << endl;
    }
    system("pause");
    return 0;
}

参考教程使用,sscanf重写一遍

#include<iostream>
#include<cstring>
using namespace std;

#pragma warning(disable:4996)
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, "%.2lf", 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 %.2lf", sum);
    }
    else if (cnt > 1) {
        printf("The average of %d numbers is %.2lf", cnt, sum / cnt);
    }
    else {
        printf("The average of 0 numbers is Undefined");
    }
    system("pause");
    return 0;
}

相关文章

网友评论

      本文标题:1108 Finding Average(20 分)

      本文链接:https://www.haomeiwen.com/subject/zzwpwftx.html