import java.util.Scanner; //拆分整数(相加) public class Test { static int[]res=new int[100]; static int p=0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m =sc.nextInt(); resolve(m,1); } static void resolve(int m,int n){ if(m<=0){ for(int i=0;i<res.length;i++){ if(res[i]!=0) System.out.print(res[i]+" "); } System.out.println(); } for(int i=n;i<=m;i++){ res[p] = i; p++; resolve(m-i,i); p--; for(int j=p;j<res.length;j++) res[j]=0; } } }
测试结果:
参考:https://blog.csdn.net/summerxiachen/article/details/62421985
原文:https://www.cnblogs.com/blizi/p/9025339.html