sse——奥运参赛国出场次序:

奥运参赛国出场次序:
输入奥运会参赛国国名,并按照字典序对其进行排序。
要求:参赛国数量不超过150个,每个国家的名字不超过9个字符。
提示:‘\0’占一个字符。
要求:请找出下面程序的错误并改正。

#include <string.h>

#include <stdio.h>

#define N 150

#define MAX_LEN 10

void SortString(char str[][MAX_LEN], int n);

int main()

{

    int i, n;

    char name[N][MAX_LEN];

    printf("How many countries?");

    scanf("%d",&n);    

    getchar();

    printf("Input their names\n");

    for(i=0;i<n;i++)

        gets(name[i]);     

    void SortString(name[N][MAX_LEN], n);  

    printf("Sorted results:\n");

    for(i=0;i<n;i++)

    {

        puts(name[i]);     

    }

    return 0;

}

void SortString(char str[][MAX_LEN], int n)    

{

    int i,j;

    char temp[MAX_LEN];

    for(i=0;i<n;i++)

    {

        for(j=i+1;j<n;j++)

        {

            if(str[j]<str[i])       

            {

                strcpy(temp,str[i]);

                strcpy(str[i],str[j]);

                strcpy(str[j],temp);

            }

        }

    }

}

正解

#include <string.h>
#include <stdio.h>

#define N 150
#define MAX_LEN 10
void SortString(char str[][MAX_LEN], int n);

int main()
{
    int i, n;
    char name[N][MAX_LEN];

    printf("How many countries?");
    scanf("%d",&n);
    getchar();
    printf("Input their names\n");

    for(i=0;i<n;i++)
       {
           gets(name[i]);
       }

    SortString(name, n);
    printf("Sorted results:\n");
    for(i=0;i<n;i++)
    {
        puts(name[i]);
    }
    return 0;

}

void SortString(char str[][MAX_LEN], int n)
{
    int i,j;
    char temp[MAX_LEN];

    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(strcmp(str[j],str[i])<0)
            {
                strcpy(temp,str[i]);
                strcpy(str[i],str[j]);
                strcpy(str[j],temp);
            }
        }
    }
}
 

posted @   诩en  阅读(103)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示