package cn.kepu.question.chapter3;
/**
* 编程珠玑,第三章,第8题
* @author ffr@cnic.cn
*
* 输入:任意一个5位以内的正整数
*
* 输出:用七段数字表示出来
*
* 思路:如题目中展示的一样,0可以用{2,3,4,5,6,0}这几段显示来表示
* 数字定义:
*用二进制位来表示{1,1,1,1,1,1,1}分别对应{0,1,2,3,4,5,6},即用bitmap位映射来表示,0是{2,3,4,5,6,0}可以用{1,0,1,1,1,1,1}来表示,因为0没有1这个边,所以对应的1边为0,其余边为1:
*
* 0-->{2,3,4,5,6,0}-->{1,0,1,1,1,1,1}-->{0x5f}
* 1-->{4,6}-->{0,0,0,0,1,0,1}-->{0x5}
* 2-->{2,4,1,5,0}-->{1,1,1,0,1,1,0}-->{0x76}
* 3-->{2,4,1,6,0}-->{1,1,1,0,1,0,1}-->{0x75}
* 4-->{3,1,4,6}-->{0,1,0,1,1,0,1}-->{0x2d}
* 5-->{2,3,1,6,0}-->{1,0,1,1,0,0,1}-->{0x79}
* 6-->{2,3,1,5,6,0}-->{1,1,1,1,0,1,1}-->{0x7b}
* 7-->{2,4,6}-->{0,0,1,0,1,0,1}-->{0x15}
* 8-->{2,1,4,1,5,6,0}-->{1,1,1,1,1,1,1}-->{0x7f}
* 9-->{2,3,4,1,6}-->{0,1,1,1,1,0,1}-->{0x3d}
*
*char[] basic = {0x5f,0x5,0x76,0x75,0x2d,0x79,0x7b,0x15,0x7f,0x3d};
*
*边定义:
*
*因为计算机只能从上往下,从做往右打印(见最后面的附图),所以第零边为横(-),编号为2,对应{0,0,1,0,0,0,0},即
*
*第零边-->“-”-->{0,0,1,0,0,0,0}-->{0x10},意思是,数字二进制表示后的第2位(从0计数)为1,就需要显示第零边,例如,0的第二位为1,而1的第二位为0,说明0需要第零边,1不需要第零边
*第一边-->“|”-->{0,0,0,1,0,0,0}-->{0x8}
*第二边-->“|”-->{0,0,0,0,1,0,0}-->{0x4}
*第三边-->“-”-->{0,1,0,0,0,0,0}-->{0x20}
*第四边-->“|”-->{0,0,0,0,0,1,0}-->{0x2}
*第五边-->“|”-->{0,0,0,0,0,0,1}-->{0x1}
*第六边-->“-”-->{1,0,0,0,0,0,0}-->{0x40}
*
*char[] rep = {'-', '|', '|', '-', '|', '|', '-'};
*char[] edge = {0x10, 0x8, 0x4, 0x20, 0x2, 0x1, 0x40};
*
*程序实现
*/
public class No8 {
public static byte[] basic = {0x5f,0x5,0x76,0x75,0x2d,0x79,0x7b,0x15,0x7f,0x3d};
public static char[] rep = {'-', '|', '|', '-', '|', '|', '-'};
public static byte[] edge = {0x10, 0x8, 0x4, 0x20, 0x2, 0x1, 0x40};
public static void represent(int[] number){
for(int num : number){
System.out.println("=============输出数字【"+num+"】==================");
byte b = basic[num];
int j = 0;
for(byte e : edge){
//System.out.println("num: "+num+",b: "+b+",e:"+e);
if((b&e) == e){
if(j == 0 || j == 3 || j == 6){
System.out.print(" "+rep[j]);
}
if(j == 1 || j == 4){
System.out.print(rep[j]);
}
if(j == 2 || j == 5){
if(num == 0 ||(num==4&&j==2) || num==8 ||(num==9&&j==2) ||(num==6&&j==5)){
System.out.print(" "+rep[j]);
}else{
System.out.print(" "+rep[j]);
}
}
// 换行控制
if(j == 0 || j == 2 || j == 3 || j == 5 || j == 6){
System.out.println();
}
if(j == 4 && num == 2){
System.out.println();
}
if(j == 1 && num == 5){
System.out.println();
}
if(j == 1 && num == 6){
System.out.println();
}
}
j++;
}
}
}
public static void main(String[] args) {
int[] number = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println("输出 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}:");
represent(number);
}
}//输出结果:
/**
输出 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}:
=============输出数字【0】==================
-
| |
| |
-
=============输出数字【1】==================
|
|
=============输出数字【2】==================
-
|
-
|
-
=============输出数字【3】==================
-
|
-
|
-
=============输出数字【4】==================
| |
-
|
=============输出数字【5】==================
-
|
-
|
-
=============输出数字【6】==================
-
|
-
| |
-
=============输出数字【7】==================
-
|
|
=============输出数字【8】==================
-
| |
-
| |
-
=============输出数字【9】==================
-
| |
-
|
*/
需要说明的是,为了便于程序输出,重新定义了边的顺序,因为计算机只能从上往下输出: