Hdoj Let the Balloon Rise (Java)

Source: Internet
Author: 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 are to balloons floating around. But to tell your a secret, the judges ' favorite time is guessing the most popular. When the contest was over, they'll count the balloons of each color and find the result.

This year, the they decide to the leave this lovely job.

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 lower-case letters.

A test Case with N = 0 terminates the "input" and "This" test case is processed.

Output for each case, print the "color of balloon for the" most popular problem on a single line. It is guaranteed this there is a unique solution to each test case.

Sample Input
5 green red Blue Red Red 3 pink Orange Pink 0
Sample Output
Red Pink WA Code: The use of two-tier for loop statistics, but always error, do not know where the wrong, but also please Daniel pointing ...
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 Code: Using Map Collection class statistics
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);
 }

    }

}






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.