素数之积
题目描述
 RSA加密算法在网络安全世界中无处不在,它利用了极大整数因数分解的困难度,数据越大,安全系数越高,给定一个32位正整数,请对其进行因数分解,找出是哪两个素数的乘积。
输入描述
 一个正整数num,0 < num <= 2147483647
输出描述
 如果成功找到,以单个空格分割,从小到大输出两个素数,分解失败,请输出-1, -1
用例
 输入15输出3 5输入27输出-1 -1
 
import math
def is_prime(num):
    if num < 2:
        return False
    for i in range(2, int(math.sqrt(num)) + 1):
        if num % i == 0:
            return False
    return True
def factorization(num):
    for i in range(2, int(math.sqrt(num)) + 1):
        if num % i == 0 and is_prime(i):
            j = num // i
            if is_prime(j):
                return i, j
    return -1, -1
# 输入
num = int(input("请输入一个正整数num: "))
# 因数分解
result = factorization(num)
# 输出结果
print(result[0], result[1])

最大坐标值

 
 
def calculate_max_coordinate(n, lucky_number, instructions):
    current_coordinate = 0
    max_coordinate = 0
    for instruction in instructions:
        if instruction == lucky_number:
            current_coordinate += instruction + 1
        else:
            current_coordinate += instruction
        max_coordinate = max(max_coordinate, current_coordinate)
    return max_coordinate
if __name__ == "__main__":
    try:
        # 输入指令总个数
        n = int(input().strip())
        # 输入幸运数
        lucky_number = int(input().strip())
        # 输入指令列表
        instructions = list(map(int, input().strip().split()))
        # 计算最大坐标值
        result = calculate_max_coordinate(n, lucky_number, instructions)
        # 输出最大坐标值
        print(result)
    except Exception as e:
        # 异常情况下输出:12345
        print(12345)

CPU算力分配

 
 
L = input().split()
L1, L2 = int(L[0]), int(L[1])
A = list(map(int, input().split()))
B = list(map(int, input().split()))
suma = sum(A)
sumb = sum(B)
ava = int((suma + sumb) / 2)
value = abs(ava - sum(A))
A.sort()
B.sort()
if suma > sumb:
    for item in A:
        if (item - value) in B:
            print(item, item - value)
            break
else:
    for item in A:
        if (value + item) in B:
            print(item, value + item)
            break

寻找最富裕的小家庭

 
# Python题解代码
class TreeNode:
    def __init__(self, value):
        self.value = value
        self.children = []
def dfs(node, max_wealth):
    current_wealth = node.value
    for child in node.children:
        current_wealth += dfs(child, max_wealth)
    max_wealth[0] = max(max_wealth[0], current_wealth)
    return current_wealth
def main():
    N = int(input())
    wealth_values = list(map(int, input().split()))
    nodes = [TreeNode(value) for value in wealth_values]
    for _ in range(N - 1):
        parent, child = map(int, input().split())
        nodes[parent - 1].children.append(nodes[child - 1])
    max_wealth = [0]
    dfs(nodes[0], max_wealth)
    print(max_wealth[0])
if __name__ == "__main__":
    main()
查找接口成功率最优时间段

 
def main():
    minAverageLost = int(input())
    nums = input().split(" ")
    nums = [int(num) for num in nums]
    res = []
    length = 0
    for i in range(len(nums)):
        for j in range(i, len(nums)):
            if sum(nums[i:j+1]) <= minAverageLost * (j-i+1):
                if length > j-i+1:
                    continue
                elif length == j-i+1:
                    res.append([i, j])
                else:
                    res = [[i, j]]
                    length = j-i+1
    for i in range(len(res)):
        if i == len(res)-1:
            print(f"{res[i][0]}-{res[i][1]}")
        else:
            print(f"{res[i][0]}-{res[i][1]} ")
if __name__ == "__main__":
    main()

敏感字段加密

 
import sys
index = int(input()) # 输入命令字索引K
input = input() # 输入命令字符串S
charArray = list(input) # 将命令字符串转换为字符数组
command = "" # 当前正在解析的命令字
commandList = [] # 存储解析后的命令字列表
for i in range(len(charArray)):
    ch = charArray[i]
    if ch == '"' and ch in command: # 如果当前字符为双引号且命令字中已经包含了一个双引号
        command += '"' # 将双引号添加到命令字中
        commandList.append(command) # 将解析完毕的命令字添加到命令字列表中
        command = "" # 重置命令字
    elif '"' not in command and ch == '_': # 如果命令字不包含双引号且当前字符为下划线
        if command: # 如果命令字不为空
            commandList.append(command) # 将解析完毕的命令字添加到命令字列表中
            command = "" # 重置命令字
    elif i == len(charArray) - 1: # 如果已经到达字符串末尾
        command += ch # 将当前字符添加到命令字中
        commandList.append(command) # 将解析完毕的命令字添加到命令字列表中
        command = "" # 重置命令字
    else:
        command += ch # 将当前字符添加到命令字中
