题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
/*************** ** 字符串不能直接赋值,需用strcpy;字符串asc比较用strcmp。冒泡排序,break跳出循环。(借鉴的) ****************/ #include <stdio.h> #include <string.h> int main() { int n, flag = 0; scanf( "%d", &n ); char str[n][101]; char tmp[101]; int i, j; for( i = 0; i < n; i++ ) { scanf("%s", str[i]); } for( j = n - 1; j >= 1; j-- ) { flag = 0; for( i = 1; i <= j; i++ ) { if( strcmp(str[i-1],str[i]) > 0 ) { strcpy( tmp, str[i-1] ); strcpy( str[i-1], str[i] ); strcpy( str[i], tmp ); flag = 1; } } if( flag == 0 ) break; } for( i = 0; i < n; i++ ) { printf("%s\n", str[i]); } return 0; }