acmcoder计算器新功能--Java版

这题不难,挺麻烦的 ,还有点小坑

//package D201804;
/**
 * @坑都在这里了:
 * 数字1的两个‘|’在后一列
 * 乘号是5*1的矩阵
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by JesseWong
 * 2018/4/1
 */
public class Main {
    public static List<char[][]> digits = new ArrayList<>();

    public static void main(String[] args) {
        init();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            char[][] resultCharMatrix = new char[5][0];
            /**
             * get prime factor
             */
            int[] primeNumbers = new int[20];
            int len = 0;
            for (int i = 2; i <= n; i++) {
                if (n % i == 0) {
                    primeNumbers[len] = i;
                    len++;
                    n /= i;
                    i--;        //相同质因数
                }
            }

            /**
             *  convert prime factors to char matrix
             */
            for (int i = 0; i < len; i++) {
                int primeNum = primeNumbers[i];
                int itr = 6;
                int[] nums = new int[7];
                while (primeNum != 0) {
                    nums[itr--] = primeNum % 10;
                    primeNum /= 10;
                }
                itr++;
                for (; itr < 7; itr++) {
                    resultCharMatrix = joint(resultCharMatrix, digits.get(nums[itr]));
                }
                if (i != len - 1) {
                    resultCharMatrix = joint(resultCharMatrix, MULTYPLY);
                }
            }
            //print
            printCharMatrix(resultCharMatrix);
        }
//        printCharMatrix(joint(digits.get(0),digits.get(2)));
    }

    public static final char[][] ZERO = {
            {' ', '-', ' '},
            {'|', ' ', '|'},
            {' ', ' ', ' '},
            {'|', ' ', '|'},
            {' ', '-', ' '}};
    public static final char[][] ONE = {
            {' ', ' ', ' '},
            {' ', ' ', '|'},
            {' ', ' ', ' '},
            {' ', ' ', '|'},
            {' ', ' ', ' '}};
    public static final char[][] TWO = {
            {' ', '-', ' '},
            {' ', ' ', '|'},
            {' ', '-', ' '},
            {'|', ' ', ' '},
            {' ', '-', ' '}};
    public static final char[][] THREE = {
            {' ', '-', ' '},
            {' ', ' ', '|'},
            {' ', '-', ' '},
            {' ', ' ', '|'},
            {' ', '-', ' '}};
    public static final char[][] FOUR = {
            {' ', ' ', ' '},
            {'|', ' ', '|'},
            {' ', '-', ' '},
            {' ', ' ', '|'},
            {' ', ' ', ' '}};
    public static final char[][] FIVE = {
            {' ', '-', ' '},
            {'|', ' ', ' '},
            {' ', '-', ' '},
            {' ', ' ', '|'},
            {' ', '-', ' '}};
    public static final char[][] SIX = {
            {' ', '-', ' '},
            {'|', ' ', ' '},
            {' ', '-', ' '},
            {'|', ' ', '|'},
            {' ', '-', ' '}};
    public static final char[][] SEVEN = {
            {' ', '-', ' '},
            {' ', ' ', '|'},
            {' ', ' ', ' '},
            {' ', ' ', '|'},
            {' ', ' ', ' '}};
    public static final char[][] EIGHT = {
            {' ', '-', ' '},
            {'|', ' ', '|'},
            {' ', '-', ' '},
            {'|', ' ', '|'},
            {' ', '-', ' '}};
    public static final char[][] NINE = {
            {' ', '-', ' '},
            {'|', ' ', '|'},
            {' ', '-', ' '},
            {' ', ' ', '|'},
            {' ', '-', ' '}};
    public static final char[][] MULTYPLY = {
            {' '},
            {' '},
            {'*'},
            {' '},
            {' '}
    };

    public static void init() {
        digits.add(ZERO);
        digits.add(ONE);
        digits.add(TWO);
        digits.add(THREE);
        digits.add(FOUR);
        digits.add(FIVE);
        digits.add(SIX);
        digits.add(SEVEN);
        digits.add(EIGHT);
        digits.add(NINE);
        digits.add(MULTYPLY);
    }
//    public static int[] convertToSequence

    /**
     * Matching char matrix a with b
     *
     * @param a
     * @param b
     * @return a matches b
     */
    public static char[][] joint(char[][] a, char[][] b) {
        int horizonLenth = a[0].length + b[0].length;
        char[][] newMatrix = new char[5][];
        for (int i = 0; i < 5; i++) {
            newMatrix[i] = new char[horizonLenth];
            int j;
            for (j = 0; j < a[i].length; j++) {
                newMatrix[i][j] = a[i][j];
            }
            for (; j < horizonLenth; j++) {
                newMatrix[i][j] = b[i][j - a[i].length];
            }
        }
        return newMatrix;
    }

    /**
     * Print Char Matrix
     *
     * @param charMatrix
     */
    public static void printCharMatrix(char[][] charMatrix) {
        for (char[] chars : charMatrix) {
            String charString = "";
            for (char ch : chars) {
                charString += ch;
            }
            System.out.println(charString);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值