PTA 1004-成績排名 c和python編寫

1004題目:

1004 成績排名 (20 分)

讀入 n(>0)名學生的姓名、學號、成績,分別輸出成績最高和成績最低學生的姓名和學號。

輸入格式:

每個測試輸入包含 1 個測試用例,格式爲

第 1 行:正整數 n
第 2 行:第 1 個學生的姓名 學號 成績
第 3 行:第 2 個學生的姓名 學號 成績
… … …
第 n+1 行:第 n 個學生的姓名 學號 成績
其中姓名和學號均爲不超過 10 個字符的字符串,成績爲 0 到 100 之間的一個整數,這裏保證在一組測試用例中沒有兩個學生的成績是相同的。

輸出格式:

對每個測試用例輸出 2 行,第 1 行是成績最高學生的姓名和學號,第 2 行是成績最低學生的姓名和學號,字符串間有 1 空格。

輸入樣例:

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

輸出樣例:

Mike CS991301
Joe Math990112

C語言代碼:

#include <stdio.h>
#include <stdlib.h>

typedef struct student{
    char name[11];
    char id[11];
    int score;
    }Stu;

int main()
{
    int n;
    scanf("%d",&n);
    Stu stu[n];
    int max,min;
    int maxNum,minNum;
    for(int i=0;i<n;i++)
    {
        scanf("%s %s %d",stu[i].name,stu[i].id,&stu[i].score);
        if(i==0){
            max=stu[0].score;
            min=stu[0].score;
            maxNum=0;
            minNum=0;
        }
        if(stu[i].score>max){
            max=stu[i].score;
            maxNum=i;
        }
        if(stu[i].score<min){
            min=stu[i].score;
            minNum=i;
        }

    }
    printf("%s %s\n",stu[maxNum].name,stu[maxNum].id);
    printf("%s %s\n",stu[minNum].name,stu[minNum].id);

    return 0;
}

python代碼:

max = 0
min = 101
n = input()
for i in range(int(n)):
    s = input()
    str = s.split()
    if int(str[2]) > max:
        max = int(str[2])
        max_mesg = str[0]+" "+str[1]
    if int(str[2]) < min:
        min = int(str[2])
        min_mesg = str[0]+" "+str[1]
print(max_mesg)
print(min_mesg)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章