如,对于正整数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 import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class SerialNum { public SerialNum() { // TODO Auto-generated constructor stub } //思路:先把num作为一个基值,然后用输入的的数和他进行比较,求得的数存入headList中。 //由于headList的第一个段中,右边的数是不能大于左边的数的。所以对最右边的数先提出来-1,然后放回去。 public static String returnEveRow(int num){ int tempN = n; List<String> headList = new ArrayList<String>(); List<String> tailList = new ArrayList<String>(); //为headList进行初始化。 while(tempN > 0){ if (tempN > num) { headList.add(String.valueOf(num)); }else { headList.add(String.valueOf(tempN)); } tempN -= num; } //后面是对headList里的元素的处理。 String tempString = "" ; int last ; while (headList.size() > 1) { for (int i = 0; i < headList.size(); i++) { tempString += headList.get(i); tempString += "+"; } for (int i = 0; i < tailList.size(); i++) { tempString += tailList.get(i); tempString += "+"; } tempString += ", "; if (headList.get(0).equals("1")) { return tempString; }else { last = Integer.parseInt(headList.get(headList.size() - 1)); last--; headList.remove(headList.size() - 1); if (last != 0) { headList.add(String.valueOf(last)); } tailList.add("1"); } } return tempString; } static int n; public static void main(String args[]){ Scanner inScanner = new Scanner(System.in); n = inScanner.nextInt(); for (int i = n-1; i>0; i--) { System.out.println(returnEveRow(i)); System.out.println(); } } }