PAT 乙级 1026 发表于: 2023-03-09 分类于: 算法/PAT代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #!/usr/bin/env python # -*- coding: utf-8 -*- # author: a2htray # create date: 2023/3/9 """ PAT 乙级 1026 """ if __name__ == '__main__': start_clock, end_clock = map(int, input().split(' ')) duration = 1.0 * (end_clock - start_clock) / 100 h = int(duration // 3600) m = int(duration % 3600 // 60) s = str(duration % 3600 % 60) tokens = s.split('.') add = 1 if int(tokens[1][0]) >= 5 else 0 s = int(tokens[0]) + add print('%02d:%02d:%02d' % (h, m, s))
Go flag 自定义选项类型 发表于: 2023-03-09 分类于: 后端技术/Goflag 包定义了一系列函数,可用于定义命令行参数,支持的参数类型如下:string:flag.StringVar 函数bool:flag.BoolVar 函数time.Duration: flag.DurationVar 函数int: flag.IntVar 函数uint: flag.UintVar 函数float64: flag.Float64Var 函数int64: flag.Int64Var 函数uint64: flag.Uint64Var 函数
Go flag 支持选项简写 发表于: 2023-03-09 分类于: 后端技术/Go在命令行工具的开发过程中,我们常常需要设置一个同时支持短名称和长名称的选项,如 -d 等价于 --debug、-p 等价于 --password。在使用 Go flag 包的情况下,该需求的实现相当简单,只需要定义两个不同的 flag 指向同一个变量即可。
PAT 乙级 1024 发表于: 2023-03-08 分类于: 算法/PAT代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #!/usr/bin/env python # -*- coding: utf-8 -*- # author: a2htray # create date: 2023/3/8 """ PAT 乙级 1024 """ from typing import List class SciNumber: def __init__(self, s: str): self.sign = '-' if s[0] == '-' else '' nums, exp = s[1:].split('E') self.nums = [v for v in nums] self.left_move = exp[0] == '-' self.exp = int(exp[1:]) # print(self.sign, self.nums, self.left_move, self.exp) def to_number(self) -> List[str]: if self.left_move: ret = ['0'] * self.exp + self.nums del ret[ret.index('.')] return ret[0:1] + ['.'] + ret[1:] else: n = len(self.nums[2:]) if self.exp >= n: ret = self.nums + ['0'] * (self.exp - n) del ret[1] else: ret = self.nums for i in range(1, 1 + self.exp): ret[i], ret[i + 1] = ret[i + 1], ret[i] if ret[0] == '0': ret = ret[1:] return ret if __name__ == '__main__': sci_number = SciNumber(input()) print(sci_number.sign + ''.join(sci_number.to_number()))
PAT 乙级 1023 发表于: 2023-03-08 分类于: 算法/PAT代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #!/usr/bin/env python # -*- coding: utf-8 -*- # author: a2htray # create date: 2023/3/8 """ PAT 乙级 1023 """ if __name__ == '__main__': digits = [] for i, digit in enumerate(map(int, input().split(' '))): digits += [i] * digit digits.sort() if digits[0] != 0: print(''.join(map(str, digits))) exit(0) for i, v in enumerate(digits): if v != 0: digits[0], digits[i] = digits[i], digits[0] break print(''.join(map(str, digits)))
PAT 乙级 1021 发表于: 2023-03-08 分类于: 算法/PAT代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #!/usr/bin/env python # -*- coding: utf-8 -*- # author: a2htray # create date: 2023/3/7 """ PAT 乙级 1021 """ if __name__ == '__main__': counts = [0] * 10 for s in input(): counts[int(s)] += 1 for i, count in enumerate(counts): if count != 0: print('%d:%d' % (i, count))
PAT 乙级 1020 发表于: 2023-03-08 分类于: 算法/PAT代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #!/usr/bin/env python # -*- coding: utf-8 -*- # author: a2htray # create date: 2023/3/6 """ PAT 乙级 1020 """ from functools import cmp_to_key class MoonCake: def __init__(self, num, total_price): self.num = num self.total_price = total_price self.unit_price = 1.0 * self.total_price / self.num def cmp(mooncake1, mooncake2): return -1 if mooncake1.unit_price > mooncake2.unit_price else 1 if __name__ == '__main__': n_class, max_requirement = map(int, input().split(' ')) mooncakes = [] for item in zip(map(float, input().split(' ')), map(float, input().split(' '))): if item[0] == 0 or item[1] == 0: continue mooncakes.append(MoonCake(item[0], item[1])) mooncakes = sorted(mooncakes, key=cmp_to_key(cmp)) sale_price = 0.0 for i in range(n_class): if max_requirement >= mooncakes[i].num: sale_price += mooncakes[i].total_price max_requirement = max_requirement - mooncakes[i].num else: sale_price += max_requirement * mooncakes[i].unit_price max_requirement = 0 break print('%.2f' % sale_price)
PAT 乙级 1019 发表于: 2023-03-08 分类于: 算法/PAT代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #!/usr/bin/env python # -*- coding: utf-8 -*- # author: a2htray # create date: 2023/3/6 """ PAT 乙级 1019 """ def get_sorted_nums(num_str): dn = 4 - len(num_str) for _ in range(dn): num_str = '0' + num_str nums = list(map(int, num_str)) nums.sort() return 1 * nums[0] + 10 * nums[1] + 100 * nums[2] + 1000 * nums[3], \ 1 * nums[3] + 10 * nums[2] + 100 * nums[1] + 1000 * nums[0] num_str = input() num1, num2 = get_sorted_nums(num_str) if num1 == num2: print(f'{num1:04d} - {num2:04d} = 0000') exit(0) while num1 - num2 != 6174: diff = num1 - num2 print(f'{num1:04d} - {num2:04d} = {diff:04d}') num1, num2 = get_sorted_nums(str(diff)) diff = num1 - num2 print(f'{num1:04d} - {num2:04d} = {diff:04d}')
PAT 乙级 1018 发表于: 2023-03-08 分类于: 算法/PAT代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #!/usr/bin/env python # -*- coding: utf-8 -*- # author: a2htray # create date: 2023/3/6 """ PAT 乙级 1018 """ if __name__ == '__main__': n = int(input()) first = [0, 0, 0] second = [0, 0, 0] first_win = 0 second_win = 0 alphabet = ['B', 'C', 'J'] for _ in range(n): p1, p2 = input().split() if p1 == 'C' and p2 == 'J': first[1] += 1 first_win += 1 elif p1 == 'C' and p2 == 'B': second[0] += 1 second_win += 1 elif p1 == 'B' and p2 == 'J': second[2] += 1 second_win += 1 elif p1 == 'B' and p2 == 'C': first[0] += 1 first_win += 1 elif p1 == 'J' and p2 == 'B': first[2] += 1 first_win += 1 elif p1 == 'J' and p2 == 'C': second[1] += 1 second_win += 1 print(first_win, n - first_win - second_win, second_win) print(second_win, n - first_win - second_win, first_win) print(alphabet[first.index(max(first))], alphabet[second.index(max(second))])
PAT 乙级 1022 发表于: 2023-03-08 分类于: 算法/PAT代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #!/usr/bin/env python # -*- coding: utf-8 -*- # author: a2htray # create date: 2023/3/8 """ PAT 乙级 1022 """ if __name__ == '__main__': num1, num2, base = map(int, input().split(' ')) total = num1 + num2 if total == 0: print(0) exit(0) res = [] while total >= 1: res = [total % base] + res total = total // base if res[0] == 0: res = res[1:] print(''.join(map(str, res)))