牛客刷题|HJ20 密码验证合格程序, HJ16 购物单,H17坐标移动

ACM输入输出处理

参考:【python & ACM 输入输出的处理:sys.stdin.readline().strip().split())】_sys.stdin.readline()输入去除掉空格-CSDN博客

line2 = sys.stdin.readline()#读一行
a = ' 8dajia8hao8 '
b = a.strip()#移除字符串 开头和结尾  的空格或换行符
c = b.strip('8')#移除字符串 开头和结尾  的指定的字符'8'
d = b.strip('o8')#移除字符串 开头和结尾  的指定的字符序列'o8'
print(s.split())#分割所有的空字符

#单个数字
n=int(input())
n = int(sys.stdin.readline())

#输入单行多个数据
x, y = map(int, input().split())
data=list(map(int,input().split(" ")))

# 读取多行数据
list_password=[]
for line in sys.stdin.readlines():
    list_password.append(line[:-1])
 

HJ20 密码验证合格程序

题目链接:密码验证合格程序_牛客题霸_牛客网 (nowcoder.com)

思路:python里面有很多库,如果题目不是只让你实现类似某一个库的功能,就可以直接灵活使用它们。

注意多个if一起用时,最后一种情况不要直接用else,要把它不属于其它情况的条件表明,以避免潜在的错误。

代码

import sys

def check(pw):
    Stype = [0]*4
    if len(pw) <= 8:
        return 'NG'
    for s in pw:
        if s.islower():
            Stype[0] = 1
        if s.isupper():
            Stype[1] = 1
        if s.isdigit():
            Stype[2] = 1
        if not s.islower() and not s.isupper() and not s.isdigit(): # 直接用else会使Stype[3]=1 即使没有特殊符号
            Stype[3] = 1
    if sum(Stype) < 3:
        return 'NG'
    dic = dict()
    for i in range(len(pw) - 2):
        if pw[i:i+3] in dic:
            return 'NG'
        else:
            dic[pw[i:i+3]] = 1
    return 'OK'

list_password=[]
for line in sys.stdin.readlines():
    list_password.append(line[:-1])
 
for i in list_password:
    print check(i)

HJ16  购物单

题目链接:购物单_牛客题霸_牛客网 (nowcoder.com)

思路:是01背包的扩展。对于每个主件,只有四种考虑情况:是否放主件,放主件+附件1,放主件+附件2, 放主件+附件1+附件2。所以只需循环每个主件,取这四种的最大价值。

记录下每个主件的价格和重要度,每个主件下的附件(如果存在)的价格和重要度。

import sys

line1 = sys.stdin.readline()
money, num = map(int, line1.split())

primary, annex = {}, {}
dp = [0] * (money + 1)
for i, line in enumerate(sys.stdin.readlines()): 
    i += 1
    price, importance, pt = map(int, line.split())
    if pt == 0:
        primary[i] = [price, importance]
    else:
        if pt not in annex:
            annex[pt] = [[price, importance]]
        else:
            annex[pt].append([price, importance])

for key in primary:
        price = [primary[key][0]]
        value = [primary[key][1] * primary[key][0]]
        if key in annex:
            price.append(price[0] + annex[key][0][0])
            value.append(value[0] + annex[key][0][0]* annex[key][0][1])
            if len(annex[key])>1:
                price.append(price[0] + annex[key][1][0])
                value.append(value[0] + annex[key][1][0]* annex[key][1][1])
                price.append(price[1] + annex[key][1][0])
                value.append(value[1] + annex[key][1][0]* annex[key][1][1])

        for j in range(money, -1, -10):
            for i in range(len(price)):
                if price[i] <= j:
                    dp[j] = max(dp[j], dp[j - price[i]] + value[i])
print(dp[money])

HJ17 坐标移动

题目链接:坐标移动_牛客题霸_牛客网 (nowcoder.com)

思路:将输入值以分号隔开存放,对每一项判断是否为合法坐标,若合法,则按照字母和移动方向的关系更新x轴,y轴的值。

代码

import sys
line = sys.stdin.readline().strip()
strList = line.split(';')
x, y = 0, 0
coord = set(['W', 'S', 'A', 'D'])
for item in strList:
    if  1 < len(item) < 4 and (item[0] in coord) and item[1:].isdigit():       
        if item[0] == 'W':
            y += int(item[1:])
        if item[0] == 'S':
            y -= int(item[1:])
        if item[0] == 'A':
            x -= int(item[1:])
        if item[0] == 'D':
            x += int(item[1:])
print(str(x) + ',' +str(y))

HJ19 简单错误记录

题目链接:简单错误记录_牛客题霸_牛客网 (nowcoder.com)

思路:建立列表和字典,分别记录地址+行号,和地址+行号:计数。对于每一行输入,只记录地址的后16位和行号,如果字典中没有,记录在字典和列表中,如果有,对应键值加1。最后输出后八位。

代码

import sys
ls = []   
dic = {}  
  
for line in sys.stdin.readlines():
    address = line.split()
    address[0] = address[0].split('\\')[-1]
    address = ' '.join([address[0][-16:], address[1]])
    if address not in dic:
        ls.append(address)
        dic[address] = 1
    else:
        dic[address] += 1
for item in ls[-8:]:
    print(str(item)+' '+ str(dic[item]))

HJ3 明明的随机数

题目链接:明明的随机数_牛客题霸_牛客网 (nowcoder.com)

思路:思路不复杂,建一个set,加入每行的数,由于set中元素不重复,所以不用手动判断是否去重。全部加入后,用sorted函数排序,输出。注意加入时要把输入转换位int形式,不然最后print出每行中间会空一行。

代码

import sys
N = sys.stdin.readline().strip()
numSet = set()

for num in sys.stdin.readlines():
    numSet.add(int(num))
numSet = sorted(numSet)
for i in numSet:
    print(i)

HJ4 字符串分割

题目链接:字符串分隔_牛客题霸_牛客网 (nowcoder.com)

思路:将字符串每八个位一组,分别加紧结果集中,如果最后一组不满八个,就在后面用零补齐八位。

代码

import sys
ans = []
strs = sys.stdin.readline().strip()
k = len(strs)/8 
for i in range(k):
    ans.append(strs[8*i:8*(i+1)])
if len(strs)%8 != 0:
    remain = len(strs)%8
    ans.append(strs[-remain:] + '0'*(8 - remain))
for i in ans:
    print(i)
 

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值