python反转整数的几种方法_【python实例29】整数反转

给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

题解

1.

1) 程序分析

学会分解出每一位数。

2) 代码# !/usr/bin/python3

# -*- coding: UTF-8 -*-

x = int(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)

3) 效果请输入一个数:

1234

4 位数: 4 3 2 1

2.

1) 程序分析

用循环分解出每一位数。

2) 代码# !/usr/bin/python3

# -*- coding: UTF-8 -*-

n = int(input('enter a number: '))

t = 0

cout = 0

while n != 0:

t = t * 10 + n % 10

n = n // 10

cout += 1

print('the reversed number is %d, it has %d digit(s)' % (t, cout))

3) 效果enter a number: 1234

the reversed number is 4321, it has 4 digit(s)

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 [email protected]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值