题解 | #HJ14 字符串排序#
字符串排序
http://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
Python版本
n = int(input())
string_list = []
for i in range(n):
string_list.append(input())
string_list.sort()
for string in string_list:
print(string)
C语言版本
#include <stdio.h>
#include <string.h>
int main() {
int n;
scanf("%d", &n);
char strings[n][101];
for (int i = 0; i < n; i++) {
scanf("%s", strings[i]);
}
char temp[101] = {0};
for(int i = 0; i < n-1; i++) {
for(int j = i+1; j < n; j++) {
if (strcmp(strings[i], strings[j]) > 0) {
memcpy(temp, strings[i], 101);
memcpy(strings[i], strings[j], 101);
memcpy(strings[j], temp, 101);
}
}
}
for (int i = 0; i < n; i++) {
printf("%s\n", strings[i]);
}
return 0;
}