python12345输出54321_逆排序:输入12345,输出54321

输入123456,输出654321,两个都是int类型;

输入的参数为num/out,int类型;

返回值为int类型。

第一种:(传入的值为正整数)【如果是负数或者为零呢?】

out = 0

while num:

out = out * 10 + num % 10

num /= 10

return out

第二种(考虑了正负数、为零的情况):

out = 0

is_negative = False

if num < 0:

is_negative = True

# is_negative = "negative" if num < 0 else "positive"

num = abs(num)

while num:

out = out * 10 + num % 10

num /= 10

if is_negative:

return -out

else:

return out

最初的写法(不考虑任何情况):

def reserve_num(num):

remainder = 0

remainder = num %10

while num>1:

num /= 10

if num > 0:

remainder = remainder*10 + num %10

return remainder

运行结果:

if __name__ == '__main__':

print reserve_num(543211) #输出112345

你可能感兴趣的:(python12345输出54321_逆排序:输入12345,输出54321)