文章

18

粉丝

0

获赞

3

访问

4.8k

头像
查找学生信息2 题解:运行超时通过率33%
P1476 清华大学机试题
发布于2024年3月20日 18:14
阅读数 315

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct {
	char number[5];
	char name[50];
	char sex[50];
	int age;
}student;

void search(student *list,int n, char* s) {
	for (int i = 0; i < n; i++) {
		if (strcmp(list[i].number, s) == 0) {
			printf("%s %s %s %d", list[i].number, list[i].name, list[i].sex, list[i].age);
			return;
		}
	}
	printf("No Answer!");
}
int main() {
	int n, m;
	char str[5];
	
	scanf("%d", &n);
	student* list = (student*)malloc(sizeof(student)*n);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %s %d", list[i].number, list[i].name, list[i].sex, &list[i].age);
	}
	
	scanf("%d", &m);
	for (int i = 0; i < m; i++) {
		scanf("%s", str);
		search(list,m,str);
		printf("\n");
	}
}

 

登录查看完整内容


登录后发布评论

2 条评论
snake
2024年3月20日 19:34

search(list,m,str);

这里传参应该是n不是m,否则可能越界

赞(0)

zx142407789 : 回复 snake: 哦哦懂了,大意了,谢谢

2024年3月21日 14:06