日期:2014-05-20  浏览次数:20860 次

考试系统中 生成试卷的一个算法问题 求教!!!
数据库中有试题表 字段有 试题编号 试题难度系数(分五个级别1-5)。。。。。。。。。。。。
现在我想写一个方法 来实现向一个试卷手动添加试题的功能 比如 我想向试卷添加 3道题 平均难度系数为3 通过这个方法我可以得出题库中 3道题 平均系数为3 的题号的所有组合 比如 第1 2 3三道题 对应难度系数分别为 1-3-5
或 第2 4 8 难度系数分别为2-3-4 这个方法 要求 题目数 和平均难度系数是参数 返回类型 就是能把符合要求的 题号组合 全部输出 请高手指点迷津。。。。。。。。。。。。


------解决方案--------------------
所有可能的结果遍历吧 ....
------解决方案--------------------
灿辉哥的?

Java code

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * 
 * @author cstur4
 *
 */
public class CSDN {
    
    private int total;
    private int difficult;
    private Set<String> res = new HashSet<String>();
    
    public CSDN(int total, int avg){
        
        this.total = total;
        difficult = total*avg;
    }
    
    public Set<String> getComposition(){
    
        int[] trace = new int[total+1];
        getNum(difficult, 1, trace);
        return res;
    }
    
    public void getNum(int difficult, int th, int[] trace){
    
        if(total == th){
            if(difficult<=5){
                trace[th] = difficult;
                Arrays.sort(trace);
                res.add(Arrays.toString(Arrays.copyOfRange(trace, 1, trace.length)));
            }
            return;        
        }
        
        for(int i=1;i<=Math.min(5, difficult-(total-th));i++)
        {
            int[] trace1 = new int[total+1];
            System.arraycopy(trace, 0, trace1, 0, trace.length);
            trace1[th] = i;
            getNum(difficult-i, th+1, trace1);
        }
    }
    
    public static void main(String[] args) {
        
        CSDN csdn = new CSDN(3, 3);
        Set<String> res = csdn.getComposition();
        Iterator<String> it = res.iterator();
        while(it.hasNext())
            System.out.println(it.next());
        
    }

    

    

}