package showTwos;
import java.util.Scanner;
public class ShowTwos {
//用户输入一整数,将其进行质因数分解,并打印分解结果。
public static void main(String[] args) {
System.out.print("input an integer: ");
Scanner console = new Scanner(System.in);
int number = console.nextInt();
System.out.print(number + " = ");
showTwos(number);
}
public static void showTwos(int number)
{
int[] coefficient = new int[number];
int count = 0;
while(number != 1)
{
for(int i = 2;i <= number;i++)
if(number % i == 0)
{
coefficient[count] = i;
count++;
number = number / i;
}
}
//结果类似于:18 = 2 * 9;打印过程属于篱笆桩循环问题。
System.out.print(coefficient[0]);
for(int i = 1;i < count ;i++)
System.out.print(" * " + coefficient[i]);
}
}
给定一个整数进行质因数分解
最新推荐文章于 2024-01-15 09:42:10 发布