鱼C论坛

 找回密码
 立即注册
查看: 1566|回复: 1

[已解决]要求实现用选择排序法将分数从高到低排序(并输出每趟排序结果)的函数

[复制链接]
发表于 2023-11-23 13:29:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
从键盘输入某班学生某门课程的成绩(每班人数最多不超过40人),当输入为负值时,表示输入结束。本题要求实现用选择排序法将分数从高到低排序(并输出每趟排序结果)的函数。

函数接口定义:
void DataSort(int score[], int n);

其中 score 是用户传入的学生成绩数组, n是学生人数。

裁判测试程序样例:
#include <stdio.h>
#define N 40
int ReadScore(int score[]);
void DataSort(int score[], int n);
void PrintScore(int score[], int n);
int main()
{
    int score[N], n;
    n = ReadScore(score);
    printf("n=%d\n", n);  
    if(n)
    { DataSort(score, n);
      printf("After sort: ");
      PrintScore(score, n);
    }
    return 0;
}

int ReadScore(int score[])
{
    int i = -1;
    do{
        i++;
        scanf("%d", &score[i]);
    }while (score[i] >= 0);
    return i;
}

/* 你的代码将被嵌在这里 */

void PrintScore(int score[], int n)
{
    int i;
    for (i=0; i<n; i++)
    {  if (i!=0)  printf(" ");
       printf("%d", score[i]);
    }
    printf("\n");
}

输入样例:
64 85 73 99 92 -1

输出样例:
n=5
第1趟:99 85 73 64 92
第2趟:99 92 73 64 85
第3趟:99 92 85 64 73
第4趟:99 92 85 73 64
After sort: 99 92 85 73 64

这题怎么写
最佳答案
2023-11-23 13:30:20
  1. #include <stdio.h>

  2. void DataSort(int score[], int n) {
  3.     int i, j, temp;
  4.    
  5.     for (i = 0; i < n - 1; i++) {
  6.         for (j = 0; j < n - i - 1; j++) {
  7.             if (score[j] < score[j+1]) {
  8.                 temp = score[j];
  9.                 score[j] = score[j+1];
  10.                 score[j+1] = temp;
  11.             }
  12.         }
  13.         // 输出每趟排序结果
  14.         for (j = 0; j < n; j++) {
  15.             printf("%d ", score[j]);
  16.         }
  17.         printf("\n");
  18.     }
  19. }

  20. int main() {
  21.     int score[40];
  22.     int n = 0;
  23.    
  24.     // 读入分数,直到输入为负值结束
  25.     while (1) {
  26.         scanf("%d", &score[n]);
  27.         if (score[n] < 0) {
  28.             break;
  29.         }
  30.         n++;
  31.         if (n == 40) {
  32.             break;
  33.         }
  34.     }
  35.    
  36.     printf("n=%d\n", n);
  37.    
  38.     if (n > 0) {
  39.         DataSort(score, n);
  40.         printf("After sort: ");
  41.         for (int i = 0; i < n; i++) {
  42.             printf("%d ", score[i]);
  43.         }
  44.         printf("\n");
  45.     }
  46.    
  47.     return 0;
  48. }
  49. 以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-23 13:30:20 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. void DataSort(int score[], int n) {
  3.     int i, j, temp;
  4.    
  5.     for (i = 0; i < n - 1; i++) {
  6.         for (j = 0; j < n - i - 1; j++) {
  7.             if (score[j] < score[j+1]) {
  8.                 temp = score[j];
  9.                 score[j] = score[j+1];
  10.                 score[j+1] = temp;
  11.             }
  12.         }
  13.         // 输出每趟排序结果
  14.         for (j = 0; j < n; j++) {
  15.             printf("%d ", score[j]);
  16.         }
  17.         printf("\n");
  18.     }
  19. }

  20. int main() {
  21.     int score[40];
  22.     int n = 0;
  23.    
  24.     // 读入分数,直到输入为负值结束
  25.     while (1) {
  26.         scanf("%d", &score[n]);
  27.         if (score[n] < 0) {
  28.             break;
  29.         }
  30.         n++;
  31.         if (n == 40) {
  32.             break;
  33.         }
  34.     }
  35.    
  36.     printf("n=%d\n", n);
  37.    
  38.     if (n > 0) {
  39.         DataSort(score, n);
  40.         printf("After sort: ");
  41.         for (int i = 0; i < n; i++) {
  42.             printf("%d ", score[i]);
  43.         }
  44.         printf("\n");
  45.     }
  46.    
  47.     return 0;
  48. }
  49. 以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 15:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表