1.如下题:
我们知道计算理工学院的每个精英班有 10 名学生,老师会依次给出 10 名学生的名字(均为不含有空格的英文字符串)。你需要将这些名字按照字典序从小到大进行输出。
输入格式
测评机会反复运行你的程序。每次程序运行时,你的程序会被输入 10 行不含有空格的字符串,分别对应十个学生的姓名(字符串长度均大于 0 且小于 20)。
输出格式
输出为 10 行,为排序后的 10 个学生姓名,每个学生姓名单独占一行。
2.知识点:
2.1.矩阵
2.2.字符串比较函数
strcmp(input,string) 这里是:当 input 小于 string返回负数,当 input 等于 string 返回 0,当 input 大于 string 返回正数。
2.3.字符串复制函数
strcpy(copy,string); 这里是: string 的内容复制到 copy 字符串中
样例输入
Alice Bob Gary Harry Ivn Julia Danis Fone Candy Evan
样例输出
Alice Bob Candy Danis Evan Fone Gary Harry Ivn Julia
3.详细代码如下:
#include <stdio.h>
#include <string.h>int main() {char strs[10][21];//输入for(int i = 0 ;i<10;i++){scanf("%s",strs[i]);}char temp[21];int tempIndex;//排序for(int i = 0;i<10;i++){tempIndex = i;for(int j = i+1;j<10;j++ ){if(strcmp(strs[j],strs[tempIndex]) < 0){tempIndex = j;}}if(tempIndex != i){strcpy(temp,strs[tempIndex]);strcpy(strs[tempIndex],strs[i]);strcpy(strs[i],temp);}}//输出for(int i = 0 ;i<10;i++){printf("%s",strs[i]);if(i<9){printf("\n");}}return 0;
}