Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何显示所有可能的“组合”;削减;在Java程序中?_Java_Arrays_Algorithm_Function - Fatal编程技术网

我如何显示所有可能的“组合”;削减;在Java程序中?

我如何显示所有可能的“组合”;削减;在Java程序中?,java,arrays,algorithm,function,Java,Arrays,Algorithm,Function,我要做的是解决一个关于切割钢筋的问题,用户会被问到他有多少钢筋和他们的长度,他想做多少切割和他们的长度,然后程序会计算钢筋的剩余量和所有可能的切割组合,在用户知道组合后,应根据钢筋的剩余部分给出切割的“排序” 例如: Number of bars: 3 Lenght of bar 1: 10 Length of bar 2: 20 Length of bar 3: 30 Number of cuts: 4 Length of cut 1: 5 Length of cut 2: 5 Length

我要做的是解决一个关于切割钢筋的问题,用户会被问到他有多少钢筋和他们的长度,他想做多少切割和他们的长度,然后程序会计算钢筋的剩余量和所有可能的切割组合,在用户知道组合后,应根据钢筋的剩余部分给出切割的“排序”

例如:

Number of bars: 3
Lenght of bar 1: 10
Length of bar 2: 20
Length of bar 3: 30

Number of cuts: 4
Length of cut 1: 5
Length of cut 2: 5
Length of cut 3: 15
Length of cut 4: 20
因此,可能的切割组合为:

Combination 1:   
 Cuts in bar 1: 5, 5 Residue bar 1: 0
 Cuts in bar 2: 20 Residue bar 2: 0
 Cuts in bar 3: 15 Residue bar 3: 15

Combination 2: 
Cuts in bar 1: 5, 5 Residue: 0
Cuts in bar 2: 15   Residue: 5
Cuts in bar 3: 20   Residue: 10

Combination 3: 
Cuts in bar 1: 0 Residue: 10
Cuts in bar 2: 5, 15 Residue: 0
Cuts in bar 3: 5, 20 Residue: 5

Combination 4: 
Cuts in bar 1: 0 Residue: 10
Cuts in bar 2: 15 Residue: 5
Cuts in bar 3: 20, 5, 5 Residue: 0
到目前为止,我有这段代码,但在显示组合时有一些错误:

    public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
    int s, n=0, cont=1, m, i, j, q, a, z;

    System.out.print("Number of bars to be cut : ");
    s = scan.nextInt();
    int b[] = new int[s];

    do{
        System.out.print("Bar's length " + cont + ": ");
        b[n] = scan.nextInt();
        n++;
        cont++;
    }while (n<s);

    System.out.print("Number of pieces to be cut: ");
        m = scan.nextInt();
        int p[] = new int[m]; 

        n=0;
        cont=1;

        do{
            System.out.print("Length of the piece " + cont + ": ");
            p[n] = scan.nextInt();
            n++;
            cont++;
        }while (n<m);

        int bars[][] = new int[s][s];
        int pieces[][] = new int[m][m];

        for (n=0; n<s; n++){
            q=n;
            for (cont=0; cont<s; cont++){
                bars[n][q] = b[cont];
                q++;
                if (q>=s){
                    q = q - s;                    
                }
            }
        }


        for (n=0; n<m; n++){
            q=n;
            for (cont=0; cont<m; cont++){
                pieces[n][q] = p[cont];
                q++;
                if (q>=m){
                    q = q - m;                    
                }
            }
        }

        int auxb[][] = new int[s][s];
        int auxp[][] = new int[m][m];
        int cpb[][]= new int [100][100];

        for (i=0; i<s; i++){
            for (j=0; j<m; j++){

                for(q=0; q<s; q++){
                    auxb[i][q] = bars[i][q];
                }

                for(q=0; q<m; q++){
                    auxp[j][q] = pieces[j][q];
                }

                q=0;
                for (n=0;n<s;n++){
                    for (cont=0;cont<m;cont++){
                        if (bars[i][n]>=pieces[j][cont] && pieces[j][cont]!=0){
                            bars[i][n] = (bars[i][n] - pieces[j][cont]);
                            cpb[n][q]=pieces[j][cont];
                            pieces[j][cont]=0;
                            q++;
                            }
                        }
                    }

                q=0;
                while(q<m && pieces[j][q]==0){
                    q++;
                }

                if (q==m){
                    a=1;
                    z=0;
                    System.out.print("Probability " + i + "," + j + " \n");
                    for (q=0; q<s; q++){
                        System.out.print("Residue of bar " + a + " es: " + bars[i][q] + ", with cuts of ");
                        while (cpb[q][z]!=0){
                            System.out.print(cpb[q][z] + ", ");
                            z++;
                        }
                        System.out.print("\n");
                        a++;
                        }
                    System.out.print("\n");
                }

                else
                    System.out.print("It's not possible to make those cuts \n");

                for(q=0; q<s; q++){
                    bars[i][q] = auxb[i][q];
                }

                for(q=0; q<m; q++){
                    pieces[j][q] = auxp[j][q];
                }

            }
        }

    }
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int s,n=0,cont=1,m,i,j,q,a,z;
系统输出打印(“要切割的钢筋数量:”;
s=scan.nextInt();
int b[]=新的int[s];
做{
系统输出打印(“条的长度”+cont+“:”);
b[n]=scan.nextInt();
n++;
cont++;

}当(n你能指出一些由代码产生的错误吗?这需要阅读大量的代码,也需要花费大量的精力去理解。我总是发现阅读多维数组是一件噩梦般的事情,就像上百行一样。它可能有助于将代码分成几个函数,使用更具描述性的变量名在这里和那里添加一条关于发生了什么的评论。不仅你更有可能得到帮助,你还需要更少的帮助,因为这样会更容易发现错误。查看“箱子包装问题”。