1054 求平均值

本题的基本要求非常简单:给定 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

输入n个数求平均值Python for 输入n个数求平均值PHP代码_测试用例

代码实现:

#include 
#include 
#include 
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     {
        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);
}
#include 
#include 
#include 
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     {
        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);
}

明日预告:集体照

拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下:

  • 每排人数为 N/K(向下取整),多出来的人全部站在最后一排;
  • 后排所有人的个子都不比前排任何人矮;
  • 每排中最高者站中间(中间位置为 m/2+1,其中 m 为该排人数,除法向下取整);
  • 每排其他人以中间人为轴,按身高非增序,先右后左交替入队站在中间人的两侧(例如5人身高为190、188、186、175、170,则队形为175、188、190、186、170。这里假设你面对拍照者,所以你的左边是中间人的右边);
  • 若多人身高相同,则按名字的字典序升序排列。这里保证无重名。

现给定一组拍照人,请编写程序输出他们的队形。

输入格式:

每个输入包含 1 个测试用例。每个测试用例第 1 行给出两个正整数 N(≤104,总人数)和 K(≤10,总排数)。随后 N 行,每行给出一个人的名字(不包含空格、长度不超过 8 个英文字母)和身高([30, 300] 区间内的整数)。

输出格式:

输出拍照的队形。即K排人名,其间以空格分隔,行末不得有多余空格。注意:假设你面对拍照者,后排的人输出在上方,前排输出在下方。

输入样例:

10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159

输出样例:

Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John

输入n个数求平均值Python for 输入n个数求平均值PHP代码_取整_02