二维矩阵字符串排序

二维矩阵字符串排序

遇到个很难的题目:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
字符串排序。调用Input函数输入10个字符串,调用Sort函数对这10个字符串按字典顺序排序,调用Print函数打印输出排序后的字符串。
Input函数原型:
void Input(char str[][80],int n);
Sort函数原型:
void Sort(char str[][80],int n);
Print函数原型:
void Print(char str[][80],int n);
***输入数据格式:读入可包含有空格的字符串(字符串最大长度80
***输出数据格式: 一行输出一个字符串
程序运行示例:
hello↙
my↙
name
is
zyk↙
how↙
are↙
you↙
he↙
and
and
are
he
hello
how
is
my
name
you
zyk

我写的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>

void Input(char str[][80], int);
void Sort(char str[][80], int);
void Print(char str[][80], int);

int main(void)
{
char str[10][80] = { 0 };

Input(str, 10);
Sort(str, 10);
Print(str, 10);

return 0;
}

void Input(char str[][80], int n) {
for (int r = 0;r < n;r++) {
for (int c = 0;c < 80;c++) {
str[r][c] = getchar();
if (str[r][c] == '\n') {
break;
}
}
}
}

void Sort(char str[][80], int n) {
for (int i = 1;i < n;i++) {
for (int r = 0;r < n - i;r++) {
int exchange = 0;
for (int c = 0;c < 80;c++) {
if (str[r][c] > str[r + 1][c]) {
exchange = 1;
break;
}
else if (str[r][c] < str[r + 1][c]) {
break;
}
}
if (exchange) {
for (int c = 0;c < 80;c++) {
int temp = str[r][c];
str[r][c] = str[r + 1][c];
str[r + 1][c] = temp;
}
}
}
}
}

void Print(char str[][80], int n) {
for (int r = 0;r < n;r++) {
for (int c = 0;c < 80;c++) {
if (str[r][c] == '\n') { //一旦检测到字符结束,开始打印下一串字符
break;
}
printf("%c", str[r][c]);
}
}
}

我感觉这题还是比较难的

看看AI写的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <string.h>

// 函数声明
void Input(char str[][80], int n);
void Sort(char str[][80], int n);
void Print(char str[][80], int n);

int main() {
char str[10][80]; // 定义一个二维数组,存放10个字符串,每个最大长度80
int n = 10;

// 1. 输入
Input(str, n);

// 2. 排序
Sort(str, n);

// 3. 输出
Print(str, n);

return 0;
}

// 输入函数
void Input(char str[][80], int n) {
int i;
for (i = 0; i < n; i++) {
// 使用 fgets 读取一行,它可以读取包含空格的字符串
// stdin 表示从标准输入(键盘)读取
fgets(str[i], 80, stdin);

// 注意:fgets 会把换行符 '\n' 也读进去,我们需要把它去掉
// 否则排序和打印时会多出一个换行
size_t len = strlen(str[i]);
if (len > 0 && str[i][len - 1] == '\n') {
str[i][len - 1] = '\0';
}
}
}

// 排序函数(冒泡排序法)
void Sort(char str[][80], int n) {
char temp[80]; // 用于交换的临时变量
int i, j;

// 外层循环控制比较轮数
for (i = 0; i < n - 1; i++) {
// 内层循环控制每轮比较的次数
for (j = 0; j < n - 1 - i; j++) {
// strcmp比较两个字符串
// 如果 str[j] > str[j+1] (返回正数),则交换位置
if (strcmp(str[j], str[j+1]) > 0) {
// 字符串赋值不能用等号,必须用 strcpy
strcpy(temp, str[j]);
strcpy(str[j], str[j+1]);
strcpy(str[j+1], temp);
}
}
}
}

// 打印函数
void Print(char str[][80], int n) {
int i;
for (i = 0; i < n; i++) {
printf("%s\n", str[i]);
}
}

总结:

他妈的,strcmp函数可以直接按照字典顺序比较字符串!!!


二维矩阵字符串排序
http://example.com/2025/12/14/二维矩阵字符串排序/
作者
王柏森
发布于
2025年12月14日
许可协议