- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex29.py
More file actions
Latest commit
38 lines (32 loc) · 1015 Bytes
/
Copy pathex29.py
File metadata and controls
38 lines (32 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'''
给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
'''
n=input("输入一个不多于5位的正整数:")
# 方法一(将正整数看作字符串)
print("方法一:")
print("该正整数的位数为%d"%len(n))
print("逆序打印该数字:"+"".join(reversed(n)))
print("\n")
# 方法二(分解数字)
print("方法二:")
n=int(n)
a=n//10000
b=n%10000//1000
c=n%1000//100
d=n%100//10
e=n%10
ifa:
print("该正整数的位数为5")
print("逆序打印该数字:%d%d%d%d%d"% (e, d, c, b, a))
elifb:
print("该正整数的位数为4")
print("逆序打印该数字:%d%d%d%d"% (e, d, c, b))
elifc:
print("该正整数的位数为3")
print("逆序打印该数字:%d%d%d"% (e, d, c))
elifd:
print("该正整数的位数为2")
print("逆序打印该数字:%d%d"% (e, d))
elife:
print("该正整数的位数为1")
print("逆序打印该数字:%d"%e)