首页 > 试题广场 >

ん...红茶?

[编程题]ん...红茶?
  • 热度指数:4208 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 128M,其他语言256M
  • 算法知识视频讲解
高贵的蕾米莉亚大小姐每天需要饮用定量 B 型血的红茶以保持威严,并且要分两杯在不同时段饮用。
女仆长十六夜咲夜每天可以制作很多杯不同剂量 B 型血的红茶供蕾米莉亚大小姐饮用。
某日,你和天才妖精琪露诺偷偷潜入红魔馆被咲夜抓住,要求在今日份的红茶中挑出所有满足大小姐要求的茶杯,否则……

输入描述:
每个样例有三行输入,第一行输入表示茶杯个数,第二行输入表示每份茶杯里的 B 型血剂量,第三行表示大小姐今天的定量


输出描述:
对每一个样例,输出所有可能的搭配方案,如果有多种方案,请按每个方案的第一杯 B 型血剂量的大小升序排列。
如果无法找到任何一种满足大小姐的方案,输出"NO"(不包括引号)并换行。
示例1

输入

7
2 4 6 1 3 5 7
7

输出

1 6
2 5
3 4

备注:
茶杯个数不超过100000,保证茶杯里的B型血剂量两两不同。
input()
l = list(map(int, input().split()))
x = int(input())
l.sort()
s, flag = set(), True
for i in l: s.add(x-i)
for i in l:
    if i in s and i<x-i:
        print(i, x-i)
        flag = False
if flag: print('NO')
python真方便~~~!
发表于 2019-09-26 16:46:02 回复(0)

参考 野径入桑麻 答案,改了判断顺序,ac100%

numCup = int(input())
doseB = list(map(int, input().split()))
doseNeed = int(input())
doseB.sort()
res_all = []
start = 0
end = numCup-1
while start < end:
    a = doseB[start]
    b = doseB[end]
    if a+b == doseNeed:
        res_all.append([a,b])    
    if a+b > doseNeed:
        end -= 1
    else:
        start +=1
if len(res_all) == 0:
    print('NO')
else:
    for item in res_all:
        print('%s %s'%(item[0],item[1]))
发表于 2019-08-20 12:49:01 回复(0)
喵喵喵,题目中虽然说明了数组中的数字是不同的。可是呢,是有相同的。所以,要想百分百pass这道题,只能在双指针的基础上加上重复检查。
import sys
input_strs = []
for line in sys.stdin:
	input_strs.append(line.strip())
cup_N = int(input_strs[0])
cup_list = []
line_1 = input_strs[1].split(" ")
for v in line_1:
	if len(v) > 0:
		if v[0] >= '0' and v[0] <= '9':
			cup_list.append(int(v))
		else:
			print("Error...")
	else:
		print("Error......")
cup_SUM = int(input_strs[2])
flag = False
cup_list.sort()
first, second = 0, cup_N - 1
if len(cup_list) != cup_N:
	print("Error1....")
# 数组中的数一定是不一样的
while first < second:
	if cup_list[first] + cup_list[second] == cup_SUM:
		# result.append([cup_list[first], cup_list[second]])
		print(str(cup_list[first]) + " " + str(cup_list[second]))
		# 针对可能重复的fisrt
		while first < second and cup_list[first] == cup_list[first + 1]:
			first += 1
			print(str(cup_list[first]) + " " + str(cup_list[second]))
			
		# 针对可能重复的second
		while first < second and cup_list[second] == cup_list[second - 1]:
			second -= 1
			print(str(cup_list[first]) + " " + str(cup_list[second]))
		flag = True
		first += 1
		second -= 1
	elif cup_list[first] + cup_list[second] < cup_SUM:
		first += 1
	else:
		second -= 1
if not flag:
	print("NO")

编辑于 2019-08-10 15:58:49 回复(1)

热门推荐

通过挑战的用户

查看代码