是质因数

蓝桥杯练习题

package c算法训练;

import java.util.Scanner;

public class 质因数 {
    
    
public static void main(String[] args) {
    
    

		Scanner sca = new Scanner(System.in);
		int n = sca.nextInt();
		int i = 2;
		String s = "";
		while (n != 1) {
    
    
			while (n%i == 0){
    
    
				n /= i;
				s += i+" ";
			}
			i++;
		}
		System.out.println(n+"="+s);
	}

}


/*Scanner sc=new Scanner(System.in);
	int a=sc.nextInt();
	zhiyinshu(a);
	
}
  //不是是质因数
  public static void zhiyinshu(int a) {
		for(int i=2;i<a;i++) {
			if(a%i==0) {
            System.out.print(i+" ");
            int num=a/i;
            
            if(isRaw(num)){
        	  System.out.print(num);
             }else {
        	   zhiyinshu(num);
           }
		}
	}
  }
  //是质因数
 public static boolean isRaw(int a) {
       boolean flag=true;
       for(int j=2;j<=Math.sqrt(a);j++) {
    	   if(a%j==0) {
    		   flag=false;
    	   }
       }
     
    	return true;
*/

猜你喜欢

转载自blog.csdn.net/weixin_45952706/article/details/114376407
今日推荐