比如,输入10,得到2*5,则要显示成
就和计算器显示的一样。
下面是代码,整体思路就是每个基本数字都有7个显示位置,全显示就是0,1就是最右边两个竖。
package com.baidu; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Calculator { private static int[] METRIX_0 = {1,1,1,0,1,1,1}; private static int[] METRIX_1 = {0,0,1,0,0,1,0}; private static int[] METRIX_2 = {1,0,1,1,1,0,1}; private static int[] METRIX_3 = {1,0,1,1,0,1,1}; private static int[] METRIX_4 = {0,1,1,1,0,1,0}; private static int[] METRIX_5 = {1,1,0,1,0,1,1}; private static int[] METRIX_6 = {1,1,0,1,1,1,1}; private static int[] METRIX_7 = {1,0,1,0,0,1,0}; private static int[] METRIX_8 = {1,1,1,1,1,1,1}; private static int[] METRIX_9 = {1,1,1,1,0,1,1}; private static String SPACE = " "; public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); while(in.hasNext()){ int num = in.nextInt(); Listlist = Factorize(num); print(list); } } public static void print(List list){ int size = list.size(); //7个位置分开打印 for(int i = 0; i < 7; i++){ for(int j = 0; j < size; j++){ printFactor(i,list.get(j)); // 位置3比较特殊, if( i == 3 ){ if(j < size - 1){ System.out.print("*"); }else{ System.out.print(SPACE); } }else{ System.out.print(SPACE); } } if(i == 1 || i == 4){ i++; } if(i == 0 || i == 2 || i == 3 || i == 5){ System.out.println(); } } System.out.println(); } /** * 把大于10的数按每位数组合成数组,比如13转换成【1,3】 * @param num * @return */ public static List rebuild(int num){ List array = new LinkedList (); while(num >= 10){ array.add(num % 10); num = num / 10; } array.add(num); List result = new ArrayList (); for(int i = array.size() -1;i>=0;i--){ result.add(array.get(i)); } return result; } /** * 打印因数 * @param pos * @param factor */ public static void printFactor(int pos,int factor){ if(factor >= 10){ List array = rebuild(factor); for(Integer ii : array){ printElem(pos,ii); //每个基本数字打印后,加一个空格 // System.out.print(SPACE); //因为位置1和位置2在同一行,位置4和位置5在同一行,所以位置1输出后,继续输出2. int k = pos; if(k == 1 || k == 4){ printElem(++k,ii); } } } else { printElem(pos,factor); int k = pos; if(k == 1 || k == 4){ printElem(++k,factor); } } } /** * 基本数字0到9的每一行打印 * @param pos * @param j */ public static void printElem(int pos,int j){ // 找到对应数字的矩阵 int[] arr = adapter(j); // 位置是0,3,6的时候,需要输出横线 - if(pos == 0 || pos == 3 || pos == 6){ // 为了美观,可以先输出一个空格 System.out.print(SPACE); if(arr[pos] == 1){ System.out.print("-"); }else{ System.out.print(SPACE); } // 为了美观,再补上一个空格 System.out.print(SPACE); } // 其余位置输出|线 else{ if(arr[pos] == 1){ System.out.print("|"); }else{ System.out.print(SPACE); } // 当位置是1和4的时候,输出1和2,4和5之间的空格 if(pos == 1 || pos == 4){ System.out.print(SPACE); } } } public static int[] adapter(int num){ switch(num){ case 0: return METRIX_0; case 1: return METRIX_1; case 2: return METRIX_2; case 3: return METRIX_3; case 4: return METRIX_4; case 5: return METRIX_5; case 6: return METRIX_6; case 7: return METRIX_7; case 8: return METRIX_8; case 9: return METRIX_9; default: return null; } } //因式分解 public static List Factorize(int n){ int key=1; int num=n; ArrayList list=new ArrayList (); while(num>1){ for(int i=2;i<=num;i++){ //从2开始除到本身,用于判断素数 if(num%i==0){ //找到素数因子 key=i; list.add(key); //保存这个素数因子 break; } } num=num/key; //继续分解除以素数因子得到的商 } return list; } }