时间:2018-11-23
概述:逆序 排序
Python 数学问题求解:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
#你这样出题对Python来说没有难度啊(出题人:你耍赖。。。)
s=input('请输入一个正整数:')
print('这是个{}位数。'.format(len(s)))
print('反序输出为:',s[::-1])
#好吧,刚才的不算
from functools import reduce
num=int(s)
long=len(str(num))#这样就可以处理0开头输入的数字了
#把各位数字分别放入列表
l=[num//(10**(long-i))%10 for i in range(1,long+1)]
num=reduce(lambda x,y:x*10+y,l[::-1])#用reduce反序把数在算出来
print('这是个{}位数。'.format(long))
print('反序输出为:',num)
'''
#原程序
#不能处理任意长度数字
x = int(raw_input("input a number:\n"))
a = x / 10000
b = x % 10000 / 1000
c = x % 1000 / 100
d = x % 100 / 10
e = x % 10
if a != 0:
print "there are 5 ",e,d,c,b,a
elif b != 0:
print "there are 4 ",d,c,b,a
elif c != 0:
print "there are 3 ",e,d,c
elif d != 0:
print "there are 2 ",e,d
else:
print "there are 1",e
'''