
用Python做ACM
William·Pan
福州大学本科在读生,对数据科学、计算机编程、商业模式等相关知识具有极高的学习激情,乐天派,从来相信这个时代只会越来越好,对自己即将投身这个时代的建设浪潮中感到无上光荣。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
用Python做50道ACM之《A+B Problem》
1.A+B Problemhttp://acm.fzu.edu.cn/problem.php?pid=1000import syslines=sys.stdin.readlines()for line in lines: line=[int(x) for x in line.rstrip().split(' ')] print(sum(line))原创 2019-08-29 12:42:48 · 1299 阅读 · 0 评论 -
用Python做50道ACM之《URLs》
17.URLshttp://acm.fzu.edu.cn/problem.php?pid=1033利用正则表达式来处理import sysimport redef check_urls(url_list): for url in url_list: protocol=re.search(r'(.*)://',url).group(1) has_p...原创 2019-08-31 08:16:32 · 313 阅读 · 0 评论 -
用Python做50道ACM之《四塔问题》
18.四塔问题http://acm.fzu.edu.cn/problem.php?pid=1036由 T(1)=1,T(2)=3,T(3)=5,T(4)=9,T(5)=13,T(6)=17,T(7)=25,T(8)=33以及T(15)=129可以得到如下函数所示规律。def min_times(number): count=0 for i in range(1,25001):...原创 2019-08-31 08:17:27 · 295 阅读 · 0 评论 -
用Python做50道ACM之《Ackermann Function》
19.Ackermann Functionhttp://acm.fzu.edu.cn/problem.php?pid=1042先编写递归函数,输入较小规模的数据观察输出结果,后利用规律编写函数直接获得结果。# 编写递归函数,输入较小规模数据进行观察def ackermann(m,n,dic): if str([m,n]) in dic.keys(): return...原创 2019-08-31 08:18:06 · 936 阅读 · 0 评论 -
用Python做50道ACM之《Hamming Problem》
20.Hamming Problemhttp://acm.fzu.edu.cn/problem.php?pid=1045符合要求的自然数一定是由p1,p2,p3中的任意1到3个数的自身连乘。import sysimport mathdef hamming(p1,p2,p3,i): if i==1: return min(p1,p2,p3) else: ...原创 2019-08-31 08:18:48 · 286 阅读 · 0 评论 -
用Python做50道ACM之《Lotto》
21.Lottohttp://acm.fzu.edu.cn/problem.php?pid=1047直接暴力遍历所有的情况import sysdef lotto(k,series): groups=[] for a1 in range(0,k): for a2 in range(a1+1,k): for a3 in range(a2...原创 2019-09-02 18:38:37 · 246 阅读 · 0 评论 -
用Python做50道ACM之《Number lengths》
22.Number lengthshttp://acm.fzu.edu.cn/problem.php?pid=1050import sysimport mathdef factorial_len(n): result=0 for i in range(1,n+1): result+=math.log10(i) return int(result+1...原创 2019-09-02 18:39:17 · 162 阅读 · 0 评论 -
用Python做50道ACM之《阅读顺序 》
23.阅读顺序http://acm.fzu.edu.cn/problem.php?pid=1054import sysline_left=0for read_in in sys.stdin.readlines(): if not read_in: break if line_left==0: line_left=int(read_in.rs...原创 2019-09-02 18:39:51 · 200 阅读 · 0 评论 -
用Python做50道ACM之《赋值问题》
24.赋值问题http://acm.fzu.edu.cn/problem.php?pid=1055import sysline_left=0for read_in in sys.stdin.readlines(): if not read_in: break if line_left==0: line_left=int(read_in.rs...原创 2019-09-02 18:40:29 · 227 阅读 · 0 评论 -
用Python做50道ACM之《扫雷游戏》
25.扫雷游戏http://acm.fzu.edu.cn/problem.php?pid=1056import sysdef transac_filed(lines,length,filed): # 先在'雷场'四周围上一圈'土',方便后续的计算操作 for i in range(0,lines): filed[i].insert(0,'.') ...原创 2019-09-02 18:41:05 · 364 阅读 · 0 评论 -
用Python做50道ACM之《a^b》
26.a^bhttp://acm.fzu.edu.cn/problem.php?pid=1057def ab_sum(a,b): number=a**b while True: number_li=[int(x) for x in list(str(number))] number=sum(number_li) if number...原创 2019-09-02 18:42:05 · 379 阅读 · 0 评论 -
用Python做50道ACM之《老师的苦恼》
27.老师的苦恼http://acm.fzu.edu.cn/problem.php?pid=1059import sys for read_in in sys.stdin.readlines(): if not read_in: break read_in.rstrip() print(read_in.lower())原创 2019-09-02 18:42:43 · 300 阅读 · 0 评论 -
用Python做50道ACM之《Fibonacci数列》
28.Fibonacci数列http://acm.fzu.edu.cn/problem.php?pid=1060动态规划填表的方式最快def fibonacci(n): if n==1 or n==2: return 1 else: a=b=1 for i in range(3,n+1): a,b=b,a...原创 2019-09-02 18:43:16 · 244 阅读 · 0 评论 -
用Python做50道ACM之《矩阵连乘》
29.矩阵连乘http://acm.fzu.edu.cn/problem.php?pid=1061import sysdef times(scheme): left_idx=[] # 用以记录左半括号所在位置 idx=0 # 用以记录当前“指针”所在位置 times=0 # 用以保存总的计算次数 while idx<len(scheme): ...原创 2019-09-02 18:43:49 · 765 阅读 · 0 评论 -
用Python做50到ACM之《飞船赛》
16.飞船赛http://acm.fzu.edu.cn/problem.php?pid=1021思路:本质就是排序,求的是排序过程中进行交换的次数。为方便计数,这里采用冒泡排序算法归并排序计数问题未解决,且对10^6数量级的数处理情况不理想需要采用更高级的排序算法,有待进一步完善import sysdef bubble(arr): count=0 for i in r...原创 2019-08-31 08:15:53 · 363 阅读 · 0 评论 -
用Python做50道ACM之《Number Trapezium》
15.Number Trapeziumhttp://acm.fzu.edu.cn/problem.php?pid=1020难点在于判定路径重叠思路:采用动态规划算法,自底向上进行运算。每次运算结果保存当前运算行的当前运算节点的左最大子路径和右最大子路径。任意两个相邻节点,对于它们的交叉路径(即左节点的右路径和右节点的左路径),判断二者是否有路径重叠,若是则保存两节点中较大节点的右(若大的为左...原创 2019-08-30 09:58:32 · 501 阅读 · 1 评论 -
用Python做50道ACM之《Duplicate Pair 》
2.Duplicate Pairhttp://acm.fzu.edu.cn/problem.php?pid=1001import syswhile True: line=sys.stdin.readline() if not line: break line=[int(x) for x in line.rstrip().split( )] i...原创 2019-08-29 12:44:27 · 649 阅读 · 0 评论 -
用Python做50道ACM之《HangOver 》
3.HangOverhttp://acm.fzu.edu.cn/problem.php?pid=1002import sysfrom fractions import Fractionimport decimaldecimal.getcontext().prec=2for c in sys.stdin: c=float(c) if c==0.00: b...原创 2019-08-29 12:46:58 · 798 阅读 · 0 评论 -
用Python做50道ACM之《Counterfeit Dollar 》
4.Counterfeit Dollarhttp://acm.fzu.edu.cn/problem.php?pid=1003由于只有一枚是假的,因此若出现左右不平衡的情况,则没有放在天枰上的银币一定是真的。先根据第一和第二次称重的结果情况来确定出一定是真的银币以及可能是假的银币,然后在第三次称重重,若出现可能是假的银币在此次称重后确定的结论和之前的结论矛盾,则该银币为真,否则为假。impor...原创 2019-08-29 12:47:51 · 687 阅读 · 0 评论 -
用Python做50道ACM之《Number Triangle 》
5.Number Trianglehttp://acm.fzu.edu.cn/problem.php?pid=1004自底向上地解决,从倒数第二行开始,每个数字用其本身和其左右子节点中较大值的和来替代。# 动态规划import sysline_left=0 # 每个用例剩余未读取行数while true: read_in=sys.stdin.readline() if...原创 2019-08-29 12:48:35 · 774 阅读 · 0 评论 -
用Python做50道ACM之《Fast Food 》
6.Fast Foodhttp://acm.fzu.edu.cn/problem.php?pid=1005重叠性未消除,本题需进一步完善import sysdef sum_min_distance(restaurants_locations,depot_num,depots_group_min_distance): if len(restaurants_locations)==1...原创 2019-08-29 12:49:32 · 719 阅读 · 0 评论 -
用Python做50道ACM之《Hay Points》
7.Hay Pointshttp://acm.fzu.edu.cn/problem.php?pid=1008import sysdef hay_points(discribtions,haypoint_dict): money=0 for i in range(0,len(discribtions)): disc=discribtions[i] ...原创 2019-08-29 12:50:20 · 707 阅读 · 0 评论 -
用Python做50道ACM之《Beavergnaw 》
8.Beavergnawhttp://acm.fzu.edu.cn/problem.php?pid=1010思路:啃掉的体积=大圆柱体体积-小圆柱体体积-两个圆台体积计算过程如下:大圆柱体体积=SD=D*pi*(D/2)^2=(pi*D^3)/4小圆柱体体积=sd=(pi*d^3)/4圆台体积=(1/3)*(S+s+pi*(D*d)/4)*(D-d)/2 =(1/6)*(...原创 2019-08-29 12:51:17 · 838 阅读 · 0 评论 -
用Python做50道ACM之《Power Strings 》
9.Power Stringshttp://acm.fzu.edu.cn/problem.php?pid=1011需利用多线程算法进行改进import sysdef max_repeat(s): if len(s)==0: print(0) elif s[0]*len(s)==s: print(len(s)) elif len(s)...原创 2019-08-29 12:52:04 · 683 阅读 · 0 评论 -
用Python做50道ACM之《Relatives》
10.Relativeshttp://acm.fzu.edu.cn/problem.php?pid=1012思路:根据“相对质数”的定义可知,如果两个数没有除1以外的公因数,则二者互为“相对质数”;质数除了它本身,任何小于它的正整数都是它的“相对质数”;1没有相对质数。import sysimport mathdef relatively_prime_num(n): # some...原创 2019-08-29 12:52:42 · 750 阅读 · 0 评论 -
用Python做50道ACM之《土地划分》
11.土地划分http://acm.fzu.edu.cn/problem.php?pid=1015划分的区块数=四边的交点数+中间的交点数-和四边重叠的边的边数import sysdef count_lands(long,wide,line_num:'len(position_list)-1',line_table): sideways=0 dots=0 for ...原创 2019-08-30 09:54:56 · 824 阅读 · 0 评论 -
用Python做50道ACM之《Playing with Calculator 》
12.Playing with Calculatorhttp://acm.fzu.edu.cn/problem.php?pid=1017import sysfor each in sys.stdin: # 因为按‘=’键的次数可为0,因此存在N即为k本身的情况,当k为个位数或者k本身各位都为同一个数字,可视为平凡情况来快速给出结论 k=int(each.rstrip())...原创 2019-08-30 09:55:41 · 421 阅读 · 0 评论 -
用Python做50道ACM之《Maximal Sum》
13.Maximal Sumhttp://acm.fzu.edu.cn/problem.php?pid=1018直接进行枚举的效果非常不好,需改进是否是一维的最大子数组的变体import sysimport numpy as npdef maximal_sum(matrix:'ndarray',N): max_sum=-float('inf') for h in ra...原创 2019-08-30 09:56:41 · 426 阅读 · 0 评论 -
用Python做50道ACM之《猫捉老鼠 》
14.猫捉老鼠http://acm.fzu.edu.cn/problem.php?pid=1019难点主要在于如何判断是否永远追不上。思路:若某一时刻猫和老鼠同时处在它们之前已经处过的状态,则二者永远不会相遇。import sysdef cat_mouse(room): times=0 catch_up=True for i in range(0,10): ...原创 2019-08-30 09:57:35 · 854 阅读 · 0 评论 -
用Python做50道ACM之《洗牌问题》
30.洗牌问题http://acm.fzu.edu.cn/problem.php?pid=1062因为一副牌中每张牌具有唯一性,因此使用递增数字代替每张牌构建给定2n长度的列表,然后按题目规则进行列表变化记录直到第一次恢复原来严格递增顺序时候的次数即可。但是这个方法面对较大的n时计算复杂度非常大。def times(n): li=[i for i in range(0,2*n)] ...原创 2019-09-02 18:44:33 · 519 阅读 · 0 评论