题目九:收件邮箱
1. 题目描述
已知字符串str,str表示邮箱的不标准格式。
其中”.”会被记录成”dot”,”@”记录成”at”。
写一个程序将str转化成可用的邮箱格式。(可用格式中字符串中除了开头结尾所有”dot”,都会被转换,”at”只会被转化一次,开头结尾的不转化)
2. 输入描述
输入字符串str.(1<=strlen(str)<=1000)
3. 输出描述
输出转化后的格式。
4. 示例
4.1 输入
mxyatoxcoderdotcom
4.2 输出
mxy@oxcoder.com
5. 答案
class Solution: def __init__(self) -> None: pass def solution(self, str): result = None # TODO: 请在此编写代码 result=str.replace('dot','.') result=result.replace('at','@',1) if result[0]==".": result="dot"+result[1:] if result[0]=="@": result="at"+result[1:] if result[-1]==".": result=result[:-1]+"dot" if result[-1]=="@": result=result[:-1]+"at" return result if __name__ == "__main__": str = input().strip() s = Solution() result = s.solution(str) print(result)
题目十:最长递增的区间长度
1. 题目描述
给一个无序数组,求最长递增的区间长度。如:[5,2,3,8,1,9] 最长区间 2,3,8 长度为 3
2. 输入描述
第一行输入整数n。(1<=n<=10000)表示数组的大小
第二行给出n个整数a.(-1e9<=a<=1e9)
3. 输出描述
输出转化后的格式。
4. 示例
4.1 输入
6
5 2 3 8 1 9
4.2 输出
3
5. 答案
class Solution: def __init__(self) -> None: pass def solution(self, n, arr): result = None # TODO: 请在此编写代码 result=0 t=1 arr.append(-1e9-1) for i in range(n): if arr[i+1]>arr[i]: t+=1 else: result=max(result,t) t=1 return result if __name__ == "__main__": n = int(input().strip()) arr = [int(item) for item in input().strip().split()] s = Solution() result = s.solution(n, arr) print(result)
题目十一:小玉家的电费
1. 题目描述
小玉家今天收到了一份电费通知单。上面写着:月用电量在150千瓦时及以下部分按每千瓦时0.4463元执行;月用电量在151~400千瓦时的部分按每千瓦时0.4663元执行;月用电量在401千瓦时及以上部分按每千瓦时0.5663元执行。请根据电价规定,计算出应交的电费应该是多少。
2. 输入描述
输入一个整数,表示用电总计(单位以千瓦时计),不超过10000。
3. 输出描述
输出一个数,保留到小数点后1位(单位以元计,保留到小数点后一位)。
4. 示例
4.1 输入
267
4.2 输出
121.5
5. 答案
import sys amount = int(sys.stdin.readline().strip()) result = 0 if amount <= 150: result = 0.4463 * amount elif 151 <= amount <= 400: result = 0.4463 * 150 + 0.4663 * (amount - 150) else: result = 0.4463 * 150 + 0.4663 * 250 + 0.5663 * (amount - 400) print('%.1f' % result) # 注意保留小数位
题目十二:单词逆序
1. 题目描述
对于一个字符串,请设计一个算法,只在字符串的单词间做逆序调整。例如:输入“I am a boy!”,输出“boy! a am I”。
2. 输入描述
输入一行字符串str。(1 <= strlen(str) <= 10000)
3. 输出描述
返回逆序后的字符串。
4. 示例
4.1 输入
It’s a dog!
4.2 输出
dog! a It’s
5. 答案
import sys s = sys.stdin.readline().strip().replace('\n', '') lst = s.split(' ') for i in range(len(lst) - 1, -1, -1): print(lst[i], end='') if i != 0: print(' ', end='')
题目十三:小Q整数分割
1. 题目描述
小Q决定吧一个整数n,分割成k个整数。
每个整数必须大于等于1。
小Q有多少方案。
2. 输入描述
输入整数n,k。(1 <= n, k<= 100)
3. 输出描述
输出方案数。答案对1e9+7取模。
4. 示例
4.1 输入
3 3
4.2 输出
1
5. 答案
import sys n, k = map(int, sys.stdin.readline().split()) if k > n: # 特判下k > n的情况 print(0) sys.exit() a = b = 1 for i in range(1, k): a *= (n - i) b *= i a /= b print('%.0f' % (a % (1e9 + 7)))
题目十四:新型美丽数列
1. 题目描述
定义美丽数列A:
- 数列中相邻的数越是靠内相对大小加一,a[2]=a[1]+1,a[n-2]=a[n-1]+1…
- 距离边缘距离相等的数的大小相等:a[0] = a[n-1],a[1] = a[n-2]…
通过修改数列中的值使得给定数列变成美丽数列。
修改后的值必须仍是正整数。
小Q有多少方案。
2. 输入描述
第一行输入整数n。(1 <= n <= 1000)表示数列的大小。
第二行输入n个整数。
3. 输出描述
输出最小修改次数。
4. 示例
4.1 输入
3
1 1 1
4.2 输出
1
5. 答案
import math import sys def diffnum(list): map = {} for index in range(len(list)): map[str(index)] = str(list[index] - index) return map def countnum(map): map1 = {} for key,value in map.items(): if value in map1.keys(): map1[str(value)] = map1[str(value)] + 1 else: map1[str(value)] = 1 return map1 list = [1,2,3,2,5,8,5,4,5,2,1] split_num = math.ceil(len(list) / 2) left_list_num = diffnum(list[0:split_num]) left_list_count = countnum(left_list_num) list_re = list[::-1] right_list_num = diffnum(list_re[0:split_num]) right_list_count = countnum(right_list_num) sum_map = left_list_count.copy() for key,value in right_list_count.items(): if key in sum_map.keys(): sum_map[key] = sum_map[key] + value else: sum_map[key] = value max_value = sorted(sum_map.values())[-1] log = sys.maxsize for key,value in sum_map.items(): if max_value == value: log = key break if log == sys.maxsize: print("错误") exit(1) count = 0 for key,value in left_list_num.items(): if value != log : list[int(key)] = list[int(key)] + (int(log) - int(value)) count = count + 1 list = list[::-1] num_log = 0 if len(list) % 2 !=0: num_log = 1 for key,value in right_list_num.items(): if num_log == 1 and key == str(split_num-1): break if value != log : list[int(key)] = list[int(key)] + (int(log) - int(value)) count = count + 1 print(count) print(list)
题目十五:熊孩子拜访
1. 题目描述
已知存在一个长度为n的整数序列A,A中所有元素按照从小到大排序,现在执行倒置一段序列。请你找出A序列的倒置子序列。如果没有,输出“0 0”。
2. 数据范围
1<=n<=1000
1<=num<=10000
3. 输入描述
第一行输入整数n。(1 <= n <= 1000)表示数列的大小。
第二行输入n个整数。
4. 输出描述
输出最小修改次数。
5. 示例
5.1 输入
4
1 3 2 4
5.2 输出
2 3
6. 答案
class Solution: def __init__(self) -> None: pass def solution(self, n, arr): result = [] # 保存右值 max = 0 # 保存左值 min = 0 next = 0 for item in arr: if next>item and item>max: max=next min=item elif next<min and item>max: min=next next=item result.append(str(min)) result.append(str(max)) if len(result)==0: result=["0","0"] return result if __name__ == "__main__": n = int(input().strip()) arr = [int(item) for item in input().strip().split()] sol = Solution() result = sol.solution(n, arr) print(" ".join(result))
题目十六:走楼梯
1. 题目描述
现在有一截楼梯, 根据你的腿长, 你一次能走 1 级或 2 级楼梯, 已知你要走 n 级楼梯才能走到你的目的楼层, 请实现一个方法, 计算你走到目的楼层的方案数。
2. 输入描述
输入你要去的楼层。
3. 输出描述
输出你走到目的楼层的方案数。
4. 示例
4.1 输入
5
4.2 输出
8
5. 答案
class Solution: def __init__(self) -> None: pass def solution(self, n): if isinstance(n, int) and n > 0: basic_dic = {1: 1, 2: 2} if n in basic_dic.keys(): return basic_dic[n] else: return self.solution(n - 1) + self.solution(n - 2) else: return False if __name__ == "__main__": n = int(input().strip()) sol = Solution() result = sol.solution(5) print(result)