PTA 名人堂与代金券 Java

在这里插入图片描述
在这里插入图片描述

思路:使用邻接表的结构来存储数据,分数做下标,账号存链表。
插入的时候在链表中使用插入排序的方式(其他排序方式也可以)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int N = Reader.nextInt();
		int G = Reader.nextInt();
		int K = Reader.nextInt();
		// 使用邻接表结构,分数做下标,账号放在链表中(使用插入排序的方式)
		node[] map = new node[101];
		for (int i = 0; i < N; i++) {
			String id = Reader.next();
			int grade = Reader.nextInt();
			// 标记分数是否出现过,只分配空间给出现过的分数
			if (map[grade] == null) {
				map[grade] = new node();
			}
			map[grade].insert(id);
		}
		
		// 计算发出去的pat代金券
		int price_pat = 0;
		
		for (int i = map.length - 1; i >= 0; i--) {
			if (map[i] == null)
				continue;
			if (i >= G) {
				price_pat += 50 * map[i].list.size();
			} else if( i >= 60) {
				price_pat += 20 * map[i].list.size();
			}
		}

		System.out.println(price_pat);
		int rank = 1;
		// 打印排名
		for (int i = map.length - 1; i >= 0; i--) {
			if (map[i] == null)
				continue;
			if (rank > K) {
				break;
			}
			int tmp = rank;
			for (String item : map[i].list) {
				System.out.println(tmp + " " + item + " " + i);
				rank++;
			}

		}

	}

	static class node {
		LinkedList<String> list = new LinkedList<String>();
		
		// 插入的时候排序
		void insert(String id) {
			int index = 0;
			for (String string : list) {
				char[] o1 = string.toCharArray();
				char[] o2 = id.toCharArray();
				int len = o1.length > o2.length ? o2.length : o1.length;
				boolean isFind = false;
				for (int i = 0; i < len; i++) {
					 // 如果字母一样,则继续查对比
					if(o1[i] != o2[i]) {
						// 如果字母不一样,则判断是否是在当前位置
						if(o1[i] > o2[i]){
							// 是在当前位置,位置已经找到
							isFind = true;
						}
						break;
					}
				}
				if (isFind) {
					break;
				}
				index++;
			}
			list.add(index, id);
		}
	}

}

// 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("");
	}

	static void init(File file) {
		try {
			reader = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		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 int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static char nextChar() throws IOException {
		return next().toCharArray()[0];
	}

	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}

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

发布了27 篇原创文章 · 获赞 2 · 访问量 1465

猜你喜欢

转载自blog.csdn.net/Samil_Hy/article/details/104209054