检查并修改c语言代码错误:#include <stdio.h> #include <string.h> struct xinxi { char name[30]; char birthday[9]; char number[18]; }; int main() { struct xinxi xin[10]; int n, i, j; int year[10]; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s %s %s", xin[i].name, xin[i].birthday, xin[i].number); year[i] = atoi(xin[i].birthday) / 10000; } // 冒泡排序 for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (year[j] < year[j+1]) { int tmp = year[j]; year[j] = year[j+1]; year[j+1] = tmp; struct xinxi tmp_xin = xin[j]; xin[j] = xin[j+1]; xin[j+1] = tmp_xin; } } } // 输出结果 for (i = 0; i < n; i++) { printf("%s %s %s\n", xin[i].name, xin[i].birthday, xin[i].number); } return 0; }。输入样例: 3 zhang 19850403 13912345678 wang 19821020 +86-0571-88018448 qian 19840619 13609876543 输出样例: wang 19821020 +86-0571-88018448 qian 19840619 13609876543 zhang 19850403 13912345678
时间: 2024-03-19 12:41:28 浏览: 148
代码没有语法错误,但是存在逻辑错误。当年份相同的时候,应该按照月份和日期的大小进行排序。以下是修改后的代码:
```
#include <stdio.h>
#include <string.h>
struct xinxi {
char name[30];
char birthday[9];
char number[18];
};
int main() {
struct xinxi xin[10];
int n, i, j;
int year[10], month[10], day[10];
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%s %s %s", xin[i].name, xin[i].birthday, xin[i].number);
year[i] = atoi(xin[i].birthday) / 10000;
month[i] = atoi(xin[i].birthday) % 10000 / 100;
day[i] = atoi(xin[i].birthday) % 100;
}
// 冒泡排序
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (year[j] > year[j+1] ||
(year[j] == year[j+1] && month[j] > month[j+1]) ||
(year[j] == year[j+1] && month[j] == month[j+1] && day[j] > day[j+1])) {
int tmp_year = year[j], tmp_month = month[j], tmp_day = day[j];
year[j] = year[j+1], month[j] = month[j+1], day[j] = day[j+1];
year[j+1] = tmp_year, month[j+1] = tmp_month, day[j+1] = tmp_day;
struct xinxi tmp_xin = xin[j];
xin[j] = xin[j+1];
xin[j+1] = tmp_xin;
}
}
}
// 输出结果
for (i = 0; i < n; i++) {
printf("%s %s %s\n", xin[i].name, xin[i].birthday, xin[i].number);
}
return 0;
}
```
输入样例:
```
3
zhang 19850403 13912345678
wang 19821020 +86-0571-88018448
qian 19840619 13609876543
```
输出样例:
```
wang 19821020 +86-0571-88018448
qian 19840619 13609876543
zhang 19850403 13912345678
```
阅读全文