首页 > 试题广场 >

六一儿童节

[编程题]六一儿童节
  • 热度指数:36316 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
六一儿童节,老师带了很多好吃的巧克力到幼儿园。每块巧克力j的重量为w[j],对于每个小朋友i,当他分到的巧克力大小达到h[i] (即w[j]>=h[i]),他才会上去表演节目。老师的目标是将巧克力分发给孩子们,使得最多的小孩上台表演。可以保证每个w[i]> 0且不能将多块巧克力分给一个孩子或将一块分给多个孩子。

输入描述:
第一行:n,表示h数组元素个数
第二行:n个h数组元素
第三行:m,表示w数组元素个数
第四行:m个w数组元素


输出描述:
上台表演学生人数
示例1

输入

3 
2 2 3
2
3 1 

输出

1
h_len = int(input())
h = list(map(int,input().split()))
w_len = int(input())
w = list(map(int,input().split()))
h.sort(reverse = 1)
w.sort(reverse = 1)
def solve(h,w):
    i,j,count = 0,0,0
    while (i<h_len and j<w_len):
        if (h[i] <= w[j]):
            count += 1
            i += 1
            j += 1
        else:
            i += 1
    return count
print(solve(h,w))
发表于 2020-03-19 22:42:31 回复(0)
python简单易懂,代码如下一看就懂
def fun():
    n = int(input())#学生个数
    h_list = [int(i) for i in input().strip().split()] #学生巧克力饥饿值
    m = int(input()) #巧克力个数
    w_list = [int(i) for i in input().strip().split()] #巧克力战斗力值
    count = 0 
    while (w_list != []) and (h_list != []):
        miniw = min(w_list)
        indexw = w_list.index(miniw)
        minih = min(h_list)
        indexh = h_list.index(minih)
        if miniw>=minih:
            count += 1
            w_list.pop(indexw)
            h_list.pop(indexh)
        else:
            w_list.pop(indexw)
    return count
print(fun())
发表于 2019-08-12 20:29:53 回复(0)
n = int(input())
h = [int(n) for n in input().split()]
m = int(input())
w = [int(n) for n in input().split()]
h = sorted(h)
w = sorted(w)
count = 0
for i in w:
    for j in h:
        if i >= j:
            count += 1
            h.remove(j)
            break
    else:
        continue
print(count)

发表于 2019-07-24 10:33:36 回复(0)

python 多行

跟 重庆第九届ACM,题鸭脖子一样
Hlen,H,Wlen,W= int(input()),list(map(int,input().split())),int(input()),list(map(int,input().split()))
H.sort(reverse=True)
W.sort(reverse=True)
i,count= 0,0
for item in H:
    if i <Wlen:
        if item<=W[i]:
            count+=1
            i+=1
print(count)

发表于 2019-04-14 00:18:35 回复(0)
n = int(input().strip())
h = list(map(int,input().strip().split()))
m = int(input().strip())
w = list(map(int,input().strip().split()))
num = 0
h = sorted(h,reverse = True)
w = sorted(w,reverse = True)
p = min(n,m)
res = 0
j = 0
for i in range(m):
    while j < n:
        if w[i] >= h[j]:
            res+=1
            j+=1
            break
        j+=1
print(res)

发表于 2019-04-05 21:42:42 回复(0)
#思路清晰简单😆 import sys
def make(n1, h, n2, w):
    h.sort()
    w.sort()
    res = 0
    i = 0
    for x in w:
        if i < n1 and x >= h[i]:
            i += 1
            res += 1
    return res

if __name__ == '__main__':
    n1 = sys.stdin.readline().strip()
    h = sys.stdin.readline().strip()
    n2 = sys.stdin.readline().strip()
    w = sys.stdin.readline().strip()
    h = list(map(lambda x : int(x), h.split()))
    w = list(map(lambda x : int(x), w.split()))
    print(make(int(n1), h, int(n2), w))

发表于 2019-04-05 19:26:13 回复(0)
import sys
n=int(sys.stdin.readline().strip())#H数组元素个数
H=list(map(int,sys.stdin.readline().strip().split(' ')))#n个h数组元素
m=int(sys.stdin.readline().strip())#W元素的个数
W=list(map(int,sys.stdin.readline().strip().split(' ')))#m个W数组元素
H.sort()
W.sort()
ctn=0
while n and m:
    if W[m-1]>=H[n-1]:
        m-=1
        n-=1
        ctn+=1
    elif W[m-1]<H[n-1]:
        n-=1
print(ctn)

