【PAT B1004】成绩排名(C语言)

认真、朴素、真实的活着,让他们成为好的见证,Because we share something so profound; Because we share one phrase so profound!
本文两个部分:
1 :第一次出现小错误
2:修补了之前的小错误
在这里插入图片描述
总是差几分,人生难完美啊,这就是继续的理由吗?

#include <stdio.h>
#include <stdlib.h>

#define MAX  10

typedef struct info{
	char name[MAX + 1];
	char id[MAX + 1];
	int score;
}info;
/* read a student`s infomation to a */
void get_info(struct info *a){
	int c;
		
	scanf("%s", a->name);
	c = getchar();
	scanf("%s", a->id);
	c = getchar();
	scanf("%d", &a->score);
}
/* copy a student`s infomation form "frome" to "to" */
void copy(struct info *to, struct info *from){
	int i;
	
	for(i = 0; (to->name[i] = from->name[i]) != '\0'; ++i);
	to->name[i] = '\0';
	for(i = 0; (to->id[i] = from->id[i]) != '\0'; ++i);
	to->id[i] = '\0';
	to->score = from->score;
}
//print the max and min infomation;
int main(){
	struct info *max = malloc(sizeof(info));
	struct info *min = malloc(sizeof(info));
	struct info *a = malloc(sizeof(info));
	int i, n;
	
	scanf("%d", &n);

	for(i = 0; i < n; ++i){
		get_info(a);
		if( i < 2){
			if(i == 0)
				copy(max, a);
			else if(a->score > max->score){
				copy(min, max);
				copy(max, a);
			}else
				copy(min, a);
		
		}else if(a->score > max->score)
			copy(max, a);
		else if(a->score < min->score)
			copy(min, a);
	}
	printf("%s %s\n", max->name, max->id);
	printf("%s %s\n", min->name, min->id);
	return 0;
}

咦!被我发现了,原来是:若只有一个人输入,输出最大最小都是那个人,
人生啊! 还需认真啊,还需细心啊,真需千百万时的修炼啊
在这里插入图片描述
正确代码:

#include <stdio.h>
#include <stdlib.h>

#define MAX  10

typedef struct info{
	char name[MAX + 1];
	char id[MAX + 1];
	int score;
}info;
void get_info(struct info *a){
	int c;
		
	scanf("%s", a->name);
	c = getchar();
	scanf("%s", a->id);
	c = getchar();
	scanf("%d", &a->score);
}
void copy(struct info *to, struct info *from){
	int i;
	
	for(i = 0; (to->name[i] = from->name[i]) != '\0'; ++i);
	to->name[i] = '\0';
	for(i = 0; (to->id[i] = from->id[i]) != '\0'; ++i);
	to->id[i] = '\0';
	to->score = from->score;
}
int main(){
	struct info *max = malloc(sizeof(info));
	struct info *min = malloc(sizeof(info));
	struct info *a = malloc(sizeof(info));
	int i, n;
	
	scanf("%d", &n);

	for(i = 0; i < n; ++i){
		get_info(a);
		if( i < 2){
			if(i == 0)
				copy(max, a);
			else if(a->score > max->score){
				copy(min, max);
				copy(max, a);
			}else
				copy(min, a);
		
		}else if(a->score > max->score)
			copy(max, a);
		else if(a->score < min->score)
			copy(min, a);
	}
	if(n == 1){
		printf("%s %s\n", max->name, max->id);
		printf("%s %s\n", max->name, max->id);
	}
	else{
		printf("%s %s\n", max->name, max->id);
		printf("%s %s\n", min->name, min->id);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值