原题传送门 1. 题目描述 2. Solution 1 1、思路分析 简单处理直接使用 eval把字符串表达式直接求值。 用栈计算方式参考: HJ50 四则运算。 2、代码实现 import sys for line in sys.stdin: s = line.strip() print(int(eval(s)))
原题传送门 1. 题目描述 2. Solution import sys if sys.platform != "linux": file_in = open("input/HJ55.txt") sys.stdin = file_in for line in sys.stdin: n = int(line.strip()) cnt = 0 for i in range(1, n + 1): if i %
原题传送门 1. 题目描述 2. Solution 1 1、思路分析 把字符串从后往前遍历,求和,保存和的个位到结果,进位进入下一位求和,逆序输出结果。 2、代码实现 import sys if sys.platform != "linux": file_in = open("input/HJ57.txt") sys.stdin = file_in while True: try:
原题传送门 1. 问题描述 2. Solution 1、思路分析 遍历,求所有的真因子,对所有真因子求和。 2、代码实现 import sys if sys.platform != "linux": file_in = open("input/HJ56.txt") sys.stdin = file_in def is_perfect_number(n): factors = [1] i = 2 w
原题传送门 1. 问题描述 2. Solution 遍历2次,第1次计数,第2次找出第1次 import sys if sys.platform != "linux": file_in = open("input/HJ59.txt") sys.stdin = file_in def solve(s): from collections import Counter cnt = dict(Counter(s)) for
原题传送门 1. 问题描述 2. Solution 1、思路分析 堆或优先队列。 2、代码实现 import heapq import sys if sys.platform != "linux": sys.stdin = open("input/HJ58.txt") def solve(n, k, nums): nums.sort() [print(x, end=" ") for x in nums[:k]]
原题传送门 1. 问题描述 2. Solution 1、思路 从中间n//2向两侧遍历,找到第一组素数和即可。 2、实现 import sys if sys.platform != "linux": file_in = open("input/HJ60.txt") sys.stdin = file_in def is_prime(n): i = 2 while i * i <= n: if n
原题传送门 1. 问题描述 2. Solution import sys """ 放苹果分为两种情况,一种是有盘子为空,一种是每个盘子均均不空 设f(m, n) 为求解函数 case 1: 若有一个盘子为空,则问题转换为f(m, n-1) 将m个苹果放到 n-1个盘子中 case 2: 若每个盘子均不空,则在每个盘子上都先放1个苹果,问题转
原题传送门 1. 题目描述 2. Solution 1 1、思路分析 最朴素的想法直接转换成二进制字符串,统计1的个数。 2、代码实现 import sys if sys.platform != "linux": sys.stdin = open("input/HJ15.txt") def solve(n): n_bin = bin(n).replace("0b", "") print(n_bi
原题传送门 1. 题目描述 2. Solution 1 1、思路分析 遍历所有的长度为n的子串,统计每个子串的字符串g、c出现次数,保存g、c出现次数最多的子串为最终答案。 2、代码实现 import sys if sys.platform != "linux": file_in = open("input/HJ63.txt") sys.stdin = file_in
原题传送门 1. 问题描述 2. Solution 滑动窗口 import sys if sys.platform != "linux": file_in = open("input/HJ64.txt") sys.stdin = file_in def solve(n, s): cur = 1 start = 1 for c in s: if c == 'U':
原题传送门 1. 问题描述 2. Solution import sys if sys.platform != "linux": sys.stdin = open("input/HJ65.txt") """ 1) 状态定义 dp[i][j] 表示字符串s1中第i个字符和s2中第j个字符所构成的最长公共子串。 2) 初始状态 dp[i][j] = {0} 3)
原题传送门 1. 问题描述 2. Solution import re import sys if sys.platform != "linux": file_in = open("input/HJ66.txt") sys.stdin = file_in command_map = { # ("reset", ""): "reset what", ("res
原题传送门 1. 问题描述 2. Solution import sys if sys.platform != "linux": file_in = open("input/HJ67.txt") sys.stdin = file_in def solve(nums, target): if len(nums) == 1: return nums[0] == target else: for i
原题传送门 1. 题目描述 2. Solution import sys if sys.platform != "linux": file_in = open("input/HJ68.txt") sys.stdin = file_in def solve(): n = int(input().strip()) order = input().strip() # 0代表从高到低,1代表从低到高 reverse =
原题传送门 1. 题目描述 2. Solution 1 1、思路分析 按公式计算,。 2、代码实现 import sys if sys.platform != "linux": sys.stdin = open("input/HJ69.txt") while True: try: m = int(input().strip()) p = int(input().strip()) n = int(
原题传送门 1. 题目描述 2. Solution 1 1、思路分析 dim(A) = (m, n), dim(B) = (n, p) AB = mp 2、代码实现 import sys if sys.platform != "linux": sys.stdin = open("input/HJ70.txt") while True: try: n = int(input().strip()) dims = []
原题传送门 1. 问题描述 2. Solution java版思路分析,来自LeetCode 72 /* DP 1. Define the state dp[i][j] to be the minimum number of operations to convert word1[0...i) to word2[0...j) 2. Initial state For the base case, that is, to con
原题传送门 1. 题目描述 2. Solution 1、思路 从上到下,从左到右遍历,每个空位置。在第一个空位置,随便填一个合法的数字,递归在填充后续空位置。如果,期间出现没有数字可以填的话,就回退到上一个位置,换下一个数字,继续。 import sys if sys.platform != "linux": file_in = open(
原题传送门 1. 问题描述 2. Solution import re org = input().strip() words = re.split(r'\W+', org) words.reverse() print(" ".join(words))
原题传送门 1. 问题描述 2. Solution 参考LeetCode5 Longest Palindromic Substring import sys if sys.platform != "linux": file_in = open("input/HJ32.txt") sys.stdin = file_in def solve(s): n = len(s) dp = [[False] * n for _ in ra
原题传送门 1. 问题描述 2. Solution 1、思路 bit对齐 2、实现 import sys if sys.platform != "linux": file_in = open("input/HJ33.txt") sys.stdin = file_in def int2bin(n, bits=8): n_b = bin(n).replace("0b", "") return
原题传送门 1. 问题描述 2. Solution 1、思路分析 按ASCII码排序 2、代码实现 import sys if sys.platform != "linux": sys.stdin = open("input/HJ34.txt") for line in sys.stdin: s = line.strip() sorted_s = sorted(s, key=lambda x: ord(x)) print(&qu
原题传送门 1. 问题描述 2. Solution 1、思路分析 找规律 0 | 1 3 6 10 1 | 2 5 9 2 | 4 8 3 | 7 0 1 0 2 1 0 3 2、代码实现 import sys if sys.platform != "linux": file_in = open("input/HJ35.txt") sys.stdin = file_in """ 0 | 1 3 6 10 1 | 2
原题传送门 1. 题目描述 2. Solution 1 1、思路分析 先生成密钥,对输入的key按输入序去重(即保留第1次出现的字符)保存结果到letters2,然后遍历a~z,把未出现在letters2中的字符加入到letters2。 之后就是加密过程,按照下标取出对应字符即可。 2、代码实现 import sys if sys.platfor