首页 > 其他 > 详细

prints out all ways to multiply smaller integers

时间:2017-12-08 00:43:30      阅读:249      评论:0      收藏:0      [点我收藏+]
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

原文:http://www.cnblogs.com/apanda009/p/8001556.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!