牛客网华为机试题之Python解法

版权声明:本文为博主原创文章,未经博主允许不得转载。博客地址:http://blog.csdn.net/qq_22186119 https://blog.csdn.net/qq_22186119/article/details/79201209

牛客网华为机试题之Python解法

第1题 字符串最后一个单词的长度

a = input().split(" ")
print(len(a[-1]))

第2题 计算字符个数

a = input()
b = input()
print(a.lower().count(b.lower()))

第3题 明明的随机数

while True:
    try:
        num = int(input())
        data = []
        for i in range(num):
            data.append(int(input()))
        data = list(set(data))
        data = sorted(data)
        for i in data:
            print(i)
    except:
        break

第4题 字符串分隔

def stringSplit(s):
    while len(s)>8:
        print(s[0:8])
        s = s[8:len(s)]
    print(s+(8-len(s))*'0')

stringSplit(input())
stringSplit(input())

第5题 进制转换

while True:
    try:
        print(str(int(input(),16)))
    except:
        break

第6题 质数因子

def primeFactor(a):
    res = []
    while a>1:
        for i in range(2,a+1):
            if a%i == 0:
                res.append(i)
                a = int(a/i)
                break
    return res

a = primeFactor(int(input()))
for i in a:
    print(i,end=" ")

第7题 取近似值

def myround(a):
    if 10*a%10>=5:
        return int(a)+1
    else:
        return int(a)

print(myround(float(input())))

第8题 合并表记录

a = input()
d = {}
for i in range(int(a)):
    b = list(map(int, input().split(' ')))
    if b[0] not in d.keys():
        d[b[0]] = b[1]
    else:
        d[b[0]] += b[1]

for key in d.keys():
    print(key, d[key])

第9题 提取不重复的整数

a = input()
s = ''
for i in range(len(a)-1,-1,-1):
    if a[i] not in s:
        s += a[i]
print(s)

第10题 字符个数统计

s = input()
a = []
for i in s:
    if 0 <= ord(i) <= 127 and i not in a:
        a.append(i)
print(len(a))
s = input()
d = {}
for i in s:
    if 0 <= ord(i) <= 127:
        d[i] = 1
print(sum(d.values()))

第11题 数字颠倒

a = input()
b = ''
for i in a[::-1]:
    b += i
print(b)

第12题 字符串反转

a = input().split(' ')
print(' '.join(a[::-1]))

第13题 句子逆序

a = int(input())
b = []
for i in range(a):
    b.append(input())
for i in sorted(b):
    print(i)

第14题 字串的连接最长路径查找

a = int(input())
b = []
for i in range(a):
    b.append(input())
for i in sorted(b):
    print(i)

第15题 求int型数据在内存中存储时1的个数

print(bin(int(input())).count('1'))

未完待续……

猜你喜欢

转载自blog.csdn.net/qq_22186119/article/details/79201209