一.題目大概:
如表所示為一張學生成績表,設計一個程式,能夠根據學生的成績進行降序排序,并列印出排序忽的結果,
| 學號 | 姓名 | 成績 | 學號 | 姓名 | 成績 |
| 1 | 王強 | 78 | 5 | 劉偉 | 90 |
| 2 | 李麗 | 65 | 6 | 張平 | 98 |
| 3 | 張寧 | 79 | 7 | 趙紅 | 75 |
| 4 | 楊林 | 98 | 8 | 陸平 | 84 |
二.演算法分析:
(1)按題目可知,有三種資訊類別,分別為學號、姓名、成績三種,并且每種都有多個資料,所以我們可以把這三類資訊用陣列實作,
(2)本題目是按成績的降序進行排序,故需對每位同學的成績進行比較,比較完后,再進行交換,這里我們需要設定三個中間變數,分別為字符陣列temp、整形變數x、浮點型變數y,
(3)對于排序方式,我們可以利用選擇排序進行排序——即選擇一個數(通常為第一個數字),對除它以外的數進行比較,然后交換位置,之后依次往后選擇數字,
三.代碼實作:
#include<stdio.h>
#include<string.h>
int main()
{
char s[100][100];
int node[100];
float sorce[100];
char temp[100];
int x;
float y;
for(int i=0;i<8;i++)
{
scanf("%d",&node[i]);
scanf("%s",s[i]);
scanf("%f",&sorce[i]);
}
for(int i=0;i<8;i++)
for(int j=i+1;j<8;j++)
{
if(sorce[i]<sorce[j])
{
x=node[i];
node[i]=node[j];
node[j]=x;
strcpy(temp,s[i]);
strcpy(s[i],s[j]);
strcpy(s[j],temp);
y=sorce[i];
sorce[i]=sorce[j];
sorce[j]=y;
}
}
printf("\n學號 姓名 成績\n");
for(int i=0;i<8;i++)
{
printf("%d %s %6.2f\n",node[i],s[i],sorce[i]);
}
}
四. 結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301520.html
標籤:其他
