''' 输入一组非负数, 输出这些数的拼接,保证该拼接后的数最大 input 20,203 output 20320 '''
import sys
def split_number(number):''':param number: 123:return: [1, 2, 3]'''length = len(str(number))i = length-1str_num = [0 for i in range(length)]n = numberwhile n > 0:temp = n % 10str_num[i] = tempi -= 1n = (n-temp) // 10return str_numdef who_is_first(num1, num2):num1_char = split_number(num1) #num2_char = split_number(num2)flag = Truelength_num1 = len(num1_char) # 第一个数的位数length_num2 = len(num2_char) # 第二个数的位数i = 0j = 0while i < length_num1 and j < length_num2 :if num1_char[i] < num2_char[j]:flag = Falsereturn flagelif num1_char[i] == num2_char[j] and length_num1 != length_num2:if i == len(num1_char)-1: # 第一个数比较短,再看第二个数的第(length_num1+1)个数是否比第一个数的第一位大if num2_char[(length_num1+1)-1] > num1_char[0]:flag = Falseelse:flag = Truereturn flagelif j == len(num2_char)-1: # 第二个数比较短if num1_char[(length_num2+1)-1] > num2_char[0]: # 第二个数比较短,再看第一个数的第(length_num2+1)个数是否比第二个数的第一位大flag = Trueelse:flag = Falsereturn flagelse:flag = Truereturn flagi += 1j += 1return flagdef my_sort(numbers):'''指定规则排序 选择排序法:param numbers::return:'''for i in range(0, len(numbers)-1):the_first = ifor j in range(i+1, len(numbers)):if who_is_first(numbers[j], numbers[the_first]):the_first = jtemp = numbers[i]numbers[i] = numbers[the_first]numbers[the_first] = tempreturn numbers
numbers = list(map(int, input().split(',')))for num in my_sort(numbers):sys.stdout.write(str(num))
详细解决方案
bilibili-2020算法笔试第一题-输出数组拼接最大数
热度:58 发布时间:2023-12-17 03:20:27.0
相关解决方案
- 2020 IEEE Fellow名单出炉:华人近三成,叶杰平、张潼、周博文、熊辉等AI大牛入选
- 2020-04-01-Cplusplus-History
- 2020。
- 2020 年 5 月全国程序员工资出炉!
- Flutter 2020:跨平台状态
- Adobe After Effects 2020 17.1.0.72特别版
- Adobe Media Encoder 2020 14.2.0 特别版
- 反汇编逆向神器 x64dbg 2020.05.15 修订版
- Premiere Pro 2020 (v14.0.4.18) 绿色特别版
- 2020-05-20
- 2020-04-18
- idea 2020.3.2菜单栏不见了
- 2020.10.20读取txt文件找出其中末尾后两位出现次数最多的
- 2020-10-13冒泡排序
- React 项目npm ERR! A complete log of this run can be found in: npm ERR! ..._logs\2020-04-...debug.log
- CVE-2020-0796 POC
- 2020-11-22几何变换、形态学操作、图像平滑
- 2020-11-8机器学习-逻辑回归
- python基础(面向对象:类、对象、魔法方法)-----10(2020-09-29)
- CVE-2020-17519复现
- Krypton DP,01背包 (2020 China Collegiate Programming Contest Changchun Onsite)
- 2020-12-23组会总结
- 2020.01.27 再见24号
- 2020-1-22
- Apache Tomcat文件包含漏洞复现(CVE-2020-1938)
- 【文献阅读】2020-SIGIR-Group-Aware Long- and Short-Term Graph Representation Learning for Sequential Group
- 【文献阅读】 2020-WWW-Attentive Sequential Models of Latent Intent for Next Item Recommendation
- #2020.1.26笔记——springdatajpa
- NOIP 2020 赛前复习摘要
- 2020.12.02 比赛总结题解合集