首页 > 试题广场 >

n个数里出现次数大于等于n2的数

[编程题]n个数里出现次数大于等于n/2的数
  • 热度指数:22132 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入n个整数,输出出现次数大于等于数组长度一半的数。

输入描述:
每个测试输入包含 n个空格分割的n个整数,n不超过100,其中有一个整数出现次数大于等于n/2。


输出描述:
输出出现次数大于等于n/2的数。
示例1

输入

3 9 3 2 5 6 7 3 2 3 3 3

输出

3
a=list(map(int,input().split()))
b=[a[0]]
c=set()
count=0 d=[] for i in range(1,len(a)): if a[i] not in b:
        b.append(a[i]) else:
        c.add(a[i])
c=list(c) for i in range(len(c)): for j in range(len(a)): if a[j]==c[i]:
            count+=1  d.append([c[i],count])
d=sorted(d,key=lambda x:x[1]) print(d[-1][0])
发表于 2019-01-01 17:24:24 回复(0)
#其实这个题应该考虑到可能有几个数字的重复次数是一样的情况
n=''.join(input().split(' '))
L=['0','1','2','3','4','5','6','7','8','9']
for i in L:
    if n.count(i)>=len(n)//2:
        print(int(i))

发表于 2018-08-12 17:18:46 回复(0)
import sys
str = sys.stdin.readline().strip().split(" ")
for i in str:
    if str.count(i) >= len(str)/2:
        print(int(i))
        break

发表于 2018-08-11 04:07:25 回复(0)
Counter
from collections import Counter
print(Counter(list(map(int,input().split()))).most_common(1)[0][0])

发表于 2018-07-08 22:08:57 回复(0)
import traceback
# ***思想
try:
    n = map(int,input().strip().split())
    reslist = []
    for item in n:
        if len(reslist)==0:
            reslist.append(item)
        else:
            if(reslist[0] == item):
                reslist.append(item)
            else:
                reslist.pop()
    print(reslist[0])


except:
    traceback.print_exc()
    pass
发表于 2018-06-22 15:49:36 回复(0)
from collections import Counter
num=list(map(int,input().split()))
d=Counter(num)#利用python内置函数Counter(),快速统计单词在文本中出现的次数,结果为dict
res=max(d.values())#找出最大的出现次数
for k,v in d.items():#输出最大次数对应的整数
    if v==res:
        print(k)

发表于 2018-05-29 11:11:56 回复(0)
n=[int(x) for x in input().split()]
A=set(n)
tmp=0
obj=0
for i in A:
    if n.count(i)>tmp:
        tmp=n.count(i)
        obj=i
print(obj)


发表于 2018-05-28 11:03:23 回复(0)
l = list(input().strip().split(" "))
l.sort()
n = len(l)
print (l[n//2-1])

发表于 2018-03-31 05:37:18 回复(0)
#! python3
#-*- coding:utf-8 -*-

'''
题目描述
输入n个整数,输出出现次数大于等于数组长度一半的数。
输入描述:
每个测试输入包含 n个空格分割的n个整数,n不超过100,其中有一个整数出现次数大于等于n/2。
输出描述:
输出出现次数大于等于n/2的数。
示例1
输入
3 9 3 2 5 6 7 3 2 3 3 3
输出
3
'''

import sys

if __name__ == '__main__':
    "doc"
    for line in sys.stdin:
        list1 = line.strip().split(' ')
        n = len(list1)/2
        dict1={}
        for i in list1:
            if i not in dict1.keys():
                dict1[i]=list1.count(i)
                if dict1[i]>=n:
                    print(i)
                    break

发表于 2018-03-12 20:13:47 回复(0)
userinput = input().split(' ')
counter={}
target = len(userinput)/2
for elements in userinput:
    if elements in counter:
        counter[elements]+=1
    else:
        counter[elements]=1
for result,threshold in counter.items():
    if(threshold>=target):
        print(result)
        break


发表于 2018-01-26 03:53:33 回复(0)
from collections import Counter

ip = input().replace(" ", "")
temp = Counter(ip)
result = [k for k, v in temp.items() if v >= len(ip) / 2]
print(" ".join(result))


发表于 2017-11-03 10:20:58 回复(0)

Python只需要3行就能通过:
利用collections的Counter模块,跟切菜一样简单:

from collections import Counter
c = Counter(list(map(int, input().split())))
print(c.most_common(1)[0][0])
发表于 2017-09-07 13:55:10 回复(3)
input_num=raw_input().split()
input_len=len(input_num)
new_Num=list(set(input_num))
out_Num=[0 for i in range(len(new_Num))]
for i in range(len(new_Num)):
    out_Num[i]=input_num.count(new_Num[i])
for j in range(len(new_Num)):
    if out_Num[j]>=len(input_num)/2:
        print new_Num[j],

发表于 2017-09-04 15:16:05 回复(0)
#!/usr/bin/env python
#-*- coding:utf8 -*-
def fun(nums):
    L=nums.split()
    n=len(L)
    dict={i:L.count(i) for i in L}
    for i in dict:
        if dict[i]>=n//2:
            return i

 
if __name__ == '__main__':
    nums = raw_input()
    print fun(nums)
发表于 2017-08-25 14:14:48 回复(0)
import sys
import re
#用一个哨兵来判断是否过半
def panduan(a):
    tmp=a[0]
    num=1
    for i in range(1,len(a)):
        if a[i]==tmp:
            num=num+1
        else:
            num=num-1
            if num < 0:
                tmp=a[i]
                num=1
    if num==0 or num>0:
        print tmp
b=re.split(' ',raw_input())
a=[]
for i in b:
    a.append(int(i))
panduan(a)

发表于 2017-08-22 17:08:46 回复(0)