Python【每日一问】29

问:

【基础题】:给一个不多于 5 位的正整数,要求:一、求它是几位数,二、逆序印出各位数字
【提高题】:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上 5, 然后用和除以 10 的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

答:

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

方法1:

num = input("请输入不大于五位数的数字:")
print("长度为:%d" % len(num))
print("反转后数字为:%s" % num[::-1])

 

方法2:

def f(n):
    str_n = str(n)
    length = len(str_n)
    reversed_n = int(str_n[::-1])
    return length, reversed_n
​
​
if __name__ == "__main__":
    c = input("请输入不多于5位的数字:")
    a, b = f(c)
    print("它是%s位数,逆序为%s" % (a, b))

 

方法3:

x = input("请输入不多于5位的数字:")
print(len(x))
​
for i in range(0, len(x)).__reversed__():
    print(x[i], end=" ")

 

方法4:

num = input("请输入不大于五位数的数字:")
length = len(num)
print("{}是{}位数".format(num, length))
print("{}逆序后的数字为:{}".format(num, num[::-1]))

 

方法5:

msg = input('请输入一个不多于5位的正整数>>>')
num = int(msg)
n = len(msg)
print("该数字是 {} 位数".format(n))
for i in range(n):
    single_num = num % 10
    print(single_num)
num = num // 10

 

方法6:

num = input("Please input a num:")
print("长度是:{0}".format(len(num)))
print("倒序数字是:{0}".format(num[::-1]))

 

方法7:

num = input("输入一个不大于5位的正整数:")
print("{}是一个{}位数,倒数打印{}".format(num,len(num),num[::-1]))

 

方法8:

number = input("Enter a number: ")
print(f'Number has got {len(number)} digits')
print(f'Reversed Number is {number[::-1]}')

 

提高题:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上 5, 然后用和除以 10 的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。

方法1:

telephone_num = input("加密前的数据:")
telephone_num_new = []
for number in telephone_num:
    int_num = int(number)
    int_num = (int_num + 5) % 10
    telephone_num_new.append(int_num)
​
telephone_num_new[0], telephone_num_new[3] = telephone_num_new[3], telephone_num_new[0]
telephone_num_new[1], telephone_num_new[2] = telephone_num_new[2], telephone_num_new[1]
​
number_str = ""for number_new in telephone_num_new:
    number_str = number_str + str(number_new)
​
print("加密后的数据:", number_str)

 

方法2:

number = input('Enter data to be encryted: ')
basic_element = list(range(10))
dictionary = dict(zip(basic_element, basic_element[5:] + basic_element[:5]))
​
encrypted_number = [str(dictionary[int(digit)]) for digit in number]
​
encrypted_number[0],encrypted_number[3], encrypted_number[1],encrypted_number[2] = \
encrypted_number[3],encrypted_number[0], encrypted_number[2],encrypted_number[1]
​
encrypted_number = ''.join(encrypted_number)
print(f'Encrypted number: {encrypted_number}')

 

方法3:

while True:
    tele_num = input("输入电话号码:")
    if len(tele_num) == 4:  # 判断输入字符串位数
        try:
            if int(tele_num) >= 0:  # 判断是否为4位整数
                break
        except:
            pass
out_tele_num = []
for num in tele_num:
    num = (int(num) + 5) % 10
    out_tele_num.append(num)
for i in out_tele_num:
    print(i, end='')
print('')
print("{}".format(''.join(map(str, out_tele_num[::-1]))))

 

posted on 2019-05-23 14:50  D小调的微笑  阅读(219)  评论(0编辑  收藏  举报