输入n个学生的姓名和百分制成绩,分段统计学生的成绩。
输入格式:
输入在第一行中给出正整数N(1≤n≤100)。随后N行,每行给出一位学生的姓名和成绩,中间以空格分隔。
输出格式:
在一行中顺序输出成绩为80-100分、60-79分、0-59分的学生人数,中间以空格分隔。
输入样例:
5
huanglan 83
wanghai 76
shenqiang 50
zhangfeng 95
zhangmeng 60
输出样例:
2 2 1
#include<stdio.h>
#define MAXN 100
struct student{
char name[20];
int score;
};
void cnt_score( struct student *p, int n );
int main()
{
int i, n;
struct student stu[MAXN];
scanf("%d", &n);
for(i = 0; i < n; i++){
scanf("%s%d", stu[i].name, &stu[i].score);
}
cnt_score(stu, n);
return 0;
}
void cnt_score(struct student *p, int n)
{
int cnt_a = 0, cnt_p = 0, cnt_f = 0;
struct student stu[q];struct student *p=stu; //填空1
while ( p <= q ){
if ((*p).score>=80&&(*p).score<=100) //填空2
cnt_a++;
else if ((*p).score>=60&&(*p).score<=79) //填空3
cnt_p++;
else cnt_f++;
p++;
}
printf("%d %d %d\n", cnt_a, cnt_p, cnt_f);
}
这样?
#include<stdio.h>
#define MAXN 100
struct student {
char name[20];
int score;
};
void cnt_score( struct student *p, int n );
int main()
{
int i, n;
struct student stu[MAXN];
scanf("%d", &n);
for(i = 0; i < n; i++) {
scanf("%s%d", stu[i].name, &stu[i].score);
}
cnt_score(stu, n);
return 0;
}
void cnt_score(struct student *p, int n)
{
int cnt_a = 0, cnt_p = 0, cnt_f = 0;
//填空1
while ( n--) {
if (p[cnt_a].score>=80&&p[cnt_a].score<=100) //填空2
cnt_a++;
else if (p[cnt_p].score>=60&&p[cnt_p].score<=79) //填空3
cnt_p++;
else cnt_f++;
p++;
}
printf("%d %d %d\n", cnt_a, cnt_p, cnt_f);
}
我是这样写的:
填空1:struct student *q=p+n-1 ;
填空2:p->score>=80 && p->score<101
填空3:p->score>=60&&p->score<80
测试通过。
感觉结构体跟数组很像,struct student stu【MAXN】很像是定义了一个一维数组,从这个角度看结构体指针会好些吧