用C语言请编程实现按奥运会参赛国国名在字典中的顺序对其入场次序进行排序。假设参赛国不超过150个。 *输入格式要求:"%d"提示信息:"How many countries?"Input their nanes:n" *输出格式要求:"Sorted results:T 程序的运行示例如下: How many c0umtries?5 Input theirnaes: America England Australia Sweden Fin1and Sorted resu1lts: America Australia England Finland Sweden
时间: 2024-03-29 20:40:55 浏览: 174
以下是使用C语言实现按字典序排序的程序:
```c
#include <stdio.h>
#include <string.h>
#define MAX_COUNTRY_NUM 150
#define MAX_COUNTRY_NAME_LEN 50
int main() {
int n, i, j;
char country[MAX_COUNTRY_NUM][MAX_COUNTRY_NAME_LEN];
char temp[MAX_COUNTRY_NAME_LEN];
printf("How many countries? ");
scanf("%d", &n);
printf("Input their names:\n");
for (i = 0; i < n; i++) {
scanf("%s", country[i]);
}
// 冒泡排序
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
if (strcmp(country[j], country[j + 1]) > 0) {
strcpy(temp, country[j]);
strcpy(country[j], country[j + 1]);
strcpy(country[j + 1], temp);
}
}
}
printf("Sorted results: ");
for (i = 0; i < n; i++) {
printf("%s ", country[i]);
}
printf("\n");
return 0;
}
```
程序中使用了冒泡排序算法对输入的国家名称进行排序,并输出排序结果。
阅读全文