1054 求平均值 (20 分)

#include<iostream>

#include<stdio.h>

#include<string.h>

using namespace std;

int main()

{

    int cou;

    int count = 0;

    cin >> cou;

    char s[210], s1[210];

    double ave = 0, num, num1;

扫描二维码关注公众号,回复: 6221056 查看本文章

    for (int i = 0; i < cou; i++)

    {

        num = 1001;

        scanf("%s", s);

        sscanf(s, "%lf", &num);

        sprintf(s1, "%.2lf", num);

        for (int j = 0; j < strlen(s); j++)

        {

            if (s[j] != s1[j])

                num = 1001;

        }

        if (num >= -1000 && num <= 1000)

        {

            count++;

            ave += num;

        }

        else

            cout << "ERROR: " << s << " is not a legal number\n";

    }

    if (count == 0)

        cout << "The average of 0 numbers is Undefined\n";

    else if(count>1)

    {

        cout << "The average of " << count << " numbers is ";

        printf("%.2f\n", ave / count);

    }

else

{

cout << "The average of 1 number is ";

        printf("%.2f\n", ave / count);

}

    system("pause");

    return 0;

}

猜你喜欢

转载自blog.csdn.net/yubai258/article/details/90171527