View Code of Problem 3861

#include<stdio.h>
#include<string.h>
struct phone
{
	char name[20];
	int p;
	int q;
};
int main()
{
	int n,i,j,t,k;
	scanf("%d",&t);
	for(k=0;k<t;k++)
	{
	scanf("%d",&n);
	struct phone ph[100];
	int a[100];
	for(i=0;i<n;i++)
	{
		scanf("%s%d%d",ph[i].name,&ph[i].p,&ph[i].q);
		a[i]=ph[i].p/ph[i].q;
	}
	char b[100];
	int p1,q1,h;
	for(i=0;i<n-1;i++)
	{
		for(j=0;j<n-i-1;j++)
		{
			if(a[j]>a[j+1])
			{
				h=a[j];
				a[j]=a[j+1];
				a[j+1]=h;
		         strcpy(b,ph[j].name);
		         strcpy(ph[j].name,ph[j+1].name);
		         strcpy(ph[j+1].name,b);
				p1=ph[j].p;
				ph[j].p=ph[j+1].p;
				ph[j+1].p=p1;
				q1=ph[j].q;
				ph[j].q=ph[j+1].q;
				ph[j+1].q=q1;
				
			}
		}
	}
	for(i=n-1;i>=0;i--)
		printf("%d %s %d %d\n",n-i,ph[i].name,ph[i].p,ph[i].q);
   } 	
	return 0;
}

Double click to view unformatted code.


Back to problem 3861