算法筆記第三章練習題_成績排名

輸入:

 

輸出:

思路:

1:令結構體Student型記錄單個學生的姓名,學號,分數.記Student型變量temp存放臨時輸入的數據,ans_max存放最高學生的成績ans_min存放最低學生的成績

2.在讀入數據前初始化ans_max和ans_min的初值,分別設置爲-1和101,方便更新

注意:字符數組name和id的大小必須至少是11不能設置爲10,不然會報錯,應爲字符數組的最後一位需要留給'\0',所以數組大小必須比題目的要求至少大一位。

 

# include <cstdio>
struct Student 
{
    char name[15];
    char id[15];
    int score;
}temp,ans_max,ans_min;
//temp存放臨時數據,ans_max爲最高分數的學生,ans_min爲最低分數的學分
int main()
{
    
    int n;
    scanf("%d",&n);
    ans_max.score = -1;           //最高分初始爲-1
    ans_min.score = 101           //最低分初始爲101
    for (int i = 0;i < n;i++)
    {
        
        scanf("%s%s%d",temp.name,temp.id,&temp.score)  //讀取學生信息
        if(temp.score>ans_max.score) ans_max = temp;    //該學生分數更高,更新
        if(temp.score<ans_min.score)  ans_min = temp;   //該學生分數更低,更新
    } //這兩句if語句好好想一想,可以適當的舉個例子
    printf("%s %s\n",ans_max.name,ans_max.id);
    printf("%s %s\n",ans_min.name,ans_min.id);        
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章