View Code of Problem 5

#include <stdio.h>

int insert(int *tmp, int *value, int *where, int all) {
	int i;
	for (i = all; i > *where; --i) {
		tmp[i] = tmp[i - 1];
	}
	tmp[*where] = *value;
	return 0;
}

int main()
{
	int turn;
	scanf("%d", &turn); // 获取轮数
	getchar();
	int turn_now;
	// 开始轮
	for (turn_now = 1; turn_now <= turn; ++turn_now) {
		int all, can;
		scanf("%d %d", &all, &can); // 获取总篮子和可选篮子数
		getchar();
		int select[10000] = { 0 };
		int blanket, tmp_blanket;
		int apple[10000], pear[10000];
		for (blanket = 0; blanket < all; ++blanket) {
			// 读取苹果和梨
			scanf("%d %d", &apple[blanket], &pear[blanket]);
			getchar();
			int i;
			// 开始遍历判断
			for (i = 0; i < blanket; i++) {
				tmp_blanket = select[i];
				if (apple[blanket] > apple[tmp_blanket] || (apple[blanket] == apple[tmp_blanket] && pear[blanket] > pear[tmp_blanket])) {
					insert(select, &blanket, &i, blanket);
					break;
				}
			}
			// 遍历没有插入
			if (i == blanket) {
				select[blanket] = blanket;
			}
		}
		int a;
		for (a = 0; a < can; a++) {
			if (a == 0) {
				printf("%d", select[a]+1);
			}
			else {
				printf(" %d", select[a]+1);
			}
		}
		printf("\n");
		if (turn_now < turn) {
			getchar();
		}
	}
}

Double click to view unformatted code.


Back to problem 5