作业帮 > 综合 > 作业

如何实现0,1,3这三个数组合成所有14位数字并将所有的结果输出?Java实现!例如:01301301301301,

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/10 23:15:53
如何实现0,1,3这三个数组合成所有14位数字并将所有的结果输出?Java实现!例如:01301301301301,
上面这一串数字只是一个例子!
递归,比循环会省很多.代码如下.数组与LEN可以自定义,
------------------------------------------------------------
public class demo {
public static void main(String[] args) {
\x05int len = 3;
\x05String[] array = { "0","1","3" };
\x05execute(array,len,"");
}
public static void execute(String[] array,int len,String str) {
\x05for (int i = 0; i < array.length; i++) {
\x05 str += array[i];
\x05 if (str.length() == len) {
\x05\x05System.out.println(str);
\x05 } else {
\x05\x05execute(array,len,str);
\x05 }
\x05 str = str.substring(0,str.length() - 1);
\x05}
}
}
public class demo {
public static void main(String[] args) {
\x05int len = 3;
\x05String[] array = { "0","1","3" };
\x05execute(array,len,"");
}
public static void execute(String[] array,int len,String str) {
\x05for (int i = 0; i < array.length; i++) {
\x05 str += array[i];
\x05 if (str.length() == len) {
\x05\x05System.out.println(str);
\x05 } else {
\x05\x05execute(array,len,str);
\x05 }
\x05 str = str.substring(0,str.length() - 1);
\x05}
}
}
再问: 那怎么对输出的结果集进行筛选?比如说限制3的个数?限制1的个数?或者连续出现3的个数?麻烦您了!!
再答: 连续 就是33 11这样的不行呗
再问: 可以,我就是想对输出的结果再做进一步的筛选和统计!!!给您添麻烦了!
再答: 我就是不知道你说的筛选条件是什么,你说具体点啊.
再问: 在输出结果中,a:筛选14位数中3的个数是大于x,小于的将其结果输出,b:筛选连续出现3的个数是大于x1小于y2的将其结果输出。谢谢
再答: a:筛选14位数中3的个数是大于x,小于的将其结果输出 b:筛选连续出现3的个数是大于x1小于y2的将其结果输出 下边加了两个方法。你自己看一下吧。 ------------------------------------------------------------------------------------- import java.util.regex.Matcher; import java.util.regex.Pattern; public class demo { public static void main(String[] args) { int len = 6; String[] array = { "0", "1", "3" }; execute(array, len, ""); } public static void execute(String[] array, int len, String str) { for (int i = 0; i < array.length; i++) { str += array[i]; if (str.length() == len) { if (check(str, "3", 3)) { System.out.println(str); } } else { execute(array, len, str); } str = str.substring(0, str.length() - 1); } } public static boolean check(String src, String checkStr, int x) { Pattern p = Pattern.compile(checkStr); Matcher m = p.matcher(src); int count = 0; while (m.find()) { String str = m.group(); if (!"".equals(str)) { count++; } } return count < x; } public static boolean check(String src, String checkStr, int x, int y) { Pattern p = Pattern.compile(checkStr); Matcher m = p.matcher(src); int count = 0; while (m.find()) { String str = m.group(); if (!"".equals(str)) { count++; } } return (count > x && count < y); } }