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.
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.
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
思路
1.逐个读入(n)
2.对比第I个是否已经读入过,若有,则指示该类型增1,若无增添加该类型,若该类型的个数过半,则该类型为最多的。
最差的情况是,n个都互不相同,n(n-1+ n-2 + n-3 。。。。。+1),0.5*n^3
最好的情况是 n
还好AC了。
1
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace hdoj { class Programweb { private static StringBuilder sb = new StringBuilder(); static void Main(string[] args) { // //DateTime d1 = DateTime.Now;//local // var lines = File.ReadLines("input2.txt").ToList();//local int num = 0; int lineIndex = 0; int lineNumIndex = -1; //获取行数 while (true) { string s1 = ""; List<string> keys = new List<string>(); List<int> values = new List<int>(); bool keyExist = false; int index = 0; // int max = 0; if (int.TryParse(Console.ReadLine(), out num)) {//web // lineNumIndex = lineNumIndex + num + 1;//local 指示数字行所在的行数=之前数字行所在的位置+上次的行数+数字本身的站1行 // if (int.TryParse(lines[lineNumIndex], out num)) {//local for (int i = 0; i < num; i++) { s1 = Console.ReadLine();//web // s1 = lines[i + lineNumIndex+1]; //local // handOneLine(s1, i,num); for (int j = 0; j < keys.Count; j++) { if (keys[j] == s1) { keyExist = true; index = j; break; } else { keyExist = false; } } //是否存在不同处理 if (keyExist) { values[index]++; if (values[index] > num / 2) { // max = index; break; } } else { keys.Add(s1); values.Add(1); } } if (num == 0) { break; } //一次结束 if (values.Count != 0) { int max = 0; int maxIndex = 0; for (int i = 0; i < values.Count; i++) { if (values[i]>max) { maxIndex = i; max = values[i]; } } sb.AppendLine(keys[maxIndex]); } keys.Clear(); values.Clear(); // // sb.AppendLine(string.Format("Case {0}:", i)); //sb.AppendLine(); } } Console.Write(sb.ToString()); //DateTime d2 = DateTime.Now; //TimeSpan ts1 = d2 - d1; //Console.WriteLine("Milliseconds:" + ts1.Milliseconds); // Console.Read();//local } } }
2