View Code of Problem 3861

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int t = scanner.nextInt();
		
		for(int i=0; i<t; i++) {
			int n = scanner.nextInt();
			Phone[] ps = new Phone[n];
			int h=1;
			for(int j=0; j<n; j++) {
				ps[j] = new Phone(scanner.next(),scanner.nextInt(),scanner.nextInt());
			}
			for(int j=0; j<n-1; j++) {
				for(int k=j+1; k<n; k++) {
					if(ps[j].index < ps[k].index) {
						Phone p = ps[j];
						ps[j] = ps[k];
						ps[k] = p;
					}
				}
			}
			
			for(Phone p:ps) {
				System.out.println(h++ + " " + p.name + " " + p.score + " " + p.money) ;
			}
		}
	}
}
class Phone implements Comparable<Phone>{
	String name;
	int score;
	int money;
	double index;
	public Phone(String name, int score, int money) {
		super();
		this.name = name;
		this.score = score;
		this.money = money;
		this.index = (double)score/money;
	}
	@Override
	public int compareTo(Phone o) {
		// TODO Auto-generated method stub
		return (int) (o.index - this.index);
	}
}

Double click to view unformatted code.


Back to problem 3861