HDOJ Let the Balloon Rise(java)

來源:互聯網
上載者:User
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 69974    Accepted Submission(s): 26018


Problem Description Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.
 
Input Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
 
Output For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 
Sample Input
 5 green red blue red red 3 pink orange pink 0   

Sample Output
 red pink WA代碼:利用兩層for迴圈統計,但是總是報錯,不知道錯在哪裡,還請大牛指點。。。 
import java.util.Scanner;public class Main {        public static void main(String[] args) {                Scanner in = new Scanner(System.in);        while(in.hasNext()){            int n = in.nextInt();            if(n == 0)                break;            String[]  str = new String[n];            boolean[] flag = new boolean[n];            for(int i=0; i<n; i++){                str[i] = in.next();                flag[i] = false;            }            int count = 1;            int max = Integer.MIN_VALUE;            int maxIndex = 0;            for(int i=0; i<n-1; i++){                if(flag[i] == true)                    continue;                flag[i] = true;                for(int j=i+1; j<n; j++){                    if(str[i].trim().equals(str[j].trim())){                        count++;                        flag[j] = true;                    }                }                                if(count > max){                    maxIndex = i;                    max = count;                    count = 1;                }            }                        System.out.println(str[maxIndex]);                    }    }}



AC代碼:利用map集合類統計
import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Scanner;public class Main {        public static void main(String[] args) {                Scanner in = new Scanner(System.in);        while(in.hasNext()){            int n = in.nextInt();            int number = 1;            if(n == 0)                break;            Map<String,Integer> map = new HashMap<String,Integer>();            for(int i=0; i<n; i++){                String str = in.next();                if(!map.containsKey(str)){                    map.put(str,number);                }else{                    int temp = map.get(str);                    map.put(str,++temp);                }            }            int max = Integer.MIN_VALUE;            String maxColor = "";            Iterator<?> it = map.keySet().iterator();            while(it.hasNext()){                String key = (String) it.next();                int value = map.get(key);                if(value > max){                    max = value;                    maxColor = key;                }            }            System.out.println(maxColor);                    }    }}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.