PAT A1025 (C语言)链表&数组

代码虐我千百遍,我待代码如飞鸟

代码不娇滴滴的说:Segmentation fault ( core dumped )

代码冷漠的说:segmentaion fault ( core dumped )

生活就是如此哲学,任何结果会来的让你明明白白
代码成熟了,上码

链表

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

typedef struct info{
	int score;
	unsigned long long int ID;
	int f_rank;
	int loc;
	int l_rank;
	struct info *next;
}info;
typedef struct head{
	int loc;
	struct head *next;
	struct info *first_info;
}head;
int local_sort(struct info *first){
	int rank = 1;
	struct info *p, *q, *last_marked;
//	p = malloc(sizeof(info));		//不是 ,是其他错误
//	q = malloc(sizeof(info));    //如果不分配这个不工作吗??
	last_marked = malloc(sizeof(info));
	last_marked->score = -1;
	p = first;
	while(p != NULL){
		q = p->next;
		while(q != NULL){
			if(q->l_rank == 0)
				if(q->score > p->score)
					p = q;
			q = q->next;
		}
		if(p->score != last_marked->score){
			p->l_rank = rank++;
			last_marked = p;
		}else{
			p->l_rank = last_marked->l_rank;
			++rank;
//			last_marked = p;
		}
		p = first;
		//test rank
//		printf("%d\n", rank);
		while(p != NULL && p->l_rank != 0)   /* 找下一个未排序的数 */
			p = p->next;
	}
	return 0;
}
int final_sort(struct info *first_node){
	int rank = 1;
	struct info *p, *q, *last_marked;
	last_marked = malloc(sizeof(info));
	last_marked->score = -1;
	p = first_node;
	while(p != NULL){
		q = p->next;
		while(q != NULL){
			if(q->f_rank == 0)
				if(q->score > p->score)
					p = q;
			q = q->next;
		}
		if(p->score != last_marked->score){
			p->f_rank = rank++;
			last_marked = p;
		}else{
			p->f_rank = last_marked->f_rank;
			++rank;
//			last_marked = p;
		}
		printf("%14lld %3d %3d %3d\n", p->ID, p->f_rank, p->loc, p->l_rank);
		p = first_node;
		while(p != NULL && p->f_rank != 0)
			p = p->next;
	}
	return 0;
}
int main(){
	int N, K;						/* N 是总地区,K 是某地区总人数 */
	printf("places:");
	scanf(" %d", &N);
	//make head list
	struct head *HEAD, *p;
	HEAD = malloc(sizeof(head));     /* 一个地区建一个链表,将各个头结点连起来 */
	HEAD->next == NULL;
	for(int i = 0; i < N; ++i){
		p = malloc(sizeof(head));
		p->next = HEAD->next;
		p->loc = N-i;
		HEAD->next = p;
	}
	//input
	struct info *q;
	q = malloc(sizeof(info));     /* 依次输入各个地区的每一个人的信息,头插法 */
	p = HEAD->next;
	while(p != NULL){
		p->first_info = NULL;
		printf("peoples in the %d location :", p->loc);
		scanf("%d", &K);
		for(int i = 0; i < K; ++i){
			q = malloc(sizeof(info));
			scanf("%lld %d", &q->ID, &q->score);
			q->loc = p->loc;
			q->l_rank = 0;
			q->f_rank = 0;
			q->next = p->first_info;
			p->first_info = q;
		}
		p = p->next;
	}
	//local_sort;
	p = HEAD->next;
	while(p != NULL){
		q = p->first_info;
		local_sort(q);
		p = p->next;
	}
	//test output
/*	p = HEAD->next;
	for(int i = 0; i < N; ++i){
		q = p->first_info;
		while(q != NULL){
			printf("%14lld  %3d  %3d  %3d\n", q->ID, q->l_rank, q->loc, q->score);
			q = q->next;
    	}
		p = p->next;
	}
*/
	//merge to become ont list
	p = HEAD->next;
	while(p != NULL){
		q = p->first_info;
		while(q->next != NULL){
			q = q->next;
		}
		p = p->next;
		if(p != NULL)
			q->next = p->first_info;
	}
	//free
	//
/*	//TEST OUTPUT
	p = HEAD->next;
	q = p->first_info;
	while(q != NULL){
		printf("%14lld %3d %3d %3d\n", q->ID, q->f_rank, q->loc, q->l_rank);
		q = q->next;
	}
*/
	//final sort and print
	p = HEAD->next;
	q = p->first_info;
	final_sort(q);

	return 0;
}

数组

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

typedef struct info{
	int score;
	unsigned long long int ID;
	int f_rank;
	int loc;
	int l_rank;
}info;
int sort_loc(struct info a[], int left, int right){
	int MAX, LAST;
	int rank = 1;
	int i = left, j;
	int n = right - left;
	
	MAX = left;
	LAST = -1;
	for(j = n; j > 0; --j){
		for(i = left; i < right; ++i){
			if(a[i].l_rank == 0 && a[i].score > a[MAX].score)
				MAX = i;
		}
		if(a[MAX].score != a[LAST].score || LAST == -1){
			a[MAX].l_rank = rank++;
			LAST = MAX;
		}else{
			a[MAX].l_rank = a[LAST].l_rank;
			++rank;
		}
		i = left;
		while(a[i].l_rank != 0 && i < right)
			++i;
		MAX = i;
	}
}
int sort_all(struct info a[], int sum){
	int MAX = 0, LAST = -1;
	int rank = 1;
	
	int j;
	for(int i = 0; i < sum; ++i){
		for(j = 0; j < sum; ++j){
			if(a[j].f_rank == 0 && a[j].score > a[MAX].score)
				MAX = j;
		}
		if(LAST == -1 || a[MAX].score != a[LAST].score){
			a[MAX].f_rank = rank++;
			LAST = MAX;
			printf("%14lld %3d  %3d  %3d\n", a[MAX].ID, a[MAX].f_rank, a[MAX].loc, a[MAX].l_rank);
		}else{
			a[MAX].f_rank = a[LAST].f_rank;
			++rank;
			printf("%14lld %3d  %3d  %3d\n", a[MAX].ID, a[MAX].f_rank, a[MAX].loc, a[MAX].l_rank);
		}
		j = 0;
		while(a[j].f_rank != 0 && j < i)
			++j;
		MAX = j;
	}
}
int main(){
	struct info a[30000] = {0};
	int N, K, sum = 0, j;
	printf("how many place take this exam:");
	scanf("%d", &N);
	for(int i = 1; i <= N; ++i){
		printf("how many people in %d place  ", i);
		scanf("%d", &K);		
		for(j = sum; j < sum + K; ++j){
			scanf("%14lld %d", &a[j].ID, &a[j].score);
			a[j].loc = i;
		}
		sort_loc(a, sum, j);
		sum = j;
	}
	sort_all(a, sum);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值