2020年4月26日 星期日

[C_MM243-易] 找出眾數

[C_MM243-易] 找出眾數

成績: 0 / 倒扣: 0.8
Problem Description
找出一串數列 ( 數字與數字間以空格區隔 ) 中出現最多次的數字。
例如: 1 2 2 3 5 6 1 2
則數字 2 出現最多次,共 3 次
Input Format
5 6 5 5 5 5 5 9 9
Output Format
5 6
第一個代表哪一個數字,第二個代表次數
Example
Sample InputSample Output
5 6 5 5 5 5 5 9 95 6

  
while True:
    try:
        nums = list(map(int,input().split()))
        data = {}
        for i in range(len(nums)):
            if nums[i] in data.keys():continue
            data.update({nums[i]:nums.count(nums[i])})
            #print(data)
        for key,value in data.items():
            if value == max(data.values()):
                print('%d %d' % (key,value))
    except(EOFError):
        break

沒有留言:

張貼留言