View Code of Problem 5

#include "stdio.h"
#include "stdlib.h"

typedef struct
{	
	int data ;
	int pear ;
	int num ;
	struct Num* node;
}Num ;

void swapData( Num * a , Num * b )
{
	int data , num , pear;

	data = a->data ;
	num  = a->num ;
	pear = a->pear ;

	a->data = b->data ;
	a->num = b->num ;
	a->pear = b->pear ;

	b->data = data ;
	b->num = num ;
	b->pear = pear ;
}


void getResult()
{
	int count , m ,  sum ,  i ;
	Num * head = (Num*)malloc(sizeof(Num));
	Num * num  = head ;

	scanf("%d %d" , &count , & m);
	sum = count ;

	head->data = 0;
	head->num = 0 ;
	head->pear = 0 ;
	head->node = NULL ;

	while(count--)
	{
		int temp , pear;
		Num * node = (Num*)malloc(sizeof(Num));
		Num * tempHead = head; 

		scanf("%d %d" , &temp , &pear);
		node->data = temp ;
		node->pear = pear ;
		node->node = NULL ;
		node->num = sum - count ;

		if(tempHead->node == NULL)
		{
			swapData( node , tempHead );
			tempHead ->node = node ;
			continue ;
		}

		while(tempHead->node != NULL)
		{
			if(node->data > tempHead->data)
			{
				swapData(tempHead , node);
			}
			else if( node->data == tempHead->data)
			{
				if(node->pear > tempHead->pear )
				{
					swapData(tempHead , node) ;
				}
			}

			tempHead = tempHead->node ;
		}

		if( node->data > tempHead->data )
		{
			swapData(node , tempHead);
		}
		else if( node->data == tempHead->data)
		{
			if(node->pear > tempHead->pear )
			{
				swapData(tempHead , node) ;
			}
		}

		tempHead->node = node ;
	}
	
	for(i =0 ; i < m -1 ; i++)
	{
		printf("%d " , head->num );
		head = head->node ;
	}
		printf("%d\n" , head->num);

}

int main(void)
{
	int num ; 
	scanf("%d" , &num);

	while(num -- )
	{
		getResult();		
	}

	return 0 ;
}

Double click to view unformatted code.


Back to problem 5