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