寺明/

我令人担忧的算法水平

用Java打印一个杨辉三角,采用即时推导的办法,使用一个非常Confusing的算法。

/* Print Pascal Tringle
 * Author : Xyjerry
 * Date : 2022/4/24
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.lang.Math;
import java.math.MathContext;
public class PascalTrangle {
    public static void main(String[] args) throws IOException {
        System.out.println("lines:");
        InputStream in = System.in;
        InputStreamReader isr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(isr);
        int height = Integer.parseInt(br.readLine());
        System.out.println("n:");
        int jrank = Integer.parseInt(br.readLine());
        br.close();
        int [] result = new int[64];
        int [] troot = new int[jrank];
        int [] cexp = new int [2];
        int [] newexp = new int [0];
        int i;
        for (i=0;i<=jrank;i++){
            result[i]=1;
        }
        System.out.print("1\r\n");
        for (i=0;i< jrank;i++){
            troot[i]= (int) Math.pow(jrank+1,jrank-i-2);
            System.out.print("1");
            if (i != jrank-1){
                System.out.print("    ");
            }
            else{
                System.out.print("\r\n");
            }
        }
        troot [troot.length - 1]=0;
        cexp=troot;
        int currentfloor;
        currentfloor = 3;
        int expmax;
        int coe;
        int result_pointer;
        result_pointer = 1+jrank;
        String ctemp;
        while (currentfloor <= height) {
            ctemp = "";
            for (i = 0; i < cexp.length;i = i + 1){
                int j;
                for (j=0;j<=jrank - 1;j++){
                    newexp = (int[]) getNewArr(newexp , cexp.length * jrank);
                    newexp[jrank*i+j] = cexp[i] + troot[j];
                }
            }
            cexp = (int[]) getNewArr(cexp,newexp.length);
            cexp = newexp;
            expmax=0;
            for (i=0 ; i < newexp.length;i++){
                if(newexp[i] > expmax){
                    expmax = newexp[i];
                }
            }
            for (;expmax >= 0;expmax--){
                coe=0;
                for (i=0;i < newexp.length;i++){
                    if (newexp[i] == expmax){
                        coe++;
                    }
                }
                if (coe != 0){
                    result[result_pointer] = coe;
                    ctemp = ctemp + String.valueOf(coe) + "    ";
                    result_pointer++;
                }
                if (result_pointer == result.length){
                    result = (int[]) getNewArr(result,result.length*2);
                }
            }
            System.out.println(ctemp);
            currentfloor++;
        }
    }
    public static Object getNewArr(Object objArr, int newLength) {
        if (!objArr.getClass().isArray()) {
            return null;
        }
        Class componentType = objArr.getClass().getComponentType();
        Object newArray = Array.newInstance(componentType, newLength);
        System.arraycopy(objArr, 0, newArray, 0, Array.getLength(objArr));
        return newArray;
    }
}

jrank一般为2。

留下一条评论

暂无评论