设计一个结构体用于表示学生的信息,包括学号、姓名和年龄。编写一个程序,从键盘输入若千个学生的信息,存储到结构体数组中,并输出年龄最大的学生的学号和姓名。输入: 从键盘输入一个整数 n(1<=n=100),表示学生人数。 接下来输入 行,每行包含一个学生的信息,格下: 学号(12位)姓名(不含空格且不超过20位)年龄(整数) 数据之间用空格隔开。 输出:输出年龄最大的学生的学号和姓名,用空格隔开0
时间: 2024-02-13 18:07:27 浏览: 42
好的,下面是C语言代码实现:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
struct Student {
char id[13]; // 学号
char name[21]; // 姓名
int age; // 年龄
};
int main() {
int n;
struct Student students[MAX_STUDENT_NUM];
printf("请输入学生人数: ");
scanf("%d", &n);
getchar(); // 读取换行符
// 读取学生信息
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的信息(学号 姓名 年龄): ", i + 1);
scanf("%s %s %d", students[i].id, students[i].name, &students[i].age);
getchar(); // 读取换行符
}
// 查找年龄最大的学生
int max_age = 0;
int max_age_index = 0;
for (int i = 0; i < n; i++) {
if (students[i].age > max_age) {
max_age = students[i].age;
max_age_index = i;
}
}
// 输出年龄最大的学生的学号和姓名
printf("年龄最大的学生: %s %s\n", students[max_age_index].id, students[max_age_index].name);
return 0;
}
```
注意事项:
1. 定义结构体时,要注意各个字段的类型和长度。
2. 在读取学生信息时,要注意读取完整一行,并使用`getchar()`读取换行符。
3. 查找年龄最大的学生时,要对每个学生的年龄进行比较,找到最大值和对应的下标。
4. 输出时要注意格式,要与题目要求保持一致。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![asm](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)