Tsukinai的第四十四个程序
奥运参赛国出场次序:
输入奥运会参赛国国名,并按照字典序对其进行排序。
要求:参赛国数量不超过150个,每个国家的名字不超过9个字符。
提示:‘\0’占一个字符。
#include <string.h>
#include <stdio.h>
#include <stdlib.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);
printf("Input their names\n");
getchar();
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]);
}
system("pause");
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);
}
}
}
}