50,544
社区成员
发帖
与我相关
我的任务
分享
public class practiceString {
public static void main(String [] arg ) {
getPrintNum(10,15);
}
static void get_helpListNum(LinkedList<Integer> temp,int sum) {
if(temp.size() == 1 || sum == 0) {
if(sum == 0) {
System.out.println(_helplist.toString());
return;
}
if(sum == temp.get(0)){
_helplist.add(sum);
System.out.println(_helplist.toString());
_helplist.remove(_helplist.size() -1);
}
} else {
int nRemove = temp.size() - 1;
LinkedList<Integer> temp2 = new LinkedList<Integer>();
temp2.addAll(temp);
LinkedList<Integer>temp3 = new LinkedList<Integer>();
int nRemoveNum = temp2.remove(nRemove);
temp3.addAll(temp2);
if(sum - nRemoveNum < 0) {
get_helpListNum(temp3,sum);
} else {
_helplist.add(nRemoveNum);
get_helpListNum(temp2,sum - nRemoveNum);
_helplist.remove(_helplist.size() - 1);
get_helpListNum(temp3,sum);
}
}
}
static void getPrintNum( int n, int sum) {
LinkedList<Integer> temp = new LinkedList<Integer>();
for(int i = 1; i < n + 1; ++i) {
temp.add(i);
}
get_helpListNum(temp,sum);
}
public static LinkedList<Integer> _helplist;
static {
_helplist = new LinkedList();
}
}
static void getPrintNum(int n, int sum) {
Stack<Integer> stack = new Stack<Integer>();
for (int i = 1; i <= n / 2; i++) {
int tempSum = sum;
stack.push(i);
tempSum = sum - i;
int popNum = i;
while (!stack.isEmpty()) {
popNum++;
if (tempSum > popNum * 2) {
stack.push(popNum);
tempSum -= popNum;
} else if (tempSum == popNum) {
stack.push(popNum);
System.out.println(stack.toString());
stack.pop();
popNum = stack.pop();
tempSum += popNum;
} else if (tempSum < popNum || popNum == 10) {
popNum = stack.pop();
tempSum += popNum;
}
}
}
}