当前位置:   article > 正文

华为机试(python)总结

华为机试(python)

知识点总结

  1. 分隔输入
    HJ1:用空格分隔
  2. 字符串大小写转换
    HJ2
  3. 字符串拼接
    HJ4
  4. 进制转换
    HJ5
  5. 字典
    HJ8
  6. 使用map()做映射
    HJ9

HJ1 字符串最后一个单词的长度

# import sys
# 输入
str = input()
# 将字符串分割为数组
List = str.split(" ")
# 打印最后一个字母的长度
print(len(List[-1]))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

HJ2 计算某字符出现次数

str = input().lower()# 大写是.upper()
char = input().lower()
count = 0
for a in list(str):
    if a == char:
        count += 1
print(count)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

HJ4 字符串分隔

def f(zifu):
    if len(zifu)<8:
        zifu = zifu +'0'*(8-len(zifu))
        print(zifu)
    else:
        while len(zifu)>8:
            qian8 = zifu[:8]
            print(qian8)
            zifu =zifu[8:]
        print(zifu+'0'*(8-len(zifu)))
    return zifu
a = input()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

HJ5 进制转换

print(int(input(),16))
  • 1

HJ6 质数因子

n = int(input())
import math
for i in range(2, int(math.sqrt(n)) + 1):
#
    while n % i == 0:
        print(int(i), end=' ')
        n /= i 
if n > 1: print(int(n), end= ' ')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

HJ7 取近似值

num = float(input())
Int = int(num)
fra= num - Int
if fra >= 0.5:
    print(Int+1)
else:
    print(Int)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

HJ8 合并表记录

amount = int(input()) #获取输入的行数
dictionary = {} #获取输入的字典


for i in range(amount): # 主体部分
    input_list = list(map(int,input().split())) # 获取此行输入并分开
    current_key = input_list[0] # 键
    current_value = input_list[1] # 值
    
    if current_key in dictionary: # 若字典里有此键
        new_value = dictionary[current_key]+ current_value # 值相加
        dictionary[current_key] = new_value
    else:
        dictionary[current_key] = current_value # 否则,存入字典
keys = list(dictionary.keys()) # 将字典从小到大排列
keys.sort()

for element in keys: # 按格式输出
    value = dictionary[element]
    print(str(element)+' '+str(value))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

HJ9 提取不重复的整数

num = input()
List = list(map(int,str(num))) #将字符串变为列表
ans = []
for n in reversed(num):
    if n not in ans:
        ans.append(n)
res = int("".join([str(i)for i in ans]))#拼接列表成为数字*
print(res)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

HJ10 字符个数统计

In = input()
num = list(map(str,In))
ans = []
for a in num:
    if a not in ans:
        ans.append(a)
print(len(ans))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

HJ11 数字颠倒

num = list(map(int,input()))
num.reverse()
res = str("".join(str(i)for i in num))
print(res)
  • 1
  • 2
  • 3
  • 4

HJ12 字符串翻转

num = list(str(input()))
num.reverse()
ans = str("".join(i for i in num))
print(ans)
  • 1
  • 2
  • 3
  • 4

HJ13 句子逆序

In = list(map(str,input().split()))

In.reverse()
ans = ' '.join(In)
print(ans)
  • 1
  • 2
  • 3
  • 4
  • 5

HJ14

n = int(input())
temp = []
for i in range(n):
    temp.append(input())
temp.sort()
for i in range(n):
    print(temp[i])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

HJ15

num = int(input())
N = list(bin(num))
n = 0
for a in N:
    if a == '1':
        n+=1
print(n)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

HJ21 简单密码

n = input()
l = list(n)
if 1 <= len(n) <=100:
    for i in range(len(n)):
        if 'a' <=n[i] <= 'z':
            if n[i] in 'abc':
                l[i] = '2'
            if n[i] in 'def':
                l[i] = '3'
            if n[i] in 'ghi':
                l[i] = '4'
            if n[i] in 'jkl':
                l[i] = '5'
            if n[i] in 'mno':
                l[i] = '6'
            if n[i] in 'pqrs':
                l[i] = '7'
            if n[i] in 'tuv':
                l[i] = '8'
            if n[i] in 'wxyz':
                l[i] = '9'
        elif 'A' <= n[i] <= 'Z':
            if n[i] == 'Z':
                l[i] = 'a'
            else:
                l[i] = chr(ord(n[i])+32+1) #ord()返回阿斯克码
#         else:
#             l[i] = n[i]
ans="".join(l)
    
print(ans)
                
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

HJ22 汽水瓶

ans =[]
while True:
    In = int(input())
    temp = 0
    if In == 0: break
    while In != 0:
        if In >= 3:
            temp += In//3

            In = In//3 + In%3
        elif In == 2:
            temp += 1
            In = 0
            ans.append(temp)
        else:
            In = 0
            ans.append(temp)
for c in ans:
    print(c)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

HJ31 单词倒排

Str = str(input())
# 以空格和特殊符号分割字符串
ans = []
temp = []
for i,a in enumerate(Str):
    if a in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
        temp.append(a)
#         print(a)
    else:
        ans.append("".join(temp))
        temp=[]
ans.append("".join(temp))
ans.reverse()
print(" ".join(ans))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

HJ34

Str = list(str(input()))
Str.sort()
ans = "".join(Str)
print(ans)
  • 1
  • 2
  • 3
  • 4

HJ35 蛇形矩阵

dim = input()
mat = [['0']* i for i in range(dim)]#先生成这个形状的0矩阵
jishu = 1#元素序数
for i in range(dim):
	for j in range(i+1):
	mat[i-j][j] = str(jishu)
	jishu += 1
for i in range(dim):
	print(' '.join(out[i]))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/146543
推荐阅读
相关标签
  

闽ICP备14008679号