andy6804tw
8/15/2016 - 3:03 PM

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1361 這題就只是紀錄國家數目(國家要排序)注意一開始可能有空白所以要用tirm(

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1361

這題就只是紀錄國家數目(國家要排序)注意一開始可能有空白所以要用tirm( )把前面空白省略

import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int n=Integer.parseInt(scn.nextLine()),count=0,i=0;
		String arr[]=new String[n];
		for(i=0;i<n;i++){
			String s[]=scn.nextLine().trim().split(" ");
			arr[i]=s[0];
		}
		Arrays.sort(arr);
		String str=arr[0];
		for(i=0;i<n;i++){
			if(str.equals(arr[i]))
				count++;
			else{
				System.out.println(arr[i-1]+" "+count);
				count=1;
				str=arr[i];
			}
		}
		System.out.println(arr[i-1]+" "+count);
	}
	/* 
    題目:Q10420 - List of Conquests
    作者:1010
    時間:西元 2016 年 8 月 */
}