View Code of Problem 5

#include <stdio.h>
struct fruit
{
	int apple;
	int pear;
	int num;
};
int main()
{
	struct fruit a[1000],flag;
	int t, m, n, i, j, k, p;
	scanf("%d", &t);
	for (k = 1; k <= t; k++)
	{
		scanf("%d %d", &n, &m);
		for (j = 0; j < n; j++)
		{
			scanf("%d %d", &a[j].apple, &a[j].pear);
			a[j].num = j;
		}
		for (i = 0; i < n - 1; i++)
		{
			p = i;
			for (j = i; j < n; j++)
			{
				if (a[p].apple < a[j].apple)p = j;
				else if (a[p].apple == a[j].apple&&a[p].pear <a[j].pear)p = j;
				else if (a[p].apple == a[j].apple&&a[p].pear == a[j].pear&&a[p].num > a[j].num)p = j;
			}
			if (p != i)
			{
				flag = a[p];
				a[p] = a[i];
				a[i] = flag;
			}
		}
		for (i = 0; i < m; i++)
		{
			if (i != m - 1)
				printf("%d ", a[i].num + 1);
			else
				printf("%d", a[i].num + 1);
		}
		printf("\n");
	}
}

Double click to view unformatted code.


Back to problem 5