從鍵盤輸入某班學生某門課的成績(每班人數最多不超過40人),當輸入為負值時,表示輸入結束,試編程將分數按從高到低順序進行排序輸出,
排序功能需要自定義函式實作,
**輸入格式要求:"%d"
**輸入提示資訊:“Input score:” “Total students are %d\n” “Sorted scores:”
**輸出格式要求:"%4d"
程式的運行示例如下:
Input score:84
Input score:83
Input score:88
Input score:87
Input score:61
Input score:-1
Total students are 5
Sorted scores: 88 87 84 83 61
#include<stdio.h>
int main()
{
int score[40], i, n, temp, j, count = 0;
printf("Input score:");
for (i = 0; i < 40; i++)
{
scanf_s("%d", &n); //此處先將成績放在n中,判斷正負,正,存入陣列,負,跳出
if (n > 0) //這樣可以避免將負值存入陣列中,可以不用再在最后輸出的回圈次數上做文章
{
score[i] = n;
count++; //count計算學生的數量
}
else
break;
}
printf("Total students are %d\n", count);
for (i = 0; i < count; i++)
{
for (j = i; j < count; j++)
{
if (score[i] < score[j])
{
temp = score[i];
score[i] = score[j];
score[j] = temp;
}
}
}
printf("Sorted scores : ");
for (i = 0; i < count; i++)
printf("%4d",score[i]);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/260949.html
標籤:其他
