`

Print all unique combination of factors of a given number

阅读更多

打印一个数的所有乘数组合,从大到小,不能有重复。

Print all unique combinations of factors of a positive integer.

For example, if the given number is 24 then the output should be:

12*2
8*3
6*4
6*2*2
4*3*2
3*2*2*2

 

public List<String> getFactorNumberCombinations(int n) {
    List<String> result = new ArrayList<String>();
    generateFactors(n, "", n, result);
    return result;
}
    
private void generateFactors(int number, String factors, int prevFactor, List<String> result) {
	for (int i = number / 2; i >= 2; i--) {
		if (number % i == 0 && i <= prevFactor) {
			if (number / i <= i) {
				String entry = factors + i + "*" + number / i;
				result.add(entry);
			}
			generateFactors(number / i, factors + i + "*", i, result);
		}
	}
}

 

重构下代码:

public List<List<Integer>> factorCombinations(int n) {
    List<List<Integer>> res = new ArrayList<>();
    helper(res, n, n / 2, new ArrayList<Integer>());
    return res;
}

private void helper(List<List<Integer>> res, int num, int largestFactor,
        List<Integer> path) {
    if (num == 1) {
        res.add(new ArrayList<Integer>(path));
        return;
    }
    for (int i = largestFactor; i > 1; i--) {
        if (num % i == 0) {
            path.add(i);
            helper(res, num / i, i, path);
            path.remove(path.size() - 1);
        }
    }
}

 

参考:

http://www.shuatiblog.com/blog/2015/02/13/combination-of-factors/

分享到:
评论

相关推荐

    A.Collection.of.Bit.Programming.Interview.Questions.solved.in.C++

    Given an array of integers where all the numbers are appearing twice find the only number which appears once Chapter 11. Given an array of integers where all the numbers are appearing twice find the ...

    MicroStation Batch Print

    The MicroStation Batch Print command allows you to quickly print a large number of sheets across any number of files. This process looks at a selected set of files and, according to customizable ...

    B+树_b+tree_

    All the numbers in a line are separated by spaces.Output Specification:For each test case insert the keys into an initially empty B+ tree of order 3 according to the given order. Print in a line Key ...

    7-1 Build A Binary Search Tree.zip_13MV_6ST_Case File_Datastrctu

    Given the relations of all the activities of a project, you are supposed to find the earliest completion time of the project. Input Specification: Each input file contains one test case. Each case ...

    Square-of-A-Number.zip_number

    这个名为"Square-of-A-Number"的压缩包文件很可能包含一个或多个程序,用于演示如何在不同的编程语言中计算一个数的平方。让我们深入探讨这个主题,详细了解一下计算数字平方的相关知识点。 1. **基本概念**: - ...

    A Byte of Python3(中文版)

    《A Byte of Python3》是一本面向初学者的Python编程指南,中文版使得更多的中文读者能够轻松地学习Python编程语言。这本书深入浅出地介绍了Python的基础知识,包括语法、数据结构、函数、模块、错误与异常处理、...

    B04_2841_EX08.zip_2841_Number Ten

    As each number is read, print it only if it is not a duplicate of a number already read. Prepare for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve...

    gone fishing

    You will be given a number of cases in the input. Each case starts with a line containing n. This is followed by a line containing h. Next, there is a line of n integers specifying fi (1 ), then a ...

    ESLII_print12_The Elements of Statistical Learning.pdf

    ESLII_print12_The Elements of Statistical Learning.pdf

    printall(varargin):打印或导出所有未完成的图形-matlab开发

    printall(root,printoptions) 使用 root 作为输出文件名的根并通过添加数字编号形成实际名称,如果可用, 窗口标题(可能会混淆的字符) 文件系统被改变。 printall 将所有打开的数字发送到打印机所有选项都传递给 ...

    ora分析脚本

    - pxwplan &lt;sql_id&gt; : get explain plan with work area information of a particular cursor and all connected cursor slave SQL - eplan &lt;sql_id&gt; []: get explain plan with execution statistics - pxeplan ...

    pku 1063 Flip and Shift

    The number of test cases ) (T is given in the first line of the input file. Each of the next T lines gives a test case. A test case consists of an integer, representing the sum of m and n, and a ...

    A Byte of Python3 附源代码

    《A Byte of Python3》是一本面向初学者的Python编程指南,它详尽地介绍了Python 3的基础知识和进阶特性。这本书不仅适合完全不懂编程的新手,也适合那些希望了解Python 3语言特性的专业人士。书中包含丰富的实例,...

    An Imaginary Tale The Story Of I - Paul J Nahin

    he has also written a number of science fiction short stories. His style is far more lively and humane than a mathematics textbook while covering much of the same ground. Readers will end up with a ...

    AVL树_RootofAVLTree_

    All the numbers in a line are separated by a space.Output Specification:For each test case print the root of the resulting AVL tree in one line.Sample Input 1:588 70 61 96 120Sample Output 1:70Sample...

    The Elements of Statistical Learning(print12)

    The Elements of Statistical Learning(print12)The Elements of Statistical Learning(print12)

    the-print-preview-of-the-Web-control.rar_C web 打印_Print Previe

    本资源"the-print-preview-of-the-Web-control.rar"提供了一个基于VC++6.0的Web打印预览控件,使得开发者能够在自己的应用程序中集成这一功能。下面将详细阐述相关的知识点。 1. **VC++6.0**:VC++6.0是Microsoft ...

Global site tag (gtag.js) - Google Analytics