26):题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
程序分析:学会分解出每一位数。
程序源代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
x = int(raw_input("请输入一个数:\n"))
a = x / 10000
b = x % 10000 / 1000
c = x % 1000 / 100
d = x % 100 / 10
e = x % 10
if a != 0:
print "5 位数:",e,d,c,b,a
elif b != 0:
print "4 位数:",e,d,c,b,
elif c != 0:
print "3 位数:",e,d,c
elif d != 0:
print "2 位数:",e,d
else:
print "1 位数:",e
以上实例输出结果为:
请输入一个数:
23459
5 位数: 9 5 4 3 2
请输入一个数:
3472
4 位数: 2 7 4 3
#!/usr/bin/python
# -*- coding: UTF-8 -*-
print( \'请输入大于10的数字:\' )
n=input()
x=[]
i=0;
while(n!=0):
x.append(n%10)
i+=1
n/=10
print( \'该数有 %d 位\n\' %i )
print( \'逆序为:\n\')
print( x[::] )
输出实例:
请输入大于10的数字:
12345
该数有 5 位
逆序为:
[5, 4, 3, 2, 1]
#!/usr/bin/python
# -*- coding: UTF-8 -*-
print \'输入大于10的数字:\'
n=input()
x=str(n)
for i in range(len(x)-1,-1,-1):
print x[i], # , 号设置不换行
测试输出结果:
输入大于10的数字:
12345
5 4 3 2 1
#!/usr/bin/python
# -*- coding: UTF-8 -*-
print( \'请输入大于10的数字:\' )
a =input()
if (len(str(a)) > 0) and (len(str(a)) <= 5) :
print "%s 是 %d 位数" %(a, len(str(a)))
newstr = str(a)[::-1]
print newstr
for i in newstr:
print i
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def output(num,l):
if l == 0:
return
print (num[l-1]),
output(num,l-1)
num = raw_input(\'输入小于5位正整数 :\' )
l = len(num)
output(num,l)
print \'\n长度为: %d\' % l
Python3 下使用列表的 reverse 方法:
#!/usr/bin/env python3
num = list(input(\'输入一个最多5位的数字:\'))
print(len(num))
num.reverse()
for i in range(len(num)):
print(num[i], end=\'\')
#!/usr/bin/python3
# coding:utf-8
s = str(input())
def fun(m):
if len(m) == 1:
return m[0]
else:
return (m[len(m) - 1] + fun(m[:(len(m) - 1)]))
if len(s) > 5:
print("输入数字超过限定位数,输入无效")
else:
print(\'数位:%s\n输入的数字:%s\n逆序数字:%s\' % (len(s), s, fun(s)))
#!/usr/bin/python
# coding:utf-8
arr=[]
def out_num(n):
length = len(str(n))
print "%d位数"%length
for i in range(1, length+1):
arr.append((n % (10**i))/10**(i-1))
print arr
out_num(245984)
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
s = 12345
s = str(s)[::-1]
print \'%d 位数\' % len(s)
for i in range(len(s)):
sys.stdout.write(s[i]+\' \')
Python3 测试实例:
# coding:utf-8
num=int(input("请输入一个正整数:"))
def fn(s):
if len(s)==1:
return(s[0])
else:
a=s[-1]
s=s[:-1]
return(a+fn(s))
while 1:
if num<=0 or len(str(num))>5:
num=int(input("输入错误,请重新输入:"))
else:
num=str(num)
print()
print("它是%d位数" % len(num))
print("逆序打印:",fn(num))
break
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def num(strl):
l = len(str(strl))
lis = []
for i in range(l):
x = strl % 10
strl /= 10
lis.append(x)
print "长度为:{0}, \n逆序为:{1}".format(l,\'\'.join(str(x) for x in lis) )
num(123)
Python3 下测试:
#!/usr/bin/python3
x=input(\'请输入一个数:\n\')
a = len(x)
print(\'这是一个{}位数\'.format(a))
b = -1
while a != 0:
a -= 1
if b == -1:
print(x[-1:],end=\' \')
b=b-1
else :
print(x[b:b+1],end=\' \')
b=b-1
Python3 下测试:
#!/usr/bin/python3
def backn(n):
ls = str(n); s = len(ls)
ls = ls[s-1:0:-1] + ls[0]
return ls, s
#测试:
backn(123456) #输出:([\'654321\'], 6)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
num1=int(input("Please input a int <10000:"))
for i in range(4,1,-1):
if num1>10**i:
print(i+1)
break
list1=[]
for j in range(i,-1,-1):
list1.append(int(num1/(10**j)))
num1=num1%(10**j)
for k in range(i,-1,-1):
print(list1[k])
Python3 下测试:
number = 12344
print("数字%d," % number, end="")
n = 0
result = ""
while number > 0:
result += str(number % 10)
number //= 10
n += 1
print("是%d位数, 逆序: %d" % (n, int(result)))
主要内容就是数字操作。如果感觉不错的话请多多点赞支持哦。。。
原文链接:https://blog.csdn.net/luyaran/article/details/80074827