2011JAVA本科蓝桥杯模拟(3)[整数的分划问题]

整数的分划问题。

如,对于正整数n=6,可以分划为:

6

5+1

4+2, 4+1+1

3+3, 3+2+1, 3+1+1+1

2+2+2, 2+2+1+1,2+1+1+1+1

1+1+1+1+1+1+1

现在的问题是,对于给定的正整数n,编写算法打印所有划分。

用户从键盘输入 n (范围1~10)

程序输出该整数的所有划分。

-----------------------------------------------------------------------------

ps:作者乃一神人

package java2011Yangti;

import java.util.Scanner;

public class M9 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		for (int i = n; i > 0; i--) {
			find("" + i, n - i, i);
			System.out.println("");
		}
	}

	public static void find(String Str, int last, int max) {
		if (last < 0)
			return;
		if (last == 0)
			System.out.print(Str + " ");
		for (int i = max; i > 0; i--) {
			find(Str + "+" + i, last - i, i);
		}
	}
}

链接出处: http://hi.baidu.com/hanna0913/item/fd079d0e76c98c80a2df43a7


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值