-cfyjbjonn 2019-01-05 23:27 采纳率: 57.1%
浏览 566
已采纳

c语言结构体给歌手分数排序

题目如下第二个:
图片说明

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

typedef struct result
{
    int num[10];
    int score[10];

}result;

void sorting(result *p)
{
    int i,j,k,temp;
    for(i = 0;i <9;i++)
    {
        k = i;
        for(j = i+1;j<10;j++)
        {
            if(p->score[i] < p->score[j])
            {
                k = j;
            }
        }
        if(k != i)
        {
            temp = p->score[j];
            p->score[j] = p->score[i];
            p->score[i] = temp;
        }
    }
}




int main()
{
    int i;
    result people;
    printf("input:\n");
    for(i = 0;i <10;i++)
    {
        scanf("%d %d",&people.num[i],&people.score[i]);
    }
    sorting(&people.score);
    for(i = 0;i < 10; i++)
    {
        printf(" %d %d\n",i,people.score[i]);
    }


}

请问哪里有问题?

  • 写回答

3条回答 默认 最新

  • threenewbee 2019-01-06 00:22
    关注
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct result
    {
        int num[10];
        int score[10];
    
    }result;
    
    void sorting(result *p)
    {
        int i, j, k, temp;
        for (i = 0; i < 9; i++)
        {
            k = i;
            for (j = i + 1; j<10; j++)
            {
                if (p->score[k] < p->score[j])
                {
                    k = j;
                }
            }
            if (k != i)
            {
                temp = p->score[k];
                int t1 = p->num[k];
                p->score[k] = p->score[i];
                p->num[k] = p->num[i];
                p->score[i] = temp;
                p->num[i] = t1;
            }
        }
    }
    
    int main()
    {
        int i;
        result people;
        printf("input:\n");
        for (i = 0; i <10; i++)
        {
            scanf("%d %d", &people.num[i], &people.score[i]);
        }
        sorting(&people);
        for (i = 0; i < 10; i++)
        {
            printf(" %d %d\n", people.num[i], people.score[i]);
        }
    }
    
    
    

    input:
    1 1
    2 4
    3 2
    4 5
    5 1
    6 7
    7 1
    8 5
    9 9
    10 1
    9 9
    6 7
    4 5
    8 5
    2 4
    3 2
    7 1
    5 1
    1 1
    10 1
    Press any key to continue . . .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?