zoukankan      html  css  js  c++  java
  • 华为2020校招笔试编程题

    华为2020校招笔试编程题

     

    刚做完华为的笔试题,简要描述一下三道编程题的解决方法以及python代码实现

    第一题大致描述:

    给定两个已经升序排序好的的序列A={a1,a2,a3,...an} 和B={b1,b2,b3...bn} ,一个数R,找出满足以下条件的的(ai,bj)序列对

    1.ai<=bj

    2.bj和ai两者的距离 满足 bj-ai<=R ,要是该条件不满足,就从序列B中找出 和ai 距离最接近R的一个点bj(同时要满足条件1)

    输入样例:A={1,3,5},b={2,4,6},R=1

    输出样例:(1,2)(3,4)(5,6)

    解决思路:遍历所有序列对,找出满足条件的对即可

    代码如下:(测试通过)

    复制代码
    import sys
    s=sys.stdin.readline()
    #获取A、B、R的值,用正则表达式匹配会更容易
    a=s.find('{')
    b=s.find('}')
    a_str=s[a+1:b].split(',')
    A=[int(x) for x in a_str]
    a=s.rfind('{')
    b=s.rfind('}')
    b_str=s[a+1:b].split(',')
    B=[int(x) for x in b_str]
    a=s.rfind('=')
    R=int(s[a+1:])
    RES=[]
    for x in A:
        has_find=False
        for y in B:
            if x<= y and y-x<=R:
                RES.append((x,y))
                has_find=True
            elif x<=y and y-x>R and has_find==False:
                RES.append((x,y))
                break
    for x in RES:
        print('({},{})'.format(x[0],x[1]),end='')
    复制代码

    第二题大致描述

    对一行给定的字符串进行反转输出,同时去除中间不满足条件的分隔符

    输入字符串:I am an 20-years out--standing @ * -stu- dent

    去除分割符并反转之后,输出字符串(子字符串以一个空格隔开):dent stu standing out 20-years an am I

    分割符描述如下:

    1、除了字母、数字和 - 之外,其他的都是分割符,如输入字符串中的@ *等都属于分割符

    2、20-years中的'-' 表示的是连接符,即当‘-’两边都有字母、数字时,‘-’就属于连接符,否则属于分割符

    3、out--standing中的‘--’表示分割符,应该拆分为两个字字符串out 和 standing

    解决思路:用栈去实现,遍历输入字符串的字符:

    1、遇到字母和数字就入栈。

    2、遇到‘-’的时候就判断是分割符还是连接符,要是是分割符,就弹出所有栈元素,构成一个子字符串,否则就入栈

    3、遇到其他分割符,弹出所有栈元素,构成子字符串

    找到所有子字符串,就可以做反序输出处理

    python代码如下:(测试通过)

    复制代码
    import sys
    #使用栈去解决问题
    #s="I am an 20-years  out--standing @ * -stu- dent"
    s=sys.stdin.readline()
    word=[]
    res=[]
    for x in s:
        if '0'<= x <='9' or 'a'<=x <='z' or 'A'<=x<='Z':
            word.append(x)
        elif x=='-':
            if len(word)==0:
                continue
            else:
                if word[-1]=='-':
                    word.pop()
                    res.append(''.join(word))
                    word = []
                else:
                    word.append(x)
        else:
            if len(word)>0:
                if(word[-1]=='-'):
                    word.pop()
                res.append(''.join(word))
                word=[]
    if(len(word)>0):
        if word[-1]=='-':
            word.pop()
        res.append(''.join(word))
    for s in res[::-1]:
        print(s,end=' ')
    复制代码

    第三题大致描述如下:

    给定多组原本的航班预订信息(航班号,座位号,乘客姓名),以及多组要改签的航班信息(原本航班号,原本座位号,新航班号,新座位号)

    输出最后的航班预订信息,要是有重复的内容,以最新改签的为标准

    输入的内容如下: 3 表示原本的航班信息数,2表示要改签的航班数

    3
    CZ7132,A1,ZHANGSAN
    CZ7132,A2,ZHAOSI
    CZ7156,A2,WANGWU
    2
    CZ7132,A1,CZ7156,A2
    CZ7156,A2,CZ7156,A3

    输出内容如下:
    CZ7132,A2,ZHAOSI
    CZ7156,A2,ZHANGSA
    CZ7156,A3,WANGW

    解决思路,采用python的字典去表示机票位置信息和乘客姓名的对应关系 {piao:name}{name:piao),先找出需要修改航班的乘客姓名,再依次更新该乘客的航班号,航班座位
    代码如下:
    复制代码
    #输入:
    #3
    #CZ7132,A1,ZHANGSAN
    #CZ7132,A2,ZHAOSI
    #CZ7156,A2,WANGWU
    #2
    #CZ7132,A1,CZ7156,A2
    #CZ7156,A2,CZ7156,A3
    #输出
    #CZ7132,A2,ZHAOSI
    #CZ7156,A2,ZHANGSAN
    #CZ7156,A3,WANGWU
    
    import sys
    old_booking_piao_name={}
    old_booking_name_piao={}
    sum_num=int(input())
    for i in range(sum_num):
        msg=input()
        m=msg.rfind(',')
        piao=msg[0:m]
        name=msg[m+1:]
        old_booking_name_piao[name]=piao
        old_booking_piao_name[piao]=name
    
    chang_booking=[]
    num=int(input())
    for i in range(num):
        msg = input()
        m = msg.split(',')
        old = m[0]+','+m[1]
        new = m[2]+','+m[3]
        # print(old,new)
        chang_booking.append((old,new))
    
    new_booking_name_piao_copy=old_booking_name_piao.copy()
    for x in chang_booking:
        name=old_booking_piao_name[x[0]]
        new_booking_name_piao_copy.pop(name)
        new_booking_name_piao_copy[name]=x[1]
    
    # print(new_booking_name_piao_copy)
    for key,val in new_booking_name_piao_copy.items():
        print(val+','+key)
    复制代码
    
    

     https://www.cnblogs.com/mangojun/p/11510959.html

  • 相关阅读:
    14_java之变量|参数|返回值|修饰符
    NYOJ 202 红黑树 (二叉树)
    NYOJ 138 找球号(二) (哈希)
    NYOJ 136 等式 (哈希)
    NYOJ 133 子序列 (离散化)
    NYOJ 129 树的判定 (并查集)
    NYOJ 117 求逆序数 (树状数组)
    NYOJ 93 汉诺塔 (数学)
    HDU 2050 折线分割平面 (数学)
    天梯赛L2-008 最长对称子串 (字符串处理)
  • 原文地址:https://www.cnblogs.com/wangyu19900123/p/11511018.html
Copyright © 2011-2022 走看看