View Code of Problem 5

 #include <stdio.h>
 #include <stdlib.h>
 #include<string.h>
typedef struct fruit{
 	int apple;
 	int orange;
 	int id;
 }fruit;
 
 int compare(const void *a,const void *b){
 	if((*(fruit *)a).apple==(*(fruit *)b).apple)
 	 return (*(fruit *)b).orange-(*(fruit *)a).orange;
 	else 
	 return  (*(fruit *)b).apple-(*(fruit *)a).apple;
 }
 int main(){
 	int n;
 	scanf("%d",&n);
	 while(n>0){	
	int x,y,i;
	scanf("%d %d",&x,&y);
	struct fruit fru[x];
	for(i=0;i<x;i++){
		fru[i].id=i+1;
		scanf("%d %d",&fru[i].apple,&fru[i].orange);
	}
	qsort(fru,x,sizeof(fru[0]),compare);
 	for(i=0;i<y;i++){
 		printf("%d",fru[i].id);
 		if(i!=y-1) printf(" ");
	 }
	 printf("\n");
	 n--;
	}
	 return 0;
 }

Double click to view unformatted code.


Back to problem 5