正数数列与负数数列在N~M范围内的所有组合 可递归,也可穷举。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Vector;public class JustForTest { public void selectInArray(int[] source, Vector<String> vresult, String result, int startIndex, int cout) { if (cout == 1) { String s1 = result + "," + source[startIndex]; vresult.add(s1.substring(1)); return; } for (int i = startIndex + 1; i < source.length; i++) selectInArray(source, vresult, result + "," + source[startIndex], i, cout - 1); } public void saveCombine(Vector<String> vs1, Vector<String> vs2, ArrayList<String> list) { for (int i = 0; i < vs1.size(); i++) for (int j = 0; j < vs2.size(); j++) list.add(vs1.get(i) + "," + vs2.get(j)); } public void range(ArrayList<String> list, int n, int m) { for (int i = 0; i < list.size(); i++) { String[] temp = list.get(i).split(","); int sum = 0; for (int j = 0; j < temp.length; j++) { sum += Integer.valueOf(temp[j]); } if (sum >= n && sum <= m) { System.out.println(list.get(i)); } } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please input n: "); int n = Integer.parseInt(br.readLine()); System.out.println("Please input m: "); int m = Integer.parseInt(br.readLine()); JustForTest demo = new JustForTest(); int[][] arrSource = { { 1, 3, 5, 9 }, { -10, -7, -6 - 4, -2 } };// 2row // N // column ArrayList<String> list = new ArrayList<String>(); Vector<String>[] arrvs = new Vector[arrSource.length]; for (int i = 0; i < arrSource.length; i++) { arrvs[i] = new Vector<String>(); for (int startIndex = 0; startIndex < arrSource[i].length; startIndex++) for (int cout = 1; cout <= arrSource[i].length - startIndex; cout++) { String result = ""; demo.selectInArray(arrSource[i], arrvs[i], result, startIndex, cout); } } demo.saveCombine(arrvs[0], arrvs[1], list); demo.range(list, n, m); }} 不是很明白LZ的意思,给个列出n-m的所有整数的组合的例子import java.util.*;public class Test { public static void main(String[] args) throws Throwable { int n = 1; int m = 5; combine(n, m); } public static List<List<String>> combine(int n, int m) { //n-m的所有组合 int a = Math.min(n, m); int b = Math.max(n, m); int[] data = new int[b-a+1]; for (int i=0; i<data.length; i++) { data[i] = i+a; } int[] dig = new int[data.length]; dig[dig.length-1] = 1; List<List<String>> result = new ArrayList<List<String>>(); for (int i=0; i<data.length; i++) { result.add(new ArrayList<String>()); } int cnt = 0; StringBuilder buf = new StringBuilder(); while (dig[0] < 2) { cnt = 0; buf.delete(0, buf.length()); buf.append("("); for (int i=0; i<dig.length; i++) { if (dig[i] == 1) { cnt++; buf.append(data[i]).append(","); } } buf.delete(buf.length()-1, buf.length()); buf.append(")"); result.get(cnt-1).add(0, buf.toString()); dig[dig.length-1]++; for (int i=dig.length-1; i>0; i--) { if (dig[i] == 2) { dig[i-1]++; dig[i] = 0; } else { break; } } } for (int i=0; i<result.size(); i++) { System.out.printf("%d个数组合:\n", i+1); System.out.println(result.get(i)); } return result; }} 为什么一直提示错误? JTable右键菜单问题 关于日期周期选择的问题 问一个异常处理的问题 关于JScrollPane自动滚动. 谁能帮我写段加法啊!!!!!! 内部类继承问题 在线等,请帮帮忙! 请问:初学JAVA,需要把精力放在些什么地方? POI_Jar包中的Cell对象,取出来的值形式多样,例如 3800/3700/3600 或者 0/3800 或者 null 等等...怎样遍历呢? 如何反射忽略某一属性 新手送分题
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Vector;public class JustForTest {
public void selectInArray(int[] source, Vector<String> vresult,
String result, int startIndex, int cout) {
if (cout == 1) {
String s1 = result + "," + source[startIndex];
vresult.add(s1.substring(1));
return;
}
for (int i = startIndex + 1; i < source.length; i++)
selectInArray(source, vresult, result + "," + source[startIndex],
i, cout - 1);
} public void saveCombine(Vector<String> vs1, Vector<String> vs2,
ArrayList<String> list) {
for (int i = 0; i < vs1.size(); i++)
for (int j = 0; j < vs2.size(); j++)
list.add(vs1.get(i) + "," + vs2.get(j));
} public void range(ArrayList<String> list, int n, int m) {
for (int i = 0; i < list.size(); i++) {
String[] temp = list.get(i).split(",");
int sum = 0;
for (int j = 0; j < temp.length; j++) {
sum += Integer.valueOf(temp[j]);
}
if (sum >= n && sum <= m) {
System.out.println(list.get(i));
}
}
} public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please input n: ");
int n = Integer.parseInt(br.readLine());
System.out.println("Please input m: ");
int m = Integer.parseInt(br.readLine()); JustForTest demo = new JustForTest();
int[][] arrSource = { { 1, 3, 5, 9 }, { -10, -7, -6 - 4, -2 } };// 2row
// N
// column
ArrayList<String> list = new ArrayList<String>();
Vector<String>[] arrvs = new Vector[arrSource.length];
for (int i = 0; i < arrSource.length; i++) {
arrvs[i] = new Vector<String>();
for (int startIndex = 0; startIndex < arrSource[i].length; startIndex++)
for (int cout = 1; cout <= arrSource[i].length - startIndex; cout++) {
String result = "";
demo.selectInArray(arrSource[i], arrvs[i], result,
startIndex, cout);
}
}
demo.saveCombine(arrvs[0], arrvs[1], list);
demo.range(list, n, m);
}
}
给个列出n-m的所有整数的组合的例子
import java.util.*;
public class Test {
public static void main(String[] args) throws Throwable {
int n = 1;
int m = 5;
combine(n, m);
} public static List<List<String>> combine(int n, int m) { //n-m的所有组合
int a = Math.min(n, m);
int b = Math.max(n, m);
int[] data = new int[b-a+1];
for (int i=0; i<data.length; i++) {
data[i] = i+a;
} int[] dig = new int[data.length];
dig[dig.length-1] = 1; List<List<String>> result = new ArrayList<List<String>>();
for (int i=0; i<data.length; i++) {
result.add(new ArrayList<String>());
} int cnt = 0;
StringBuilder buf = new StringBuilder();
while (dig[0] < 2) {
cnt = 0;
buf.delete(0, buf.length());
buf.append("(");
for (int i=0; i<dig.length; i++) {
if (dig[i] == 1) {
cnt++;
buf.append(data[i]).append(",");
}
}
buf.delete(buf.length()-1, buf.length());
buf.append(")");
result.get(cnt-1).add(0, buf.toString()); dig[dig.length-1]++;
for (int i=dig.length-1; i>0; i--) {
if (dig[i] == 2) {
dig[i-1]++;
dig[i] = 0;
} else {
break;
}
}
} for (int i=0; i<result.size(); i++) {
System.out.printf("%d个数组合:\n", i+1);
System.out.println(result.get(i));
} return result;
}
}