Digital rearrangement

Title Description

There is a positive integer x beef, beef need digit numbers in x are rearranged to obtain a new number (different from the number x), beef wondered whether this new number may be a multiple of the original x. Please come help him solve this problem.

Enter a description:

T + 1 input comprises line, comprising a first line integer t (1 ≤ t ≤ 10) , 
the next t lines, each line an integer x (1 ≤ x ≤ 10 ^ 6)

Output Description:

For each x, if possible after rearrangement becomes a multiple export its "Possible", otherwise output "Impossible".
Example 1

Entry

copy
2
14
1035

Export

copy
Impossible 
Possible 
problem-solving ideas: using the full array c ++ STL can greatly simplify the code, there is a function used during my c ++ classification described in
the following code:
#include <bits/stdc++.h>
using namespace std;
int main() {
    int T; cin >> T;
    while (T--) {
        int n; cin >> n;
        string s_n = to_string(n);
        sort(s_n.begin(), s_n.end());
        bool flag = false;
        do {
            int p_num = stoi(s_n);
            if (p_num!=n && p_num%n==0) {
                flag = true;
                break;
            }
        } while (next_permutation(s_n.begin(), s_n.end()));
        cout << (flag ? "Possible" : "Impossible") << endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/cstdio1/p/10994918.html