打印一个数的所有乘数组合,从大到小,不能有重复。
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/
相关推荐
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 ...
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 ...
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 ...
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"的压缩包文件很可能包含一个或多个程序,用于演示如何在不同的编程语言中计算一个数的平方。让我们深入探讨这个主题,详细了解一下计算数字平方的相关知识点。 1. **基本概念**: - ...
《A Byte of Python3》是一本面向初学者的Python编程指南,中文版使得更多的中文读者能够轻松地学习Python编程语言。这本书深入浅出地介绍了Python的基础知识,包括语法、数据结构、函数、模块、错误与异常处理、...
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...
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
printall(root,printoptions) 使用 root 作为输出文件名的根并通过添加数字编号形成实际名称,如果可用, 窗口标题(可能会混淆的字符) 文件系统被改变。 printall 将所有打开的数字发送到打印机所有选项都传递给 ...
打印RIP-APrint RIP POLA&Nano宏华喷绘打印软件 APrint RIP NANO 喷绘机软件是一种输出软件,它可以将图形、图像文件转换成各种大幅面打印机相应的打印语言,然后传给打印机输出。这个软件的核心是RIP(Raster ...
- pxwplan <sql_id> : get explain plan with work area information of a particular cursor and all connected cursor slave SQL - eplan <sql_id> []: get explain plan with execution statistics - pxeplan ...
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》是一本面向初学者的Python编程指南,它详尽地介绍了Python 3的基础知识和进阶特性。这本书不仅适合完全不懂编程的新手,也适合那些希望了解Python 3语言特性的专业人士。书中包含丰富的实例,...
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 ...
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)