作业
数字数码管
数字或字母能够用7位数码管显示(就是排列为8字形的7个数码管)
@@@@ 0
@ @ 1 2
@ @ 3
@@@@ 4 5
@ @ 6
@ @
@@@@
对于大型灯管,为了节约使用,在切换数字的时候,假设该灯管的状态没有改变,则不须要对该灯管关了再开。
已知一个数字变化的系列,求7个数码管开关的动作。
3:0,2,3,5,6
6:0,1,4,6,5,3
以下的代码不过两个数字变化后,第几个灯管变化,题目要求数字变化系列。
另一个逻辑问题:全灭到第一个数没有实现亮灯号。
import java.util.Arrays; import java.util.Scanner; /*数字数码管 数字或字母能够用7位数码管显示(就是排列为8字形的7个数码管) @@@@ 0 @ @ 1 2 @ @ 3 @@@@ 4 5 @ @ 6 @ @ @@@@ 对于大型灯管,为了节约使用,在切换数字的时候,假设该灯管的状态没有改变,则不须要对该灯管关了再开。 已知一个数字变化的系列,求7个数码管开关的动作。 3:0,2,3,5,6 6:0,1,4,6,5,3 */ public class NumberLight { public static void main(String[] args) { System.out.println("First Number:"); Scanner sc = new Scanner(System.in); int first = sc.nextInt(); System.out.println("Second Number:"); int second = sc.nextInt(); System.out.print("要反向的开关号为:"); for(int i=0;i<7;i++){ if(showNumber(second)[i]!=showNumber(first)[i]){//推断这两个数之间那几个位置不同 System.out.print(i+" ");//打印出不同的地方 } } System.out.println(); System.out.println(Arrays.toString(showNumber(first))); System.out.println(Arrays.toString(showNumber(second))); } //数字数码管显示方法,參数为要显示的数字 public static int[] showNumber(int a){ int []n = {0,0,0,0,0,0,0}; switch (a) { case 0: n[0]=1;n[1]=1;n[2]=1;n[4]=1;n[5]=1;n[6]=1; break; case 1: n[2]=1;n[5]=1; break; case 2: n[0]=1;n[2]=1;n[3]=1;n[4]=1;n[6]=1; break; case 3: n[0]=1;n[2]=1;n[3]=1;n[5]=1;n[6]=1; break; case 4: n[1]=1;n[2]=1;n[3]=1;n[5]=1; break; case 5: n[0]=1;n[1]=1;n[3]=1;n[5]=1;n[6]=1; break; case 6: n[0]=1;n[1]=1;n[3]=1;n[4]=1;n[5]=1;n[6]=1; break; case 7: n[0]=1;n[2]=1;n[5]=1; break; case 8: n[0]=1;n[1]=1;n[2]=1;n[3]=1;n[4]=1;n[5]=1;n[6]=1; break; case 9: n[0]=1;n[1]=1;n[2]=1;n[3]=1;n[5]=1;n[6]=1; break; default: break; } return n;//返回int数组 } }
First Number: 3 Second Number: 8 要反向的开关号为:1 4 [1, 0, 1, 1, 0, 1, 1] [1, 1, 1, 1, 1, 1, 1]
改进版,实现连续数字序列
package Homework; import java.util.Scanner; /*数字数码管 数字或字母能够用7位数码管显示(就是排列为8字形的7个数码管) @@@@ 0 @ @ 1 2 @ @ 3 @@@@ 4 5 @ @ 6 @ @ @@@@ 对于大型灯管,为了节约使用,在切换数字的时候,假设该灯管的状态没有改变,则不须要对该灯管关了再开。 已知一个数字变化的系列,求7个数码管开关的动作。 3:0,2,3,5,6 6:0,1,4,6,5,3 */ public class NumberLight { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入数字序列(用逗号分隔):"); String str = sc.nextLine(); str="-1,"+str;//一刀切上式,给初始补一个-1,在后面调用方法的时候进入switch的default,默认是全灭的。这里仅仅要写不是0到9的数字就可以。 String str2 [] = str.split(",");//被分隔后的字符串数组 System.out.println("变更数字时要反向的开关号为:"); //外层循环,对str2循环,比較后一个与前一个的差别 //内层循环,对每个数字里面的数码管号循环,推断是否有不同。 for(int i=0;i<str2.length-1;i++){ System.out.print(str2[i+1]+":"); for(int k=0;k<7;k++){ if(showNumber(Integer.valueOf(str2[i+1]))[k]!=showNumber(Integer.valueOf(str2[i]))[k]){ System.out.print(k+" "); } } System.out.println(); } } //数字数码管显示方法,參数为要显示的数字 public static int[] showNumber(int a){ int []n = {0,0,0,0,0,0,0}; switch (a) { case 0: n[0]=1;n[1]=1;n[2]=1;n[4]=1;n[5]=1;n[6]=1; break; case 1: n[2]=1;n[5]=1; break; case 2: n[0]=1;n[2]=1;n[3]=1;n[4]=1;n[6]=1; break; case 3: n[0]=1;n[2]=1;n[3]=1;n[5]=1;n[6]=1; break; case 4: n[1]=1;n[2]=1;n[3]=1;n[5]=1; break; case 5: n[0]=1;n[1]=1;n[3]=1;n[5]=1;n[6]=1; break; case 6: n[0]=1;n[1]=1;n[3]=1;n[4]=1;n[5]=1;n[6]=1; break; case 7: n[0]=1;n[2]=1;n[5]=1; break; case 8: n[0]=1;n[1]=1;n[2]=1;n[3]=1;n[4]=1;n[5]=1;n[6]=1; break; case 9: n[0]=1;n[1]=1;n[2]=1;n[3]=1;n[5]=1;n[6]=1; break; default: break; } return n;//返回int数组 } }
请输入数字序列(用逗号分隔): 8,0,0 变更数字时要反向的开关号为: 8:0 1 2 3 4 5 6 0:3 0:
隐藏password
password备忘扰乱法
我们的password假设非常长非常复杂,easy忘记。假设太简单,不安全。把password记录在本子上,更easy泄密!
有人想了这么个办法,把password嵌入一堆随机的数字中。
由于每一个人对password全然记住困难,但从一些线索中回顾出来就非常easy。
password:75383
3 5 6 4 7 2 8 6
5 4 7 2 7 0 7 4
1 6 5 9 5 8 0 3
1 6 7 0 3 6 8 9
3 6 4 7 8 0 9 4
3 4 6 9 3 6 8 9
2 1 3 6 7 8 1 3
2 7 3 9 4 6 3 5
嵌入时,能够横向或纵向。假设再复杂点,能够设计对角线。
/* * 隐藏password password备忘扰乱法 我们的password假设非常长非常复杂,easy忘记。假设太简单,不安全。把password记录在本子上,更easy泄密! 有人想了这么个办法,把password嵌入一堆随机的数字中。 由于每一个人对password全然记住困难,但从一些线索中回顾出来就非常easy。 password:75383 3 5 6 4 7 2 8 6 5 4 7 2 7 0 7 4 1 6 5 9 5 8 0 3 1 6 7 0 3 6 8 9 3 6 4 7 8 0 9 4 3 4 6 9 3 6 8 9 2 1 3 6 7 8 1 3 2 7 3 9 4 6 3 5 嵌入时,能够横向或纵向。假设再复杂点,能够设计对角线。 */ package Homework; import java.util.Scanner; public class HideCode { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入要隐藏的password:"); String code = sc.nextLine(); System.out.println("请输入矩阵宽:"); int width = sc.nextInt(); System.out.println("请输入矩阵高:"); int height = sc.nextInt(); Scanner sc2 = new Scanner(System.in); System.out.println("请输入方向(1表示横,2表示竖,3表示斜):"); String orientation = sc2.nextLine(); System.out.println("请输入起始位置x坐标:"); int x = sc2.nextInt(); System.out.println("请输入起始位置y坐标:"); int y = sc2.nextInt(); //首先生成一个随机矩阵 int[][]a = new int[height][width]; for(int i=0;i<height;i++){ for(int j=0;j<width;j++){ a[i][j] = (int)(10*Math.random()); } } //方向:横 if(orientation.equals("1")){ if(x>width||y+code.length()-1>height){ errorTips(); }else{ for(int i=0;i<code.length();i++){ a[x-1][y-1+i] = Integer.valueOf(String.valueOf(code.charAt(i))); } showMatrix(height,width,a); } } //方向:竖 if(orientation.equals("2")){ if(x+code.length()-1>width||y>height){ errorTips(); }else{ for(int i=0;i<code.length();i++){ a[x-1+i][y-1] = Integer.valueOf(String.valueOf(code.charAt(i))); } showMatrix(height,width,a); } } //方向:斜 if(orientation.equals("3")){ if(x+code.length()-1>width||y+code.length()-1>height){ errorTips(); }else{ for(int i=0;i<code.length();i++){ a[x-1+i][y-1+i] = Integer.valueOf(String.valueOf(code.charAt(i))); } showMatrix(height,width,a); } } } //尺寸不正确 public static void errorTips(){ System.out.println("请检查输入的起始位置或矩阵宽高。"); } //显示生成的矩阵 public static void showMatrix(int height,int width,int a[][]){ for(int i=0;i<height;i++){ for(int j=0;j<width;j++){ System.out.print(a[i][j]+" "); } System.out.println(); } } }
请输入要隐藏的password: 1234567890 请输入矩阵宽: 11 请输入矩阵高: 11 请输入方向(1表示横,2表示竖,3表示斜): 3 请输入起始位置x坐标: 1 请输入起始位置y坐标: 2 2 1 4 0 1 8 3 9 3 2 9 1 7 2 9 2 6 4 0 3 0 5 6 7 7 3 2 5 1 6 7 9 8 1 0 7 7 4 4 9 0 8 4 8 2 4 3 4 4 5 2 5 1 5 5 9 6 5 8 0 2 6 9 7 8 2 1 8 9 6 3 4 7 7 2 4 5 4 6 4 2 0 8 6 3 8 4 4 8 1 2 6 7 8 0 6 6 9 8 3 1 0 6 4 5 3 7 9 0 0 3 6 7 1 3 1 9 3 3 0 3