分解因数_年终奖_因数个数
分解因数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
StringBuilder result = new StringBuilder();
int num = sc.nextInt();
int count = 0;
result.append(num + " " + "=");
int j = num;
for (int i = 2;i<=Math.sqrt(j);i++) {//找出素数因数!
while (num % i == 0) {//说明是因数!
result.append(" "+i+" "+"*");//添加到结果中!
num = num/i; //进一步分解!
count++; //记录因数个数!
}
if (num == 1) break;//num为1不需要分解!
}
if (num != 1) result.append(" "+ num);//不为1说明因数就是它本身!
if (result.charAt(result.length()-1) == '*') {//将最后的*和空格删除!
result.deleteCharAt(result.length()-1);
result.deleteCharAt(result.length()-1);
}
System.out.println(result);
//result.setLength(0);//设置字符序列的长度! 相当于置空!
}
}
} 年终奖
import java.util.*;
public class Bonus {
public int getMost(int[][] board) {
// write code here
int row = board.length;
int col = board[0].length;
for(int i = 1;i<row;i++){
board[i][0] += board[i-1][0];
board[0][i] += board[0][i-1];
}
for(int i = 1;i<row;i++){
for(int j = 1;j<col;j++){
board[i][j] += Math.max(board[i-1][j],board[i][j-1]);
}
}
return board[row-1][col-1];
}
} 因数个数
// write your code here
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int x = sc.nextInt();
int ret = 0;
// 36 = 2*2*3*3! 因数为 2 !
//我们以36为例:我们先求出一个因数,
//然后对该因数相除连续相除自到结果不含该因子时,我们就可以找下一个因子!
//找到 2 36/2 = 18 18/2 = 9 次数++!
// 3 9/3 = 3 3/3 = 1 次数++!
for(int i =2;i<=Math.sqrt(x);i++){
if(x%i==0){//找到一个因子
while(x%i==0){
//把相同因子去除!
x/=i;
}
ret++;//次数加1
}
}
if(x!=1){//如果最后x不为 1 说明还有一个因数且是一个素数 !
ret++;
}
System.out.println(ret);
}
}
}
查看15道真题和解析