【编程题】网易有道笔试题——数字位数全排列整除问题

问题描述

输入为t+1行,第一行为为数字t,下面的t行数字分别作为输入x进行判断,对一个数字x,调整x各个数位生成新的数,如果生成的新的数是原来数字的整数倍,则输出“Possible”,否则输出“Impossible”。

例如有如下输入:

2
14
1035
则输出为:

Impossible
Possible

代码

# coding=utf-8

t = input()
x = []
for i in range(int(t)):
    x.append(input())
#print(x)

def permutation(result, str, list):

    if len(list) == 1:
        result.append(str  + list[0])
    else:
        for temp_str in list:
            temp_list = list[:]
            temp_list.remove(temp_str)
            permutation(result, str + temp_str, temp_list)


for i in range(int(t)):
    #print(x)
    x_ = x[i]
    #print(x_)
    x_list = []
    test = []
    result = []
    for i in range(len(str(x_))):
        x_list.append(x_[i])
    permutation(test, "",x_list)
    a = 0
    for item in test:
        if int(item) % int(x_) ==0 and int(item)/int(x_) != 1:
            a = a + 1
    if a ==0:
        print('Impossible')
    else:
        print('Possible')

你可能感兴趣的:(python,2018秋招笔试题)