
这个题目要用什么方法?这个题目要用什么方法?C语言,C语言,CC语言
用一个for循环从学生结构数组的下标为1的结构开始访问,直到最后一个。
然后在for循环里面,用if语句判断当前年龄最大的结构的年龄是否小于当前数组下标的学生年龄,如果小于,则把这个结构赋值给年龄最大的结构变量stu_max,直到比较完学生结构数组所有元素就得出最大年龄的结构stu_max。
最后打印stu_max存放的最大年龄的学生信息。
代码如下:
#include <stdio.h>
#include <string.h>
struct person{
char name[20];
char sex[4];
int age;
};
typedef struct person PERSON;
int main(void){
int i;
PERSON stu[4]={
{"li","女?",18},
{"wang","男D",20},
{"zhang","男D",20},
{"sun","女?",22},
};
PERSON stu_max = stu[0];
for(i=1;i<4;i++){
if(stu_max.age<stu[i].age){
stu_max=stu[i];
}
}
printf("年龄最大的学生信息:姓名:%s, 性别:%s, 年龄:%d. \n",stu_max.name,stu_max.sex,stu_max.age);
return 0;
}
