L2-3 Hall of fame and voucher (25 points) java

For MOOC in Chinese Universities( http://www.icourse163.org/ )For students studying "data structure" course, if they want to obtain a certificate of competency, they must have a total score of 60 or above, plus benefits: those with a total score of [G, 100] can get a 50 yuan PAT voucher; those with a total score of [60, g] can get a 20 yuan PAT voucher. The national examination center is universal and valid for one year. At the same time, the teacher will include the top K students in the "Hall of fame". Please write a program to help the teacher list the students in the hall of fame and count the par value of PAT vouchers.

Input format:

Input three integers given in the first line, namely, n (positive integer no more than 10000, which is the total number of students), G (integer in the (60100) range, which is the grade dividing line of the voucher described in the question surface), K (positive integer no more than 100 and no more than N, which is the lowest ranking in the hall of fame). Next N lines, each line gives a student's account number (string with no more than 15 characters in length and no spaces) and total score (integer in the range [0, 100]), separated by spaces. There are no duplicate accounts.

Output format:

First, output the total face value of the issued PAT voucher in one line. Then, the student's ranking, account number and score are output in non ascending order according to the total score, separated by one space. It should be noted that students with the same score enjoy the same ranking. When ranking in parallel, they are output in ascending alphabetical order according to the account number.

Input example:

10 80 5
cy@zju.edu.cn 78
cy@pat-edu.com 87
1001@qq.com 65
uh-oh@163.com 96
test@126.com 39
anyone@qq.com 87
zoe@mit.edu 80
jack@ucla.edu 88
bob@cmu.edu 80
ken@163.com 70

Output example:

360
1 uh-oh@163.com 96
2 jack@ucla.edu 88
3 anyone@qq.com 87
3 cy@pat-edu.com 87
5 bob@cmu.edu 80
5 zoe@mit.edu 80

Last test point timeout

import java.io.IOException;
import java.io.*;
import java.util.*;

//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}
	static char nextChar() throws IOException {
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
}

public class Main {
	static int n;
	static int g;
	static int k;
	static class Person{
		String account;
		int grade;
	}
	static Person[]person;
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		n = Reader.nextInt();
		g = Reader.nextInt();
		k = Reader.nextInt();
		person = new Person[n+1];
		person[0] = new Person();
		for (int i = 1; i <= n; i++) {
			person[i] = new Person();
			person[i].account = Reader.next();
			person[i].grade = Reader.nextInt();
		}
		Arrays.sort(person, 1, n+1, new Comparator<Person>() {

			@Override
			public int compare(Person o1, Person o2) {
				// TODO Auto-generated method stub
				if (o1.grade<o2.grade) {
					return 1;
				} else if(o1.grade ==o2.grade) {
					return o1.account.compareTo(o2.account);
				} else {
					return -1;
				}
				
			}
		});	
		int sum = 0;	
		for (int i = 1; i < person.length; i++) {
			if (person[i].grade>=g) {
				sum+=50;
			} else if(person[i].grade>=60){
				sum+=20;
			} else {
				break;
			}
		}
		System.out.println(sum);
		int rank = 1;
		for (int i = 1; i < person.length; i++) {
			if (person[i].grade==person[i-1].grade) {
				System.out.println(rank+" "+person[i].account+" "+person[i].grade);
			} else {
				if (i>k) {
					break;
				}
				System.out.println(i+" "+person[i].account+" "+person[i].grade);
				rank = i;
			}
		}
	}
}

Published 62 original articles, won praise 9, visited 2287

Tags: Java

Posted on Fri, 07 Feb 2020 06:09:30 -0800 by updwebmaster