Loading... ### 题目: 每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。 ### 输入格式: 输入第一行给出一个正整数 N(≤1000),随后 N 行,每行给出一个考生的信息:`准考证号 试机座位号 考试座位号`。其中`准考证号`由 16 位数字组成,座位从 1 到 N 编号。输入保证每个人的准考证号都不同,并且任何时候都不会把两个人分配到同一个座位上。 考生信息之后,给出一个正整数 M(≤N),随后一行中给出 M 个待查询的试机座位号码,以空格分隔。 ### 输出格式: 对应每个需要查询的试机座位号码,在一行中输出对应考生的准考证号和考试座位号码,中间用 1 个空格分隔。 ### 输入样例: ```in 4 3310120150912233 2 4 3310120150912119 4 1 3310120150912126 1 3 3310120150912002 3 2 2 3 4 ``` ### 输出样例: ```out 3310120150912002 2 3310120150912119 1 ``` 我写的代码超时: ```cpp #include <stdio.h> #include <stdlib.h> typedef struct Student { int status; //准考证号 int text;//试机座位号 int examination;//考试座位号 struct Student *next; }stu; int main() { int people; stu *scan_people(int), *head; void print_people(stu *); scanf("%d", &people); head = scan_people(people); print_people(head); return 0; } stu *scan_people(int people) { stu *head, *p1, *p2; int m; p2 = p1 = (stu *)(malloc (sizeof (stu))); scanf("%d %d %d", &p1->status, &p1->text, &p1->examination); head = p1; m = 1; while (m < people) { p1 = (stu *)(malloc(sizeof(stu))); scanf("%d %d %d", &p1->status, &p1->text, &p1->examination); p2->next = p1; p2 = p1; m++; } p2->next = NULL; return head; } void print_people(stu *head) { stu *p1; int cha_xun, wait, n; scanf("%d", &wait); n = 0; while (n < wait) { scanf ("%d", &cha_xun); p1 = head; while (p1 != NULL) { if (p1->text == cha_xun) { printf("%d %d\n", p1->status, p1->examination); } p1 = p1->next; } } } ``` 换思路后报错代码: ```cpp #include <stdio.h> #include <stdlib.h> int main () { int n, i, j, k; int m, temp, input; int *array; scanf("%d", &n); array = (int *)(calloc(sizeof(int),n*3)); for (i = 0; i < n; i++) { for (j = 0; j < 3; j++) { scanf ("%d", *(array + i) + j); } } for (i = 0; i < n; i++) { for (j = 0; j < 3; j++) { printf ("%d ", *(*(array + i) + j) ); } printf ("\n"); } temp = 0; scanf ("%d", &m); while (temp < m) { scanf ("%d", &input); { for (i = 0; i < n; i++) { if (*(*(array + i) + 1) == input) { for (j = 0; j < n; j++) { for (k = 0; k < 3; k++) { if (k != 1) { printf ("%d", *(*(array + j) + k)); } } printf ("\n"); } } } } temp++; } return 0; } ``` 最后修改:2021 年 06 月 24 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果文章有用,请随意打赏。
1 条评论
大佬(๑•̀ㅁ•́ฅ)
滴!学生卡!打卡时间:下午2:41:37,请上车的乘客系好安全带~