if index < 0 or index > len(commandList) - 1: # 如果命令字索引超出范围
    print("ERROR")
else:
    commandList[index] = "******" # 将指定索引的命令字替换为******
    result = []
    for item in commandList:
        result.append("_" + item) # 在命令字之前添加下划线
    result = "".join(result)
    result = result[1:] # 删除第一个下划线
    print(result)

虚拟游戏理财

 
number, N, X = map(int, input().split())
return_list = list(map(int, input().split()))
risk_list = list(map(int, input().split()))
max_cost_list = list(map(int, input().split()))
return_list.append(0)
risk_list.append(0)
max_cost_list.append(0)
number += 1
max_return = 0
max_status = ["0"] * number
max_return_risk = 0
for i in range(number):
    for j in range(i + 1, number):
        if risk_list[i] + risk_list[j] <= X:
            max_return_product_index = i if return_list[i] > return_list[j] else j
            other_return_product_index = i + j - max_return_product_index
            max_return_cost = min(N, max_cost_list[max_return_product_index])
            other_return_cost = min(N - max_return_cost, max_cost_list[other_return_product_index])
            cur_return = (
                return_list[max_return_product_index] * max_return_cost
                + return_list[other_return_product_index] * other_return_cost
            )
            if cur_return > max_return:
                max_return = cur_return
                max_return_risk = risk_list[i] + risk_list[j]
                max_dict = {
                    max_return_product_index: max_return_cost,
                    other_return_product_index: other_return_cost,
                }
                for k in range(number):
                    max_status[k] = str(max_dict.get(k, 0))
print(" ".join(max_status[:-1]))

IPv4地址转换成整数

 
import re
input = input()
ipSections = input.split("#") # 将输入的字符串按照"#"分割成4个小节
if len(ipSections) != 4: # 如果分割后的小节数量不等于4,则说明输入的IPv4地址格式不正确
  print("invalid IP") # 输出"invalid IP"表示非法IPv4地址
  exit() # 程序结束
firstSection = int(ipSections[0]) # 将第一个小节转换为整数
if firstSection < 1 or firstSection > 128: # 如果第一个小节的值不在1~128的范围内,则说明输入的IPv4地址格式不正确
  print("invalid IP") # 输出"invalid IP"表示非法IPv4地址
  exit() # 程序结束
for i in range(1, 4): # 遍历后面的3个小节
  sectionValue = int(ipSections[i]) # 将当前小节转换为整数
  if sectionValue < 0 or sectionValue > 255: # 如果当前小节的值不在0~255的范围内,则说明输入的IPv4地址格式不正确
    print("invalid IP") # 输出"invalid IP"表示非法IPv4地址
    exit() # 程序结束
hexString = "" # 创建一个字符串变量,用于拼接转换后的十六进制字符串
for i in range(4): # 遍历4个小节
  hexSection = hex(int(ipSections[i]))[2:] # 将当前小节转换为十六进制字符串
  if len(hexSection) == 1: # 如果转换后的十六进制字符串只有1位,则在前面补0
    hexSection = "0" + hexSection
  hexString += hexSection # 将转换后的十六进制字符串添加到字符串变量中
print(int(hexString, 16)) # 将拼接后的十六进制字符串转换为整数并输出

报文重排序

 
N = int(input().strip())
strings = input().strip().split()
letterMap = {}
for i in range(len(strings)):
    str = strings[i]
    if str == "":
        continue
    index = 0
    for j in range(len(str)):
        if str[j].isdigit():
            index = j
            break
    letter = str[:index]
    num = int(str[index:])
    letterMap[num] = letter
res = ""
for i in range(1, N+1):
    res += letterMap[i] + " "
print(res[:-1])

整数编码


 
num = int(input())
# 将待编码数字转换成二进制字符串
binaryStr = bin(num)[2:]
result = ''
# 每7位一组进行编码
for end in range(len(binaryStr), 0, -7):
    # 取出当前组需要编码的二进制字符串
    currentBinaryStr = binaryStr[max(end - 7, 0):end]
    # 判断当前字节是否为最后一个字节,设置最高位
    flag = '1' if end - 7 > 0 else '0'
    # 将当前字节转换成十进制数
    decimal = int(flag + currentBinaryStr, 2)
    # 将当前字节的十六进制字符串形式添加到结果中
    hexStr = hex(decimal)[2:].upper()
    # 如果十六进制字符串长度为1,需要在前面补0
    hexStr = '0' + hexStr if len(hexStr) == 1 else hexStr
    # 将当前字节的十六进制字符串形式添加到结果中
    result += hexStr
# 返回编码结果的十六进制字符串形式
print(result)


![[C/C++]string类常用接口介绍及模拟实现string类](https://img-blog.csdnimg.cn/direct/367e962274e24e46ba2abeee0f0bf148.png)



![[QT]自定义的QtabWidget](https://img-blog.csdnimg.cn/direct/52ed24c0306e4ea8a3846f376dfcb375.gif)













