课上遇到的问题
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct stu{
int seq;
int gpa;
struct stu * next;
};
struct stu * CreateRandList( )
///创建含有N位同学的班级,N是一个大于10的随机数 ,按照同学的序号从小到大排列
///每位同学的成绩在0~99之间随机生成
///返回头指针
{
struct stu *head , *p , *q;
int sum = 0 , i = 0, N = 0;
srand((int)time(NULL));
N = 10 + rand()%100;
head = (struct stu *)malloc(sizeof(struct stu ));
q = head;
for(i = 0; i < N; i++)
{
p = (struct stu *)malloc(sizeof(struct stu ));
sum = rand()%10 + sum + 1;
p->seq = sum;
p->gpa = rand()%100;
q->next = p;
q = p;
}
q->next = NULL;
return head;
}
void Award( struct stu *head )
///打印出成绩最高的同学的序号,如果有多位同学成绩最高则打印出他们所有人的序号
{
}
int main()
{
struct stu *head;
head = CreateRandList( );
Award( head ) ;
}
可以输出注释所提出的要求
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct stu{
int seq;
int gpa;
struct stu * next;
};
struct stu * CreateRandList( )
///创建含有N位同学的班级,N是一个大于10的随机数 ,按照同学的序号从小到大排列
///每位同学的成绩在0~99之间随机生成
///返回头指针
{
struct stu *head , *p , *q;
int sum = 0 , i = 0, N = 0;
srand((int)time(NULL));
N = 10 + rand()%100;
head = (struct stu *)malloc(sizeof(struct stu ));
q = head;
for(i = 0; i < N; i++)
{
p = (struct stu *)malloc(sizeof(struct stu ));
sum = rand()%10 + sum + 1;
p->seq = sum;
p->gpa = rand()%100;
q->next = p;
q = p;
}
q->next = NULL;
return head;
}
void Award( struct stu *head )
///打印出成绩最高的同学的序号,如果有多位同学成绩最高则打印出他们所有人的序号
{
}
int main()
{
struct stu *head;
head = CreateRandList( );
Award( head ) ;
}
void Award( struct stu *head )
///打印出成绩最高的同学的序号,如果有多位同学成绩最高则打印出他们所有人的序号
{
struct stu *p = head;
int max = 0;
while(p){
if (p->gpa > max)
{
max = p->gpa;
}
p = p->next;
}
printf("the top gpa seq is:\n");
p = head;
while(p){
if (p->gpa >= max)
{
printf("%d ", p->seq);
}
p = p->next;
}
}