Python实现Pat 1032. Sharing (25)
来源:互联网 发布:正在优化social club 编辑:程序博客网 时间:2024/06/01 23:01
题目
Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given {32, 321, 3214, 0229, 87}, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.
Input Specification:
Each input file contains one test case. Each case gives a positive integer N (<=10000) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the smallest number in one line. Do not output leading zeros.
Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287
解答
排序题,关键在于字符串的大小比较。比较两个字符串大小时如32和321,如果有相同前缀,则删除较长的字符串中的前缀,并比较剩下的数的大小,即32和321的比较结果应该是32和1的比较结果。
from functools import cmp_to_keydef cmpt(s1,s2): len1=len(s1) len2=len(s2) maxl=max(len1,len2) for i in range(maxl): if s1[i%len1]!=s2[i%len2]: return int(s1[i%len1])-int(s2[i%len2]) else: return 0line0=input().split(' ')[1:]line0.sort(key=cmp_to_key(cmpt))num=''for e in line0: num+=ecount=0#剔除无效的‘0’for e in num: if e=='0': count+=1 else: breaknum=num[count:]#特殊情况if not num: num='0'print (num)
又错了一个小点。不管了
- Python实现Pat 1032. Sharing (25)
- Python实现Pat 1032. Sharing (25)
- [PAT]1032. Sharing (25)(Java实现)
- 1032. Sharing (25)-PAT
- 【PAT】1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT:1032. Sharing (25)
- PAT 1032. Sharing (25)
- pat 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- PAT 1032. Sharing (25)
- 1032. Sharing (25) PAT
- PAT (Advanced) 1032. Sharing (25)
- PAT A 1032. Sharing (25)
- 进程控制
- 简单小项目:注释转换
- 火墙
- Git基础知识学习1
- 浅谈Java中的hashcode方法
- Python实现Pat 1032. Sharing (25)
- equals变量在前面或者在后面有什么区别吗?
- 以太坊执行miner.start返回null
- 当人工智能遇上伦理道德
- 第7章 数据规整化:清理、转换、合并、重塑(5)
- 【BIM应用】阿里巴巴北方最重要基础设施—张北云计算数据中心
- oracle数据库单个数据文件的大小限制
- 我在兄弟连学python(1)-------运算和运算符
- myeclipse + Maven下main方法找不到主类的一种情况