码迷,mamicode.com
首页 > 其他好文 > 详细

蓝桥杯——说好的进阶之和式分解

时间:2014-05-20 15:41:37      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:java   蓝桥杯   正整数分解   和式分解   

对于正整数 n,输出和等于 n且组成和式的数字从左至右是非递增的所有正整数和式。

输入:

6

输出:

6=6
6=5+1
6=4+2
6=4+1+1
6=3+3
6=3+2+1
6=3+1+1+1
6=2+2+2
6=2+2+1+1
6=2+1+1+1+1
6=1+1+1+1+1+1

import java.util.Scanner;

public class Main{

	static int[] a = new int[1000];

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner=new Scanner(System.in);
		int n=Integer.parseInt(scanner.nextLine());
		a[0] = n;
		cal(n, 1);
	}

	static void cal(int n, int p) {
		for (int i = n < a[p - 1] ? n : a[p - 1]; i > 0; i--) {
			a[p] = i;
			if (n == i) {
				System.out.printf("%d=%d", a[0], a[1]);
				for (int j = 2; j <= p; j++) {
					System.out.printf("+%d", a[j]);
				}
				System.out.println();
			} else {
				cal(n - i, p + 1);
			}
		}
	}
}


蓝桥杯——说好的进阶之和式分解,布布扣,bubuko.com

蓝桥杯——说好的进阶之和式分解

标签:java   蓝桥杯   正整数分解   和式分解   

原文地址:http://blog.csdn.net/hymanxq/article/details/26207725

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!