HDU - 1263(java map nesting, set sorting)

This problem is more comprehensive, using set sorting, and map double nesting, but also for java class application. If you have time, you need to watch it several times........

Summer is coming ~ ~ so happy, ha ha, so many fruits ~ ~
Joe runs a small fruit shop. He thinks the way to survive is to run the most popular fruit. Now he wants a detailed list of fruit sales so that Joe can easily master the sales of all fruits

Input

The first positive integer N (0 < N < = 10) indicates that there are N groups of test data
The first line of each group of test data is an integer M (0 < M < = 100), which means there are M successful transactions. Then there are M lines of data, each line represents a transaction, which is composed of fruit name (lowercase letters, length no more than 80), fruit origin (lowercase letters, length no more than 80) and the number of fruits traded (positive integer, no more than 100)

Output

For each set of test data, please output a list of fruit sales in correct format (please analyze the sample output). This list includes the origin, name and number of sales information of all fruits. Fruits are classified by origin first, and the origin is arranged alphabetically; fruits of the same origin are sorted by name, and the names are sorted alphabetically
There is a blank line between the two sets of test data. There is no blank line after the last set of test data

Sample Input

1
5
apple shandong 3
pineapple guangdong 1
sugarcane guangdong 1
pineapple guangdong 3
pineapple guangdong 1

Sample Output

guangdong
   |----pineapple(5)
   |----sugarcane(1)
shandong
   |----apple(3)

Sponsor

 

//package javasetmapvector1;
import java.util.*;
class Node{
	
	
	static TreeSet<String> sortSet=new TreeSet<>(new Comparator<String>(){
		public int compare(String s1,String s2) {
			return s1.compareTo(s2);
		}
	});
	HashMap<String,Integer> node;
	public Node(){
		node=new HashMap<String,Integer>();
	}
	public void Add(String name,Integer number)
	{
		Integer n=node.get(name);
		if(n==null)
		{
			node.put(name, number);
		}
		else {
			node.put(name, n+number);
		}
	}
	public void sys()
	{
	     Set<String> ss=node.keySet();
	     sortSet.addAll(ss);
	     Iterator<String> it=sortSet.iterator();
	     while(it.hasNext())
	     {
	    	 String name=it.next();
	    	 Integer nub=node.get(name);
	    	 System.out.println("   |----"+name+"("+nub+")");
	     }
	     sortSet.clear();
	}
}
public class Main {
	static TreeSet<String> sortSet=new TreeSet<>(new Comparator<String>(){
		public int compare(String s1,String s2) {
			return s1.compareTo(s2);
		}
	});
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int T=sc.nextInt();
		//sc.nextLine();
		for(int j=0;j<T;j++)
		{
			HashMap<String,Node> map=new HashMap<String,Node>();
			int n=sc.nextInt();
			for(int i=0;i<n;i++)
			{
				String name=sc.next();
				String place=sc.next();
				int number=sc.nextInt();
				Node w=map.get(place);
				if(w==null)
				{
					Node nod=new Node();
					nod.Add(name, number);
					map.put(place,nod);
					
				}
				else
				{
					w.Add(name, number);
					map.put(place,w);
					
				}
				
			}
			Set<String> k=map.keySet();
			sortSet.addAll(k);
			Iterator<String> it=sortSet.iterator();
			while(it.hasNext())
			{
				String palce1=it.next();
				Node no=map.get(palce1);
				System.out.println(palce1);
				no.sys();
				
			}
			sortSet.clear();
			
			
				if(j!=T-1) System.out.println();
			}
		}
	
}

 

Published 209 original articles, won praise 16, visited 10000+
Private letter follow

Tags: Java

Posted on Mon, 09 Mar 2020 23:20:01 -0700 by OldManRiver