把原数字的2到9倍都算出来,依次装入multiset里面比较是否相等即可。
我这里用List代替了multiset:
import java.util.*; public class Main { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int k = 0; k < t; ++k){ int x = sc.nextInt(); List<Integer> v = f(x); boolean b = true; for(int i = 2; i <= 9; ++i){ if(v.equals(f(i*x))){ b = false; System.out.println("Possible"); break; } } if(b) System.out.println("Impossible"); } } static List<Integer> f(int n){ int[] v = new int[10]; String s = Integer.toString(n); for(int i = 0 ; i < s.length(); ++i){ v[s.charAt(i) - '0']++; } List<Integer> r = new ArrayList<>(); for(int i = 0; i < 10; ++i) r.add(v[i]); return r; } }