2022.6.27 星期一

机考3道题目,第一题100分,第二题100分,第三题200分,总分=sum(用例通过率*分数)

第一题:字符串消消乐。easy

字符串消消乐。相邻且相同的字符串可以消除,消除后字符继续消除,给你一串字符,给出最终的长度。字符串大小写敏感,有其他非字母字符返回0。

例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。
之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。

解题思路:两个字符相同就消除,继续下一次消除迭代

代码如下:

# strs=input()
strs = 'abccd'

def xxl(strs):
    #####字符串消消乐,相邻且相同的字符消除。
    nums_list = []
    l=0
    for i in strs:
        if not i.isalpha():
            return l
        else:
            nums_list.append(i)
    l = len(nums_list)
    for i in range(l - 1):
        if (nums_list[i] == nums_list[i + 1]):
            nums_list.pop(i)
            nums_list.pop(i)
            tmp_str=''.join(nums_list)
            print(strs,'xxl临时字符串',tmp_str)
            return xxl(tmp_str)
    else:
        return l

def xxl_lx(strs):
    ####消除相邻且相同的字符。包括2个或者2个以上字符相同
    tmp_strs=[]
    l=0
    for i in strs:
        if not i.isalpha():
            return l
    l=len(strs)
    same_flag=False
    for i in range(l):
        if(strs[i]==strs[i-1] and i>0):
            if(same_flag):
                same_flag=True
            else:
                same_flag = True
                tmp_strs.pop()
        else:
            same_flag = False
            tmp_strs.append(strs[i])

    tmp_str=''.join(tmp_strs)
    print(strs,'xxl_lx临时字符串', tmp_str)
    if l == len(tmp_str):
        return tmp_str
    else:
        return xxl_lx(tmp_str)

# ll = xxl(strs)
ll = xxl_lx(strs)
print(ll)

第二题:题目较长。easy

【We Are A Team】

总共有 n 个人在机房,每个人有一个标号(1<=标号<=n),他们分成了多个团队,需要你根据收到的 m 条消息判定指定的两个人是否在一个团队中,具体的:

1、消息构成为 a b c,整数 a、b 分别代表两个人的标号,整数 c 代表指令

2、c == 0 代表 a 和 b 在一个团队内

3、c == 1 代表需要判定 a 和 b 的关系,如果 a 和 b 是一个团队,输出一行’we are a team’,如果不是,输出一行’we are not a team’

4、c 为其他值,或当前行 a 或 b 超出 1~n 的范围,输出‘da pian zi’

输入描述

第一行包含两个整数 n,m(1<=n,m<100000),分别表示有 n 个人和 m 条消息
随后的 m 行,每行一条消息,消息格式为:a b c(1<=a,b<=n,0<=c<=1)
输出描述: 1、c ==1,根据 a 和 b 是否在一个团队中输出一行字符串,在一个团队中输出‘we are a team’,不在一个团队中输出’we are not a team’ 2、c 为其他值,或当前行 a 或 b 的标号小于 1 或者大于 n 时,输出字符串‘da pian zi’
如果第一行 n 和 m 的值超出约定的范围时,输出字符串”Null”。
示例1 输入输出示例仅供调试,后台判题数据一般不包含示例

输入

5 7

1 2 0

4 5 0

2 3 0

1 2 1

2 3 1

4 5 1

1 5 1

输出

We are a team

We are a team

We are a team

We are not a team

示例2 输入输出示例仅供调试,后台判题数据一般不包含示例

输入

5 6

1 2 0

1 2 1

1 5 0

2 3 1

2 5 1

1 3 2

输出

we are a team

we are not a team

we are a team

da pian zi

解题思路:第一行输入n和m,后面是m行数字,分别为a,b,c。如果c=0则代表ab是一个团队。

c=1,时需要判断是否在一个团队,在“we are a team”,不在“we are a team”。c其他值输出“da pian zi”

代码如下:

#定义函数
def func():
    #一个团队的列表,存储团队成员
    is_team = []
    #对应m行的输入项
    all_list=[]
    #存储输出结果
    result=[]
    for i in range(0, m):
        lists = list(map(int, input().split()))
        if (lists[2] == 0):
            is_team.append(lists[0])
            is_team.append(lists[1])
        all_list.append(lists)

    for ll in all_list:
        a, b, c = ll[0], ll[1], ll[2]
        if a < 1 or a > n or b < 1 or b > n:
            result.append("da pian zi")
        #c=0不用处理输出
        if c == 0:
            continue
        #c=1时需要判断
        elif c == 1:
            if is_team.count(a) > 0 and is_team.count(b) > 0:
                result.append("we are a team")
            # elif a < 1 or a > n or b < 1 or b > n:
            #     result.append("da pian zi")
            else:
                result.append("we are not a team")
        else:
            result.append("da pian zi")
    for i in result:
        print(i)


#调用函数
# 5 6
# 1 2 0
# 1 2 1
# 1 5 0
# 2 3 1
# 2 5 1
# 1 3 2
n, m = map(int, input().split())

if n < 1 or n > 100000 or m < 1 or m > 100000:
    print('NULL')
else:
    func()



可惜最后通过率只有70+%。应该是对n,m范围的处理上有问题,应该直接退出不需要后面的m行输入,对a,b的范围判断输出“da pian zi”这里也有问题,还有多个团队处理上,我只用了一个list来存储团队,实际上不止一个团队,应该换种思路。

第三题:稍后补充

总成绩:240+

来源:Price Tag238

物联沃分享整理
物联沃-IOTWORD物联网 » 华为OD机试题

发表评论