View Code of Problem 3861

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct{
	char name[20];
	float a,b;
}phone;
phone m[100];
int cmp(const void*a,const void *b)
{
	phone *pa=(phone *)a;
	phone *pb=(phone *)b;
	float num1=pa->a/pa->b;
	float num2=pb->a/pb->b;
	if(num1>num2)
	{
		return -1;
	}
	else
	{
		return 1;
	}
}
int main() {
	int t,i,j;
	scanf("%d",&t);
	while(t--)
	{
		int n,k=1;
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%s %f %f",m[i].name,&m[i].a,&m[i].b);
		}
		qsort(m,n,sizeof(phone),cmp);
		for(i=0;i<n;i++)
		{
			printf("%d %s %.f %.f\n",k++,m[i].name,m[i].a,m[i].b);
		}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 3861