你可以对整数
选择一个大于等于
给定正整数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
scanner.close();
System.out.println(minCost(N));
}
private static int minCost(int n) {
if (n == 1) return 0;
int cost = 0;
// 分解质因数
for (int i = 2; i * i <= n; i++) {
// 计算质因数i的指数
while (n % i == 0) {
cost += i; // 累加质因数作为成本
n /= i;
}
}
// 如果剩余的n是一个质数
if (n > 1) {
cost += n;
}
return cost;
}
}
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int n = in.nextInt();
int cost = 0;
for (int i = 2; n != 1; i++) {
while (n != 1 && n % i == 0) {
cost += i;
n /= i;
}
}
System.out.println(cost);
}
} #include <iostream>
using namespace std;
int minCost(int N) {
if (N == 1) return 0;
int cost = 0;
for (int p = 2; p * p <= N; ++p) {
if (N % p == 0) {
int e = 0;
while (N % p == 0) {
e++;
N /= p;
}
cost += p * e;
}
}
if (N > 1) cost += N; // 剩余未分解的大于1的质数
return cost;
}
int main() {
int N;
cin >> N;
cout << minCost(N) << endl;
return 0;
}