od机试算法题目,手打验证①

  1. HJ1 字符串最后一个单词的长度
    #计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
str = 'hello world 123456'
arr = str.split(' ')
print(arr)
#arr 是列表,长度-1,就是最后一个元素的下标
index_last = len(arr) -1
print(len(arr[index_last]))
  1. HJ2 计算某字符出现次数
    # 写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母)
a_str = "ABCabc123"
b_str = "5"
n = 0
for a in a_str:
    if a.lower() == b_str.lower():
        n += 1
print(n)
  1. 明明生成了NN个1到500之间的随机整数。
    # 请你删去其中重复的数字,即相同的数字只保留一个,把其余相同的数去掉,然后再把这些数从小到大排序,按照排好的顺序输出。
while True:
    try:
        a = int(input())
        i = 1
        list_a = []
        while i <= a:
            list_a.append(int(input()))
            i += 1
        list_b = list(set(list_a))
        list_b.sort()
        for m in list_b:
            print(m)
    except:
        break
  1. leetcode14-最长公共前缀
'''
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""

'''

class Solution:
    def longestCommonPrefix(self, strs):

        res = ""
        for tmp in zip(*strs):
            tmp_set = set(tmp)
            if len(tmp_set) == 1:
                res += tmp[0]
            else:
                break
        return res
  1. #创建二叉树
# 输入获取
operations = eval(input())

class Node:
    def __init__(self, val):
        self.val = val
        self.lc = None
        self.rc = None


# 算法入口
def getResult(operations):
    tree = [[Node(-1)]]

    for i in range(len(operations)):
        height, index = operations[i]

        if len(tree) <= height + 1:
            tree.append([])

        ch = Node(i)
        tree[height + 1].append(ch)

        fa = tree[height][index]
        if not fa.lc:
            fa.lc = ch
        elif not fa.rc:
            fa.rc = ch

    ans = []
    queue = [tree[0][0]]

    while len(queue) > 0:
        node = queue.pop(0)

        if node is not None:
            ans.append(node.val)
            queue.append(node.lc)
            queue.append(node.rc)
        else:
            ans.append("null")

    while True:
        if ans[-1] == "null":
            ans.pop()
        else:
            break

    return ans


# 算法调用
res = str(getResult(operations))
print(res.replace("'", ""))
  1. 回文数字符串

# 输入获取
#  “ level ”
s = input()
# 算法入口
def getResult(s):
    count = {}

    # 统计各字母个数
    for c in s:
        if count.get(c) is None:
            count[c] = 0
        count[c] += 1

    ans = []
    mid = ""

    for c in count.keys():
        # 如果字母数量大于等于2,则可以成对出现,
        if count[c] >= 2:
            n = count[c] // 2
            print(c)
            ans.extend([c] * n)
            print(ans.extend([c]*n))
        # 如果字母数量只有1个,或者字母数量大于2但是为奇数,则最后必然只剩单个字母可用,此时我们应该在这些无法成对的单字母中选择一个字典序最小的
        if count[c] % 2 != 0 and (mid == "" or c < mid):
            mid = c

    ans.sort()
    left = "".join(ans)

    ans.reverse()
    right = "".join(ans)

    return left + mid + right  # 回文串左边部分 + 中间单字母 + 回文串右边部分


# 调用算法
print(getResult(s))

  1. 最快到达医院的方法
# 输入获取
# x, y, m, l, n = map(float, input().split())
# 算法入口
def getResult(x, y, m, l, n):
    """
    :param x: 到达A医院的距离 (公里)
    :param y: 到达B医院的距离 (公里)
    :param m: 计程车平均速度  (米/分钟)
    :param l: 上车等待时间    (分钟)
    :param n: 步行速度        (米/分钟)
    :return: 请问大壮到达哪家医院最快
    """
    taxiToA = x * 1000 / m + l
    walkToB = y * 1000 / n

    if taxiToA == walkToB:
        return "Same"
    elif taxiToA > walkToB:
        return "Walk"
    else:
        return "Taxi"


# 算法调用
x, y, m, l, n = map(float, input().split())

print(getResult(x, y, m, l, n))
  1. HJ17-字符串移动
'''
开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。
从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。

输入:

合法坐标为A(或者D或者W或者S) + 数字(两位以内)

坐标之间以;分隔。

非法坐标点需要进行丢弃。
'''
str_lst = input().split(";")

x, y =0, 0

for s in str_lst:
    if len(s)>1 and len(s)<=3 and s[0] in 'WASD' and s[1:].isdigit():
        if s[0] == 'W':
            y = y + int(s[1:])
        elif s[0] == 'S':
            y = y - int(s[1:])
        if s[0] == 'A':
            x = x - int(s[1:])
        elif s[0] =='D':
            x = x + int(s[1:])

print(str(x)+','+str(y))

  1. HJ20-密码验证合格程序
'''
描述
密码要求:

1.长度超过8位

2.包括大小写字母.数字.其它符号,以上四种至少三种

3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)

数据范围:输入的字符串长度满足 1 \le n \le 100 \1≤n≤100 
输入描述:
一组字符串。

输出描述:
如果符合要求输出:OK,否则输出NG4
'''

while True:
    try:
        s = input()
        a, b, c, d = 0, 0, 0, 0
        for i in s:
            if i.isdigit():
                a = 1
            elif i.islower():
                b = 1
            elif i.isupper():
                c = 1
            else:
                d = 1
        judge = True
        for j in range(len(s)-3):
            if s.count(s[j:j+3]) > 1:
                judge = False
        if (a+b+c+d)>=3 and len(s)>8 and judge:
            print('OK')
        else:
            print('NG')


    except:
        break
  1. HJ23-删除字符串中出现次数最少的字符
'''
实现删除字符串中出现次数最少的字符,若出现次数最少的字符有多个,则把出现次数最少的字符都删除。
输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。
input: aabcddd
'''
while True:
    try:
        s = input()
        sets = set(s)
        print('sets:',sets)
        #创建的空字典
        dic = dict()
        n = s
        print('s:',s)
        print(type(s))
        print('n:',n)
        print(type(n))
        for i in sets:
            dic[i] = s.count(i)
        print('指定后dic:',dic)
        res = min(dic.values())
        print(res)
        for k,v in dic.items():
            if v == res:
                s = s.replace(k, "")
        print(s)
    except:
        break
  1. HJ33-整数与IP地址间的转换
'''
原理:ip地址的每段可以看成是一个0-255的整数,把每段拆分成一个二进制形式组合起来,然后把这个二进制数转变成
一个长整数。
10.0.3.193
167969729

167773121
10.3.3.193

'''


while 1:
    try:
        a=list(map(int,input().split('.')))
        b=int(input())
        c=''
        for i in a:
            s=bin(i)[2:]
            while (len(s)<8):
                s='0'+s
                print(s)
            c += s
            print(c)
        b=bin(b)[2:]

        while (len(b)<32):
            b= '0'+b
            print(b)
        print(str(int(b[0:8],2))+'.'+str(int(b[8:16],2))+'.'+str(int(b[16:24],2))+'.'+str(int(b[24:32],2)))
    except:
        break

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值