让气球飞起来
這是一道我們大一新生C語言課上我認為比較困難的題我想了很久才有一個思路,我認為很有必要記錄下這道題。
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
說明
再來一次比賽!看到氣球飄來飄去真是太興奮了。但告訴你一個秘密,評委們最喜歡的時間是猜最流行的問題。比賽結束后,他們會清點每種顏色的氣球并找出結果。
今年,他們決定把這份可愛的工作留給你。
輸入
輸入包含多個測試用例。每個測試用例以一個數字N(0<N<=1000)開始,即分布的氣球總數。接下來的N行每行包含一種顏色。氣球的顏色是由多達15個小寫字母組成的字符串。
N=0的測試用例終止輸入,不處理此測試用例。
輸出
每一個氣球最流行的問題是每行的顏色。保證每個測試用例都有一個唯一的解決方案。
樣本輸入
5
green
red
blue
red
red
3
pink
orange
pink
0
樣品輸出
red
pink
題意
這道題主要是要我們輸入幾種顏色,然后找出顏色最多的一種。輸入一個整數N代表N個氣球,然后依次輸入N個氣球的顏色,最后輸出一個最多氣球的顏色。
主要思路
這題要用到有關字符數組的知識,我們依次輸入顏色名稱的字符串,首先我們定義兩個數組,字符串a【】和整數數組b【】,和一個Max,我們依次輸入顏色名稱的字符串存入a【】,我們可以每次找出一個字符串然后在剩下的字串中找相同的并統計個數,如果這個次數最大,就把值賦給Max,最后輸出最大次數的串。
代碼如下
注意點
我認為這道題需要注意每次循環之后要把b【】清零,否則系統會出錯。
還有就是這道題關鍵要定義兩個數組,一個記錄最大次數,一個記錄最大次數字符的位置。
總結
- 上一篇: 边界行动
- 下一篇: 瑶琳c语言,中国浙江省的瑶琳仙境属于