3.2 - C 查找学生信息

测试地址:

【题目描述】

输入N个学生的信息,然后进行查询。

【输入】

输入的第一行为N,即学生的个数(N <= 1000)

接下来的N行包括N个学生的信息,信息格式如下:

01 李江 男 21

02 刘唐 男 23

03 张军 男 19

04 王娜 女 19

然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下:

02

03

01

04

【输出】

输出M行,每行包括一个对应于查询的学生的信息。

如果没有对应的学生信息,则输出“No Answer!”

【样例输入】

5
001 张三 男 19
002 李四 男 20
003 王五 男 18
004 赵六 女 17
005 刘七 女 21
7
003
002
005
004
003
001
006

【样例输出】

003 王五 男 18
002 李四 男 20
005 刘七 女 21
004 赵六 女 17
003 王五 男 18
001 张三 男 19
No Answer!

【AC代码】 

#include<iostream>
using namespace std;
struct node{
	int age;
	string id, name, sex;
}stu[1010];
int main(){
	int n, m;
	while(!(cin >> n).eof()){
		for(int i = 0; i < n; i++)
			cin >> stu[i].id >> stu[i].name >> stu[i].sex >> stu[i].age;
		cin >> m;
		while(m--){
			string x;
			cin >> x;
			bool flag = false;
			for(int i = 0; i < n; i++){
				if(x == stu[i].id){
					flag = true;
					cout << stu[i].id << " " << stu[i].name << " " << stu[i].sex << " " << stu[i].age << endl;
					break;
				}
			}
			if(!flag)	cout << "No Answer!" << endl;
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个基于链表的 C 语言程序,可以按照学生姓名查找学生信息: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义学生结构体 typedef struct student { char name[20]; int age; float gpa; struct student* next; } Student; // 查找学生信息的函数 Student* findStudent(Student* head, char* name) { // 遍历链表 while (head != NULL) { // 比较姓名是否相同 if (strcmp(head->name, name) == 0) { return head; // 找到匹配的学生,返回指针 } head = head->next; // 指向下一个节点 } return NULL; // 没有找到匹配的学生,返回 NULL } int main() { // 创建链表 Student* head = NULL; Student* node1 = (Student*)malloc(sizeof(Student)); strcpy(node1->name, "Alice"); node1->age = 20; node1->gpa = 3.5; node1->next = NULL; head = node1; Student* node2 = (Student*)malloc(sizeof(Student)); strcpy(node2->name, "Bob"); node2->age = 21; node2->gpa = 3.2; node2->next = NULL; node1->next = node2; // 按姓名查找学生信息 char name[20]; printf("请输入学生姓名:"); scanf("%s", name); Student* result = findStudent(head, name); if (result != NULL) { printf("姓名:%s 年龄:%d GPA:%f\n", result->name, result->age, result->gpa); } else { printf("没有找到匹配的学生信息!\n"); } // 释放链表内存 while (head != NULL) { Student* temp = head; head = head->next; free(temp); } return 0; } ``` 在这个程序,我们首先定义了一个 `Student` 结构体,包含学生姓名、年龄、GPA 和下一个节点的指针。然后,我们创建了一个包含两个学生信息的链表,并且让用户输入要查找学生姓名。最后,我们调用 `findStudent` 函数来查找匹配的学生信息,并输出结果。如果没有找到匹配的学生信息,则程序会输出“没有找到匹配的学生信息!”的提示。 值得注意的是,这个程序只是一个简单的示例,实际应用可能需要对输入进行一些错误处理,例如输入的姓名长度超过了数组的限制等等。 ### 回答2: 在C语言,我们可以使用链表来存储和操作学生信息。链表是一种动态数据结构,可以动态地插入和删除节点,非常适合用来存储不确定个数的数据。 首先,我们需要定义一个学生信息的结构体,包含姓名、年龄、性别等属性。然后,定义一个链表节点结构体包含指向下一个节点的指针以及一个指向学生信息结构体的指针。 接下来,我们可以编写一个函数来创建一个链表节点,并根据输入的学生信息来初始化节点。然后,再写一个函数来插入节点到链表。 当需要按姓名查找学生信息时,我们可以编写一个函数,传入链表头节点和要查找姓名作为参数。函数会遍历链表,比较链表每个节点的姓名与要查找姓名是否一致,如果一致则返回该节点的学生信息。 需要注意的是,在比较字符串时不能直接使用等于运算符,而是要使用字符串比较函数strcmp()来比较。比较结果为0表示相等。 下面是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 学生信息结构体 typedef struct student { char name[20]; int age; char gender[10]; } Student; // 链表节点结构体 typedef struct node { Student *data; struct node *next; } Node; // 创建链表节点并初始化 Node* createNode(Student *data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; return newNode; } // 插入节点到链表 void insertNode(Node** head, Student *data) { Node* newNode = createNode(data); newNode->next = *head; *head = newNode; } // 按姓名查找学生信息 Student* findStudentByName(Node* head, char* name) { Node* temp = head; while (temp != NULL) { if (strcmp(temp->data->name, name) == 0) { return temp->data; } temp = temp->next; } return NULL; // 找不到返回NULL } int main() { Node* head = NULL; // 初始化链表头节点 // 创建并插入学生信息 Student* stu1 = (Student*)malloc(sizeof(Student)); strcpy(stu1->name, "张三"); stu1->age = 20; strcpy(stu1->gender, "男"); insertNode(&head, stu1); Student* stu2 = (Student*)malloc(sizeof(Student)); strcpy(stu2->name, "李四"); stu2->age = 21; strcpy(stu2->gender, "女"); insertNode(&head, stu2); // 按姓名查找学生信息 char name[20]; printf("请输入要查找姓名:\n"); scanf("%s", name); Student* result = findStudentByName(head, name); if (result != NULL) { printf("姓名:%s\n", result->name); printf("年龄:%d\n", result->age); printf("性别:%s\n", result->gender); } else { printf("未找到该学生信息!\n"); } return 0; } ``` 这个示例代码,我们定义了一个链表,其包含两个学生信息。通过输入要查找姓名,我们可以输出对应的学生信息。你可以根据自己的需求和实际情况,扩展和优化这个代码片段。 ### 回答3: 在C语言,我们可以使用链表来存储学生信息并按姓名进行查找。首先,我们需要定义一个学生结构体,包含姓名和其他相关信息。 ```c typedef struct Student { char name[50]; int age; float gpa; struct Student* next; } Student; ``` 然后,我们可以创建一个链表,其每个节点都是一个学生信息。接下来,我们编写一个函数,用于按姓名在链表查找学生信息。 ```c Student* searchStudent(Student* head, char* name) { Student* current = head; while(current != NULL) { if(strcmp(current->name, name) == 0) { return current; } current = current->next; } // 如果找不到匹配的学生信息 return NULL; } ``` 在主函数,我们可以创建一个链表并添加学生信息。然后,调用 `searchStudent` 函数来查找学生信息,并打印出来。 ```c int main() { // 创建链表并添加学生信息 Student* head = NULL; Student* student1 = (Student*)malloc(sizeof(Student)); strcpy(student1->name, "张三"); student1->age = 19; student1->gpa = 3.9; student1->next = NULL; head = student1; // 添加更多的学生信息... // 按姓名查找学生信息 char searchName[50]; printf("请输入想要查找学生姓名:"); scanf("%s", searchName); Student* result = searchStudent(head, searchName); if(result != NULL) { printf("找到了学生信息:\n"); printf("姓名:%s\n", result->name); printf("年龄:%d\n", result->age); printf("GPA:%f\n", result->gpa); } else { printf("没有找到匹配的学生信息。\n"); } return 0; } ``` 这样,我们就可以使用C语言链表按姓名查找学生信息了。需要注意的是,对于更复杂的链表操作,我们可能需要额外的函数来插入、删除和修改学生信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值