题目:从键盘输入1个正整数,把这个整数的各位数字重新组合, 形成一个位数不变的最大数和最小数,并计算最大数与最 小数的差。 例如:输入241897,重组后最大数为:987421,最小数为:124789, 差为:862632 输入240097,重组后最大数为:974200,最小数为:200479, 差为:773721def bubblesortDown(date): length = len(date) #**********SPACE********** for i in range【?】: for j in range(0,length-i-1): if(date[j]>date[j+1]): t = date[j] date[j] = date[j+1] date[j+1] = t m=0 if(date[m]=='0'): while(date[m]=='0'): #**********SPACE********** 【?】 else: date[0] = date[m] date[m] = '0' content = "".join(date) return content def bubblesortUp(date): length = len(date) for i in range(length-1): for j in range(length-1,i,-1): #**********SPACE********** if(date[j-1]【?】date[j]): t = date[j-1] #**********SPACE********** 【?】 date[j] = t content = "".join(date) return content def diss(a): list1=list(a) max = int(bubblesortUp(list1)) min = int(bubblesortDown(list1)) dis=max-min print("最大数为:{},最小数为:{},差为:{}".format(max,min,dis) ) def main(): num = input("请输入一个正整数:") diss(num); if __name__ == '__main__': main()
时间: 2023-07-23 14:05:19 浏览: 237
从键盘输入n个整数,删除其中的最大数后输出剩余部分
这是一个关于重新组合正整数的程序,它会根据输入的正整数,计算出重组后的最大数和最小数,并计算它们的差值。以下是这个程序的完整代码:
```python
def bubblesortDown(date):
length = len(date)
for i in range(length):
for j in range(0,length-i-1):
if(date[j]>date[j+1]):
t = date[j]
date[j] = date[j+1]
date[j+1] = t
m=0
if(date[m]=='0'):
while(date[m]=='0'):
m += 1
else:
date[0] = date[m]
date[m] = '0'
content = "".join(date)
return content
def bubblesortUp(date):
length = len(date)
for i in range(length-1):
for j in range(length-1,i,-1):
if(date[j-1]<date[j]):
t = date[j-1]
date[j-1] = date[j]
date[j] = t
content = "".join(date)
return content
def diss(a):
list1=list(a)
max_num = int(bubblesortUp(list1))
min_num = int(bubblesortDown(list1))
diff = max_num - min_num
print("最大数为:{},最小数为:{},差为:{}".format(max_num,min_num,diff) )
def main():
num = input("请输入一个正整数:")
diss(num);
if __name__ == '__main__':
main()
```
你可以运行这个程序并输入一个正整数,它会输出重新组合后的最大数和最小数,并计算它们的差值。希望对你有帮助!
阅读全文