题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
#include <stdio.h>
#include <string.h>
int main()
{
int n;
scanf("%d",&n);
char str[n][101];
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
}
//冒泡排序
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(strcmp(str[i], str[j])>0) // 字符串的比较
{
char temp[101];
//字符串的交换
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j], temp);
}
}
}
for(int i=0;i<n;i++)
{
printf("%s\n",str[i]);
}
return 0;
}
查看7道真题和解析