View Code of Problem 83

#include <stdio.h>

struct stu {
	int id;
	int score;
	struct stu *next;
};

int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	struct stu *head, *q, *p;
	q = NULL;
	head = (struct stu *)malloc(sizeof(struct stu));
	int i;
	for (i = 0; i < n+m; i++) {
		p = (struct stu *)malloc(sizeof(struct stu));
		scanf("%d", &p->id);
		scanf("%d", &p->score);
		if(q==NULL){
			head->next = q = p;
		}
		else {
			q->next = p;
			q = p;
		}
	}
	q->next = NULL;
//结束创建链表
	struct stu *stu_min, *stu_min_front, *p_front, *q_front;
	p = head->next;
	p_front = head;
	stu_min_front = head;
	int min;	
	while (p != NULL) {
		q = p->next;
		q_front = p;
		min = p->id;
		stu_min = p;
		while (q != NULL) {
			if (min > q->id) {
				min = q->id;
				stu_min = q;
				stu_min_front = q_front;
			}
			q = q->next;
			q_front = q_front->next;
		}
		if (stu_min != p) {
			stu_min_front->next = stu_min->next;
			p_front->next = stu_min;
			stu_min->next = p;
			p_front = stu_min;
		}
		else {
			p = p->next;
			p_front = p_front->next;
		}
	}
	p = head->next;
	while (p!=NULL) {
		printf("%d %d\n", p->id, p->score);
		p = p->next;
	}
}

Double click to view unformatted code.


Back to problem 83