nyoj90 整数划分

来源:互联网 发布:js判断网址是否变化 编辑:程序博客网 时间:2024/06/11 13:37

整数划分

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
将正整数n表示成一系列正整数之和:n=n1+n2+…+nk, 
其中n1≥n2≥…≥nk≥1,k≥1。 
正整数n的这种表示称为正整数n的划分。求正整数n的不 
同划分个数。 
例如正整数6有如下11种不同的划分: 
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。 

输入
第一行是测试数据的数目M(1<=M<=10)。以下每行均包含一个整数n(1<=n<=10)。
输出
输出每组测试数据有多少种分法。
样例输入
16
样例输出
11
package nyoj90;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Scanner;public class Main {public static List<List<Integer>> consum(int[] candidate, int sum){List<List<Integer>> result = new ArrayList<List<Integer>>();Arrays.sort(candidate);ArrayList<Integer> list = new ArrayList<Integer>();compute(candidate, 0, sum, 0, list, result);return result;}private static void compute(int[] candidate, int start, int sum, int cur, List<Integer> list, List<List<Integer>> result){if(cur>sum)return;if(cur==sum){result.add(list);return;}for(int i=start;i<candidate.length;i++){List<Integer> temp = new ArrayList<Integer>(list);temp.add(candidate[i]);compute(candidate, i, sum, cur+candidate[i], temp, result);}}public static void main(String[] args) {Scanner reader = new Scanner(System.in);int N = reader.nextInt();while(N-->0){int n = reader.nextInt();int[] candidate = new int[n];for(int i=0;i<n;i++){candidate[i] = i+1;}List<List<Integer>> reault = consum(candidate, n);System.out.println(reault.size());}reader.close();}}


0 0