[蓝桥杯] 算法训练 素因子去重

// http://lx.lanqiao.cn/problem.page?gpid=T513

Scanner in = new Scanner(System.in);
long num = in.nextLong();
HashSet<Integer> set = new HashSet<>();

while (num > 1) {
    int i = 2;
    while (true) {
        if (num % i == 0) {
            set.add(i);
            num /= i;
            break;
        }
        i++;
    }
}

int ret = 1;
for (int i : set) {
    ret *= i;
}
System.out.println(ret);

发表评论