prints out all ways to multiply smaller integers
Posted apanda009
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了prints out all ways to multiply smaller integers相关的知识,希望对你有一定的参考价值。
Write a program that takes an integer and prints out all ways to multiply smaller integers that equal the original number,
without repeating sets of factors. In other words, if your output contains 4 * 3,
you should not print out 3 * 4 again as that would be a repeating set. Note that this is not asking for prime factorization only.
Also, you can assume that the input integers are reasonable in size; correctness is more important than efficiency. Eg: PrintFactors(12) 12 * 1 6 * 2 4 * 3 3 * 2 * 2
public class PrintFactors { public static void main(String[] args) { printFactor(100); } private static void printFactor(int n) { if (n <= 0) { System.out.print("Wrong input!"); return; } if (n == 1) { System.out.print("1"); return; } long[] a = new long[n/2]; findFactor(n/2, n, a, 0); } private static void findFactor(long i, long n, long[] arr, int index) { if (i == 1) { if (n == 1) { for (int k = 0; k < index - 1; k++) { System.out.print(arr[k]); if (k < index - 2) { System.out.print("*"); } } System.out.println(); } return; } for (long k = i; k >= 1; k--) { if (n % k == 0) { arr[index] = k; findFactor(k, n / k, arr, index + 1); } } } }
以上是关于prints out all ways to multiply smaller integers的主要内容,如果未能解决你的问题,请参考以下文章
html 如何暂停所有Vimeo视频。资料来源:http://base42.nl/easiest-way-to-pause-all-vimeo-videos-on-your-site/
Different Ways to Add Parentheses
What is the best way to handle Invalid CSRF token found in the request when session times out in Spr
LeetCode-Different Ways to Add Parentheses
Try to buy one befor all the tickets _ A.will be sold B.are sold C.will be sold out D.are sold out