zoukankan      html  css  js  c++  java
  • Java实现 POJ 2749 分解因数(计蒜客)

    POJ 2749 分解因数(计蒜客)

    Description

    给出一个正整数a,要求分解成若干个正整数的乘积,即a = a1 * a2 * a3 * … * an,并且1 < a1 <= a2 <= a3 <= … <= an,问这样的分解的种数有多少。注意到a = a也是一种分解。

    Input

    第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数a (1 < a < 32768)

    Output

    n行,每行输出对应一个输入。输出应是一个正整数,指明满足要求的分解的种数

    Sample Input

    2
    2
    20
    

    Sample Output

    1
    4
     
    
    import java.util.Scanner;
    
    public class 分解因数 { 
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		for (int i = 0; i < n; i++) {
    			int num = sc.nextInt();
    			System.out.println(count(num,num));
    		}
    		
    	}
    					//a是被除数,b是因子
    public static int count(int a, int b) {
    	//当我a==1的时候,说明整除了
    	if(a == 1) 
    		return 1;
    	//b==1,证明下面没有方法可以走了
    	if(b == 1) 
    		return 0;
    	//当我可以除的时候我可以选择除,也可以选择继续找因子
    	if(a % b == 0){
    		
    		return count(a/b, b) + count(a, b-1); 
    	}
    	//当我不能除,我只能找因子
    	else
    		return count(a, b-1);
    	 
    }
    
    }
    
    
  • 相关阅读:
    【转】Oracle中的decode在mysql中的等价实现
    Perhaps you are running on a JRE rather than a JDK
    iisapp -a命令出现 :此脚本不能与WScript工作
    HDU 6070 线段树
    HDU 1853 MCMF
    Codeforces 7C 扩展欧几里得
    HDU 5675 智慧数
    Lucas 大组合数
    bzoj 2179 FFT
    POJ 1155 树形背包
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075379.html
Copyright © 2011-2022 走看看