作业帮 > 综合 > 作业

java语言实现满足多条件匹配简单过滤输出问题

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/06/11 13:45:52
java语言实现满足多条件匹配简单过滤输出问题
用java语言同时满足下列2个条件就输出源代码
(1)假定从1-11这11个数字中任选6个全组合输出(每行输出6个不相同数字,并且从小到大排列)
(2)将第一问得到数据过滤,并且同时满足下面3条条件就输出,输出结果用"116.txt"保存在C盘
01,02,03含有1至2个
02,06,08,09含有1至3个
01,06,07,08,09,10,11含有0至2个
正确输出结果如下
02,03,04,05,10,11
02,03,04,05,09,11
02,03,04,05,09,10
02,03,04,05,08,11
02,03,04,05,08,10
02,03,04,05,08,09
02,03,04,05,07,11
02,03,04,05,07,10
02,03,04,05,07,09
02,03,04,05,07,08
02,03,04,05,06,11
02,03,04,05,06,10
02,03,04,05,06,09
02,03,04,05,06,08
02,03,04,05,06,07
package zhidao;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;

public class Combin
{
private static String line = System.getProperty ("line.separator");

private static LinkedList<String[]> recursionSub ( LinkedList<String[]> list, int count, String[] array, int ind,
int start, int... indexs )
{
start++;
if (start > count - 1)
{
return null;
}
if (start == 0)
{
indexs = new int[array.length];
}
for ( indexs[start] = ind; indexs[start] < array.length; indexs[start]++ )
{
recursionSub (list, count, array, indexs[start] + 1, start, indexs);
if (start == count - 1)
{
String[] temp = new String[count];
for ( int i = count - 1; i >= 0; i-- )
{
temp[start - i] = array[indexs[start - i]];
}
boolean flag = true;
L: for ( int i = 0; i < temp.length; i++ )
{
for ( int j = i + 1; j < temp.length; j++ )
{
if (temp[i] == temp[j])
{
flag = false;
break L;
}
}
}
if (flag)
{
list.add (temp);
}
}
}
return list;
}

private static void filter ( LinkedList<String[]> list ) throws IOException
{
File file = new File ("c:/116.txt");
FileWriter fw = new FileWriter (file);
for ( String[] strings : list )
{
int count1 = 0, count2 = 0, count3 = 0;
String temp = Arrays.toString (strings).replaceAll ("[\\[\\]\\s]", "");
if (temp.contains ("01"))
{
count1++;
count3++;
}
if (temp.contains ("02"))
{
count1++;
count2++;
}
if (temp.contains ("03"))
{
count1++;
}
if (temp.contains ("06"))
{
count2++;
count3++;
}
if (temp.contains ("08"))
{
count2++;
count3++;
}
if (temp.contains ("09"))
{
count2++;
count3++;
}
if (temp.contains ("07"))
{
count3++;
}
if (temp.contains ("10"))
{
count3++;
}
if (temp.contains ("11"))
{
count3++;
}
if (count1 >= 1 && count1 <= 2 && count2 >= 1 && count2 <= 3 && count3 >= 0 && count3 <= 2)
{
fw.write (temp + line);
}
}
fw.flush ();
fw.close ();
}

public static void main ( String[] args ) throws IOException
{
String[] A = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11" };
LinkedList<String[]> list = new LinkedList<String[]> ();
recursionSub (list, 6, A, 0, -1);

// 假定从1-11这11个数字中任选6个全组合输出(每行输出6个不相同数字,并且从小到大排列)
for ( String[] strings : list )
{
String temp = Arrays.toString (strings).replaceAll ("[\\[\\]\\s]", "");
System.out.println (temp);
}
// 将第一问得到数据过滤,并且同时满足下面3条条件就输出,输出结果用"116.txt"保存在C盘
filter (list);
}
}
再问: 那个1-129行用什么输入的
再答: 什么?这是百度自己的富文本编辑器啊,你很感兴趣么?