发表于 2019-03-30 21:45:49 回复(0)
思路:第一行:n,表示h数组元素个数  --  表示小朋友个数  第二行:n个h数组元素         --  表示不同得小朋友上台表演所需要获得的巧克力大小  第三行:m,表示w数组元素个数   --  表示巧克力个数  第四行:m个w数组元素         --  表示实际上每块巧克力的大小
    先将h和w数组升序排列
    要使表演的小朋友个数最多,则先让需要获得最小巧克力的小朋友先选,选择最小的符合条件的巧克力,此时巧克力列表删除取走的那一个巧克力,pop()
    以此类推,第二个,第三个小朋友选择....
    当某一个小朋友有多个符合的巧克力大小可选择时,选择最小那个,结束这一个小朋友的选择。break  
n = int(input(""))    #表示小朋友个数 h_n = input('')        #表示不同得小朋友上台表演所需要获得的巧克力大小 m = int(input(""))    #表示巧克力个数 h_m = input('')        #表示实际上每块巧克力的大小 needs_ch = [int(item) for item in h_n.split(' ')] needs_ch.sort() trues_ch = [int(item) for item in h_m.split(' ')] trues_ch.sort()
num = 0 for i in needs_ch:     for j in trues_ch:         if j>=i:             num+=1             trues_ch.pop(trues_ch.index(j))    #某个小朋友选择了最合适的巧克力,此块巧克力被弹出             break    #某一个小朋友有多个符合的巧克力大小可选择时,选择最小那个,结束这一个小朋友的选择 print(num)

发表于 2019-03-28 21:53:27 回复(0)
n = int(input())
h = list(map(int,input().split()))
m = int(input())
w = list(map(int,input().split()))
h.sort()
w.sort()
count=0
d=0
for i in range(0, n):
    for j in range(d, m):
        if w[j] >= h[i]:
            count = count+1
            d = j+1
            break
        else:
            continue

print(count)
发表于 2019-03-24 21:49:53 回复(0)
n = int(input())
h = list(map(int, input().split()))
m = int(input())
w = list(map(int, input().split()))
h.sort()
w.sort()
choc_num = 0
max_child = 0
flag_get = 0
for i in range(n):
    for j in range(choc_num, m):
        if w[j] >= h[i]:
            max_child += 1
            choc_num = j + 1
            flag_get = 1
            break
    if flag_get == 0:
        break
print(max_child)

发表于 2019-03-19 11:50:48 回复(0)
缩进很重要,浪费了很多时间:
Python版
def compare_choc(a, b, m):
    c = 0;
    t = m-1;
    for i in range(m-1, -1, -1):
        if t >= 0:
            for j in range(t, -1, -1):
                if a[i] <= b[j]:
                    c =c+ 1;
                    t = j-1;
                    break;
        else:
            break;
    print(c)

n = int(input());
h = input();
m = int(input());
w = input();
h1=sorted([int(i) for i in h.split()]);
w1= sorted([int(j) for j in w.split()]);
if n >= m:
    p = m;
else:
    p = n;
compare_choc(h1, w1, p);


发表于 2019-02-28 20:57:28 回复(2)
# 六一儿童节,Python
# 先排序,再按照小到大,满足巧克力分配,逐个让满足到的小朋友退出(i++)。
def tianji(n,h_sor,m,w_sor):
    result = 0
    i = 0
    if min(h_sor) > max(w_sor):
        return result
    else:
        for j in range(len(w_sor)):
            #del h_sor[h_posi]
            #del w_sor[w_posi]
            if i <= n-1:
                if w_sor[j] >= h_sor[i]:
                    result = result + 1
                    i = i + 1
    return result
    
if __name__=='__main__':
    n = int(input())
    h_vec = [int(t) for t in input().split(' ')]
    m = int(input())
    w_vec = [int(t) for t in input().split(' ')]
    h_sor = sorted(h_vec)
    w_sor = sorted(w_vec)
    print(tianji(n,h_sor,m,w_sor))

发表于 2019-02-20 00:30:33 回复(0)
贪心策略
n = int(input())
h = list(map(int, input().split(' ')))
m = int(input())
w = list(map(int, input().split(' ')))

w.sort()
h.sort()

i = j = 0
while i < n and j < m:
    if w[j] >= h[i]:
        i += 1
    j += 1
    
print(i)

发表于 2019-02-06 16:31:41 回复(0)
# coding=utf-8

def fun():
    n = int(raw_input()) #人数
    wn = map(int,raw_input().split()) #每个人的权重
    m = int(raw_input()) #巧克力数
    wm = map(int,raw_input().split()) #每个巧克力的权重
    wn.sort()
    wm.sort()
    result = 0
    for i in range(m):
        if wn[result]<=wm[i]:
            result += 1
        if result==n:
            break
    return result

if __name__=='__main__':
    print(fun()) 


发表于 2019-01-15 18:30:25 回复(0)