package com.lqb; import java.util.Scanner; public class Test { int[][] nums=null; private void init() { //生成二位数组 System.out.println("请输出数组的阶数"); Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); scanner.close(); //创建二维数组 nums =new int [n][n]; int m=1; for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { nums[i][j]=m++; System.out.print(nums[i][j]+"\t"); } System.out.println(); } print(); } private void print() { //设置起点 终点坐标 int x1=0,y1=0; int x2=nums.length-1,y2=nums.length-1; //默认从left==>right boolean l2r=true; while(x1<=x2&&y1<=y2) { //从left==>right if(l2r) { System.out.print(nums[x1][y1]+"\t"); if(x1==0&&y1<y2) { //第一行 没有到最后的y 平移变向 y1++; l2r=!l2r; }else if (y1==y2) { //在第一行 没有到最后一Y 平移变向 x1++; l2r=!l2r; }else { //上 x1--; y1++; } }else { System.out.print(nums[x1][y1]+"\t"); //从right到left if(y1==0&&x1<=x2) { x1++; l2r=!l2r; } else if (x1==x2) { y1++; l2r=!l2r; }else { x1++; y1--; } } } } public static void main(String[] args) { // TODO Auto-generated method stub new Test().init(); } }
package com.lqb; import java.util.Scanner; public class Test1 { private int[][] nums; /** * 2022/38 */ /** * * * @param args */ private void init() { System.out.println("请输入二维数组"); //创建Scanner对象 Scanner scanner =new Scanner(System.in); int n=scanner.nextInt(); //创结二维数组 nums =new int[n][n]; int num=0; for (int i = 0; i <nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { nums[i][j]=++num; System.out.print(nums[i][j]+"\t"); } System.out.println(); } print(); } private void print() { System.out.println("输出结果为"); //定义二个点的(X,Y) 起点(x1,y1) 到 终点(x2,y2) //找到当前数组的范围 int x1=0,y1=0,x2=nums.length-1,y2=nums[0].length-1; //控制层数 while(x1<=x2&&y1<=y2) { //定义移动点的x y int x=x1,y=y1; //向右移动 x不动 y++ while(y<=y2) { System.out.print(nums[x][y++]+"\t"); } //数值向下移动y不变 x加加 y=y2; x++; while(x<=x2) { System.out.print(nums[x++][y]+"\t"); } //数值向left移动 x不变 y在-- x=x2; y--; while(y>=y1) { System.out.print(nums[x][y--]+"\t"); } y=y1; x--; //数值向上移动 y不变x-- while(x>x1) { System.out.print(nums[x--][y]+"\t"); } //逐渐缩小范围 x1++;y1++;x2--;y2--; /* * //将范围缩小 */ } } public static void main(String[] args) { // TODO Auto-generated method stub new Test1().init(); } }
package com.lqb; import java.util.Scanner; /** * * @author MZFAITHDREAM * */ public class Test2 { private int[][] nums; private void init() { System.out.println("输入二维数组的阶数"); Scanner scanner =new Scanner(System.in); int n=scanner.nextInt(); nums =new int[n][n]; int num=0; for (int i = 0; i <nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { nums[i][j]=++num; System.out.print(nums[i][j]+"\t"); } System.out.println(); } print(); } private void print() { // TODO Auto-generated method stub System.out.println("输出结果为"); int x1=0,y1=0,x2=nums.length-1,y2=nums.length-1; while(x1<=x2&&y1<=y2) { //定义坐标 向下 int x=x1,y=y1; //在同一 while(x<=x2){ System.out.print(nums[x++][y]+"\t"); } //righ x=x2; y++; while(y<=y2){ System.out.print(nums[x][y++]+"\t"); } //向上 y=y2; x--; while(x>x1){ System.out.print(nums[x--][y]+"\t"); } x=x1; y--; //向left while(y>y1){ System.out.print(nums[x][y--]+"\t"); } x1++; y1++;x2--;y2--; } } public static void main(String[] args) { new Test2().init(); } }
package com.lqb; /** *第四章的第二题 *2022/3/9 */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class Test3 { /** * * @param args */ int[][] nums=null; private void init() { System.out.println("输入数组的阶数:"); Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); scanner.close(); nums=new int[n][n]; for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { //随机性产生0 nums[i][j]=Math.random()>0.1?new Random().nextInt(9)+1:0; System.out.print(nums[i][j]+"\t"); } System.out.println(); } print(); } private void print() { System.out.println("结果:"); //创建两个一维数组,分别用于记录为0的横纵坐标 int[] row=new int[nums.length]; int[] col=new int[nums[0].length]; //循环遍历所有的点 for (int i = 0; i < nums.length; i++ ) { for (int j = 0; j < nums[i].length; j++) { if (nums[i][j]==0) { row[i]=1; col[j]=1; } } } System.out.println(Arrays.toString(row)); System.out.println(Arrays.toString(col)); System.out.println("\n\n"); //判断两个一维数组中记录的下标的位置所在的整行和整列清零 for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { if (row[i]>0||col[j]>0) { nums[i][j]=0; } } } for (int i = 0; i < nums.length; i++) { System.out.println(Arrays.toString(nums[i])); } } public static void main(String[] args) { new Test3().init(); } }
package com.lqb; import java.util.Random; import java.util.Scanner; public class Test4 { /** * * @param args */ int[][] nums=null; private void init() { System.out.println("输入数组的阶数:"); Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); scanner.close(); nums=new int[n][n]; for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { //随机性产生0 nums[i][j]=Math.random()>0.1?new Random().nextInt(9)+1:0; System.out.print(nums[i][j]+"\t"); } System.out.println(); } print(); } private void print() { System.out.println("结果:"); //创建两个一维数组,分别用于记录为0的横纵坐标 int[] row=new int[nums.length]; int[] col=new int[nums[0].length]; //循环遍历所有的点 for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { if (nums[i][j]==0) { row[i]=1; col[j]=1; } } } //判断两个一维数组中记录的下标的位置所在的整行和整列清零 for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { if (row[i]==1||col[j]==1) { nums[i][j]=0; } System.out.print(nums[i][j]+"\t"); } System.out.println(); } } public static void main(String[] args) { new Test4().init(); } }
package com.lqb; import java.util.Random; import java.util.Scanner; public class Test5 { /** * * @param args */ int[][] nums=null; //创建临时的二维数组 int[] [] temp=null; private void init() { System.out.println("输入数组的阶数:"); Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); scanner.close(); //定义两个二维数组 nums=new int[n][n]; temp=new int[n][n]; //for循环遍历二位数组 for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { //随机性产生 用Random nums[i][j]=Math.random()>0.2?new Random().nextInt(5)+1:0; System.out.print(nums[i][j]+"\t"); } System.out.println(); } print(); } private void print() { System.out.println("结果:"); //创建两个一维数组,分别用于记录为0的横纵坐标 //x y int[] row=new int[nums.length]; int[] col=new int[nums[0].length]; //循环遍历所有的点 x y for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { //判断了 if (nums[i][j]==0) { //将行y 标记一维数组标记唯一 row[i]=1; col[j]=1; } } } //返找 for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { //如果两格一维数组中 X坐标 Y坐标 if (row[i]!=1&&col[j]!=1) { //该数字X y都 不为0 int sum=0; // 累加横坐标的和 for (int k = 0; k < nums.length; k++) { sum+=nums[i][k]; } //累加Y坐标的和为 for (int k = 0; k < nums.length; k++) { sum+=nums[k][j]; } sum-=nums[i][j]; temp[i][j]=sum; } } } //for for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { if(temp[i][j]>0) { nums[i][j]=temp[i][j]; } System.out.print(nums[i][j]+"\t"); //随机性产生0 /*nums[i][j]=Math.random()>0.1?new Random().nextInt(5)+1:0;*/ System.out.print(nums[i][j]+"\t"); } System.out.println(); } } public static void main(String[] args) { new Test5().init(); } }
package com.lqb; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Scanner; public class Test6 { /** * * @param args */ int[][] nums=null; //创建临时的二维数组 List<Map<String, Integer>> olist =new ArrayList<>(); /*int[][] temp=null;*/ private void init() { System.out.println("输入数组的阶数:"); Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); scanner.close(); //定义两个二维数组 nums=new int[n][n]; /* * temp=new int[n][n]; */ //for循环遍历二位数组 for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { //随机性产生 用Random nums[i][j]=Math.random()>0.2?new Random().nextInt(5)+1:0; System.out.print(nums[i][j]+"\t"); } System.out.println(); } print(); } private void print() { System.out.println("结果:"); //创建两个一维数组,分别用于记录为0的横纵坐标 //x y int[] row=new int[nums.length]; int[] col=new int[nums[0].length]; //循环遍历所有的点 x y for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { //判断了 if (nums[i][j]==0) { //将行y 标记一维数组标记唯一 row[i]=1; col[j]=1; } } } //返找 for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { //如果两格一维数组中 X坐标 Y坐标 if (row[i]!=1&&col[j]!=1) { //该数字X y都 不为0 int sum=0; // 累加横坐标的和 for (int k = 0; k < nums.length; k++) { sum+=nums[i][k]; } //累加Y坐标的和为 for (int k = 0; k < nums.length; k++) { sum+=nums[k][j]; } sum-=nums[i][j]; /* temp[i][j]=sum; */ } } } //for for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums[i].length; j++) { /* * if(temp[i][j]>0) { nums[i][j]=temp[i][j]; } */ System.out.print(nums[i][j]+"\t"); //随机性产生0 /*nums[i][j]=Math.random()>0.1?new Random().nextInt(5)+1:0;*/ System.out.print(nums[i][j]+"\t"); } System.out.println(); } } public static void main(String[] args) { new Test6().init(); } }