介绍:
一维数组,用来熟悉代码,主要考察二维数组:
-
二维数组存储行、列数据,遍历,输出结果
-
二维数组的旋转
-
二维数组数据的找规律。等等
二维数组问题,不难,但是比较繁琐。需要细心、耐心
一、一维数组
求数组中的最大值
二、二维数组
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();//行数
int m = scan.nextInt();//列数
int a [][] = new int [n][m];
//接收数据
for (int i =0 ; i <n ; i++){
for (int j = 0 ; j <m ;j++){
a[i][j] = scan.nextInt();
}
}
//关闭资源,减少浪费
scan.close();
//输出:n和m的先后先后顺序调换 :第一行变成第一列,第一列变成最后一行
for (int i=0 ; i < m; i++){
for (int j = n-1 ; j >=0 ; j--){
System.out.print(a[j][i]+ " ");
}
System.out.println();
}
这种题目一般会给出小段数据,寻找规律,创建对应的二维数组,通过索引查询某行某列的数据
步骤:
-
找出数据的规律
-
创建二维数组
-
通过索引获取数据
int[][]a=new int[40][];
int b=1;
for (int i = 0; i <40; i++) {
a[i]=new int[40-i];
for(int j=0;j <=i;j++){
if(i%2==0){
a[i-j][j]=b;
}else{
a[j][i-j]=b;
}
b+=1;
}
}
System.out.println(a[19][19]);
这段代码是使用 Java 语言创建了一个二维数组 a
,其中数组的行数为 40。在 a
数组的每一行中,列数逐渐减少,即第一行有 40 列,第二行有 39 列,以此类推,直到最后一行只有 1 列。
然后,通过两个嵌套的循环,将变量 b
的值依次赋给数组 a
的元素。在内层循环中,根据外层循环变量 i
的奇偶性,分别赋值给不同位置的元素:
- 如果
i
是偶数,则在当前行倒序赋值; - 如果
i
是奇数,则在当前行正序赋值。
最后,打印出 a[19][19]
的值。
根据这个逻辑,a[19][19]
的值应该是在第 20 行(索引为 19)的最后一个元素。因为这个循环是递增的,所以 b
的值也会递增。在这个特定的位置,b
的值应该是 (20*21)/2 = 210
,因为在前面的循环中,b
的值一直在递增,最终到达了 210。
因此,System.out.println(a[19][19]);
应该输出 210
,也可以用递归(参考蓝桥杯之排序和算法--递归部分)
package com.wl.myclocksource.utils;
public class SnakeMatrix {
public static void main(String[] args) {
int[][] matrix = new int[10][10];
int value = 1;
int row = 0;
int col = 0;
boolean upward = true;
for (int i = 0; i < 100; i++) {
matrix[row][col] = value;
value++;
if (upward) {
if (col == 9) {
row++;
upward = false;
} else if (row == 0) {
col++;
upward = false;
} else {
row--;
col++;
}
} else {
if (row == 9) {
col++;
upward = true;
} else if (col == 0) {
row++;
upward = true;
} else {
row++;
col--;
}
}
}
// 打印蛇形矩阵
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
}
在这段代码中,根据 upward
变量的值(表示当前填充的方向),以及当前位置的行号和列号,来决定下一步的填充方向。具体来说:
-
如果当前方向为向上填充 (
upward == true
),则根据当前位置的行号和列号的奇偶性来决定下一步的填充方向。如果列号已经到达矩阵的右边界 (col == 9
),则需要向下移动一行并改变填充方向;如果行号已经到达矩阵的上边界 (row == 0
),则需要向右移动一列并改变填充方向;否则,继续向上填充。 -
如果当前方向为向下填充 (
upward == false
),同样根据当前位置的行号和列号的奇偶性来决定下一步的填充方向。如果行号已经到达矩阵的下边界 (row == 9
),则需要向右移动一列并改变填充方向;如果列号已经到达矩阵的左边界 (col == 0
),则需要向下移动一行并改变填充方向;否则,继续向下填充。
通过这样的判断,可以确保在填充过程中根据当前位置的行号和列号的奇偶性来决定填充的方向,从而生成蛇形矩阵的形状。
2017年Java组c组第四题:承压计算
X 星球的高科技实验室中整齐地堆放着某批珍贵金属原料。
每块金属原料的外形、尺寸完全一致,但重量不同。 金属材料被严格地堆放成金字塔形。
7
5 8
7 8 8
9 2 7 2
8 1 4 9 1
8 1 8 8 4 1
7 9 6 1 4 5 4
5 6 5 5 6 9 5 6
5 5 4 7 9 3 5 5 1
7 5 7 9 7 4 7 3 3 1
4 6 4 5 5 8 8 3 2 4 3
1 1 3 3 1 6 6 5 5 4 4 2
9 9 9 2 1 9 1 9 2 9 5 7 9
4 3 3 7 7 9 3 6 1 3 8 8 3 7
3 6 8 1 5 3 9 5 8 3 8 1 8 3 3
8 3 2 3 3 5 5 8 5 4 2 8 6 7 6 9
8 1 8 1 8 4 6 2 2 1 7 9 4 2 3 3 4
2 8 4 2 2 9 9 2 8 3 4 9 6 3 9 4 6 9
7 9 7 4 9 7 6 6 2 8 9 4 1 8 1 7 2 1 6
9 2 8 6 4 2 7 9 5 4 1 2 5 1 7 3 9 8 3 3
5 2 1 6 7 9 3 2 8 9 5 5 6 6 6 2 1 8 7 9 9
6 7 1 8 8 7 5 3 6 5 4 7 3 4 6 7 8 1 3 2 7 4
2 2 6 3 5 3 4 9 2 4 5 7 6 6 3 2 7 2 4 8 5 5 4
7 4 4 5 8 3 3 8 1 8 6 3 2 1 6 2 6 4 6 3 8 2 9 6
1 2 4 1 3 3 5 3 4 9 6 3 8 6 5 9 1 5 3 2 6 8 8 5 3
2 2 7 9 3 3 2 8 6 9 8 4 4 9 5 8 2 6 3 4 8 4 9 3 8 8
7 7 7 9 7 5 2 7 9 2 5 1 9 2 6 5 3 9 3 5 7 3 5 4 2 8 9
7 7 6 6 8 7 5 5 8 2 4 7 7 4 7 2 6 9 2 1 8 2 9 8 5 7 3 6
5 9 4 5 5 7 5 5 6 3 5 3 9 5 8 9 5 4 1 2 6 1 4 3 5 3 2 4 1
X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X
其中的数字代表金属块的重量(计量单位较大)。最下一层的 XX 代表30台极高精度的电子秤。假设每块原料的重量都十分精确地平均落在下方的两个金属块上,最后,所有的金属块的重量都严格精确地平分落在最底层的电子秤上。电子秤的计量单位很小,所以显示的数字很大。工作人员发现,其中读数最小的电子秤的示数为:2086458231。请你推算出:读数最大的电子秤的示数为多少?
//又是一个明确了行和列的数据,因为换行符与空格的存在,现将数据放入字符串中,然后去除换行符与空格,然后放入二维数组中
import java.util.Scanner;
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
//将数据放入字符串中
String s=" 7 \r\n" +
" 5 8 \r\n" +
" 7 8 8 \r\n" +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+
" 5 9 4 5 5 7 5 5 6 3 5 3 9 5 8 9 5 4 1 2 6 1 4 3 5 3 2 4 1 \r\n"+
"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0";
String all1 = s.replaceAll("\r\n", "");//字符串去掉全部的换行
String all = all1.replaceAll(" ", "");//字符串去掉全部的空格
char[] cs = all.toCharArray();//字符串转成char数组
double[][] a=new double[30][];//创建double二维数组
int b=0;//char数组的下标
for (int i = 0; i < a.length; i++) {//将字符串的数据放入二维数组中
a[i]=new double[i+1];//设置数组每一行的列数
for (int j = 0; j < a[i].length; j++) {
a[i][j]=cs[b]-'0';//char类型减去'0',得到int类型的数据
b++;//cs数组下标自增
}
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if(i==0) {//第一行第一列位置的重量值为本身
a[i][j]=a[i][j];
}else if(j==0&&i>=1) {//第一列其他位置的重量值=本身重量+前一行同一列的重量/2
a[i][j]+=(a[i-1][j]/2.0);
}else if (j==i && j>=1) {//行列相同其他位置的重量值=本身重量+前一行前一列的重量/2
a[i][j]+=(a[i-1][j-1]/2.0);
}else{//剩余位置重量=本身重量+前一行前一列的重量/2+前一行同一列的重量/2
a[i][j]+=(a[i-1][j-1]/2.0+a[i-1][j]/2.0);
}
}
}
double min=a[29][0];//最小值的初始值为30行第一个元素
double max=a[29][0];//最大值的初始值为30行第一个元素
for (int i = 0; i <30; i++) {
if(min>a[29][i]) {
min=a[29][i];//重新赋值
}
if(max<a[29][i]) {//重新赋值
max=a[29][i];
}
}
//因为数据超出了int与long的范围,则使用BigDecimal对象获取超大数值
BigDecimal bd = new BigDecimal((2086458231/min)*max);
System.out.println(bd.toString());
}
}
小蓝有一个数字矩阵,里面只包含数字0和2。小蓝很喜欢2020,他想找 到这个数字矩阵中有多少个2020。小蓝只关注三种构成2020的方式:1.同一行里面连续四个字符从左到右构成 2020;2.在一条从左上到右下的斜线上连续四个字符;3.从左上到右下构成 2020。例如,对于下面的矩阵:....共有多少个2020?
//将数据放入file文件中,使用IO流的方式读取文件数据
public static void main(String[] args) throws Exception {
//1.字符流读取数据
FileReader in=new FileReader("C:\\Users\\Administrator\\Desktop\\a.txt");
//2.设置读取变量
int line=0;
//3.因为有换行符的存在,先将数据放入字符串中,创建字符串s
String s="";
//4.IO流读取数据
while ((line=in.read())!=-1) {
//读取的整数类型数据转成字符类型
char d=(char)line;
//只将0和2的数据放入字符串中
if(d=='0' || d=='2') {
s+=d;
}
}
in.close();
//5.创建字符串索引
int b=0;
//6.创建对应的二维数组
char[][] cs=new char[300][300];
//7.将字符串转成字符数组
char[] array = s.toCharArray();
//8.遍历二维数组,将字符数组数据放入二维数组中
for (int i = 0; i <300; i++) {
for (int j = 0; j <300; j++) {
cs[i][j]=array[b];
b++;
}
}
//设置2020的总个数变量
int count=0;
//1.纵向从上到下构成 2020
for (int i = 0; i <297; i++) {
for (int j = 0; j <300; j++) {
//因2020有4个字符组成,创建(i,j)到(i+3,j)字符的数组
char[] cs1= {cs[i][j],cs[i+1][j],cs[i+2][j],cs[i+3][j]};
//通过char数组创建字符串
String s1=new String(cs1);
//产生的字符串与”2020”进行比较,相等则总个数+1
if (s1.equals("2020")) {
count++;
}
}
}
//2.在一条从左上到右下的斜线上连续四个字符
for (int i = 0; i <297; i++) {
for (int j = 0; j <297; j++) {
//同1
char[] cs1= {cs[i][j],cs[i+1][j+1],cs[i+2][j+2],cs[i+3][j+3]};
String s1=new String(cs1);
if (s1.equals("2020")) {
count++;
}
}
}
//3.横向从左到右构成 2020
for (int i = 0; i <300; i++) {
for (int j = 0; j <297; j++) {
//同1
char[] cs1= {cs[i][j],cs[i][j+1],cs[i][j+2],cs[i][j+3]};
String s1=new String(cs1);
if (s1.equals("2020")) {
count++;
}
}
}
//输出总个数count
System.out.println(count);
}
仔细寻找,会发现:在下面的8×8 的方阵中,隐藏着字母序列:LANQIAO。
SLANQIAO
ZOEXCCGB
MOAYWKHI
BCCIPLJQ
SLANQIAO
RSFWFNYA
XIFZVWAL
COAIQNAL
我们约定: 序列可以水平,垂直,或者是斜向; 并且走向不限(实际上就是有一共 8 种方向)。 上图中一共有4个满足要求的串。下面有一个更大的(100×100)的字母方阵。 你能算出其中隐藏了多少个 LANQIAO 吗?
分析:
-
方向: 水平:从左到右、从右到左 垂直:从下到上,从上到下
-
斜向:从左下到右上,从右上到左下 、 从左上到右下,从右下到左上。
这种题目的数据量很大,让从中寻找出符合某些条件的数据。一般数据给你字符串或者给你文件。然后创建二维数组。使用循环讲数据存储道二维数组里面(注意调整格式)。然后通过遍历二维数组进行数据匹配。
步骤:
-
数据放入字符串或者文件中。
-
创建对应的二维数组。
-
使用循环或者IO流读取数据放入二维数组。
-
查询对应数据
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
String string="FOAIQNALWIKEGNICJWAOSXDHTHZPOLGYELORAUHOHCZIERPTOOJUITQJCFNIYYQHSBEABBQZPNGYQTCLSKZFCYWDGOAIADKLSNGJ\r\n" +
"GSOZTQKCCSDWGUWAUOZKNILGVNLMCLXQVBJENIHIVLRPVVXXFTHQUXUAVZZOFFJHYLMGTLANQIAOQQILCDCJERJASNCTLYGRMHGF\r\n" +
"TSDFYTLVIBHKLJVVJUDMKGJZGNNSTPVLCKTOFMUEUFSVQIAUVHNVFTGBDDARPKYNNCUOYUAZXQJNOEEYKLFRMOEMHUKJTPETHLES\r\n" +
"FKVINSLWEVGAGMKVFVIUBMYOIAFHLVNKNTYKTZWVXQWRWIGPENFXYDTKRVPKRTHMGHVYOCLDCKMEKRLGEKBYUCLOLYPAKPFSOREH\r\n" +
"KWPUOLOVMOFBIXYACWRDYBINTMPASPCEOKHXQIGBQQMCEOIVULIEOPFSCSIHENAJCVDPJDOIWIIULFDNOZOFVAMCABVGKAKCOZMG\r\n" +
"XWMYRTAFGFOCNHLBGNGOXPJSTWLZUNNAGIRETGXFWAQSSJPFTQAXMTQWMZWYVEPQERKSWTSC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ZCMUJFLVCLIOQQRVKSWFIAKNHMRLNJTKGVNTGLCVPVMBLJANOBCXUGVWBJYSIXZQVAVFWILWFB\r\n" +
"QWNLTPMCYHRSKVHXLONRANWKWXUTHYQLIOFKGDBMSWDRCYRKVSAGGRJMWQYQFLMUIGGCLAUQAACTYLPZEOJBHMWRKHCRXGTGRMUP\r\n" +
"CPQKJRBLYDNPUGHCRBVYBAIRVCAWLBWVWCMKNBIRKJOUGYQEBQRHDSTWXDIWGRVMLIJFBWHLHCDAAVUDLZSCGQNOUXVUIVIZZZMD\r\n" +
"NMHGYPFUUDWKQGTAKKGCDFJFYJFNRZVXDPGZEAMWQVQZODKTXHIYFVKJSSAWVHYCUCZMLLBPXTILDYJQEMWDRUFKISOUVPUDTYPB\r\n" +
"FDAQUBXHUJYTAYNWVIJNUSQDTQDEMUAPWXRYUWONTBDZCHZOUEGPMWEZTQWWSHAYOBWVTDIMZYNVNZKUHOFCQKPHJXWNRCGUJEKO\r\n" +
"WSDAUGUTVWCVHEMOIRJJGTANUWTSAIXXEVZTBDHPGSRHHVWCDZVZYRJTLONIJVXEATHQXOUKBIGZONFRSZIOGWNTYAJYLQCGEOWY";
String all = string.replaceAll("\r\n", "");//字符串去换行符
char[] cs = all.toCharArray();//字符串转成字符数组
int b=0;//字符数组的下标
char[][] array=new char[100][100];//创建100行100列二维数组
//遍历二维数组,将字符串数据放入二维数组中
for (int i = 0; i <100; i++) {
for (int j = 0; j <100; j++) {
array[i][j]=cs[b];//将字符数组的字符放入二维数组中
b++;//字符串下标自增
}
}
int count=0;//数据中LANQIAO总个数
//1.从左到右横向查找LANQIAO
for (int i = 0; i <100; i++) {
for (int j = 0; j <94; j++) {
//因为LANQIAO七个字符,创建(i,j)到(i,j+6)七个元素的char数组
char[]cd1= {array[i][j],array[i][j+1],array[i][j+2],array[i][j+3],array[i][j+4],array[i][j+5],array[i][j+6]};
//使用char数字创建字符串
String s1=new String(cd1);
//产生的字符串与”LANQIAO”进行匹配,相等则个数+1
if (s1.equals("LANQIAO")) {
count++;
}
}
}
//2.从右到左横向查找LANQIAO
for (int i = 0; i<100; i++) {
for (int j = 99; j>=6; j--) {
//因为LANQIAO七个字符,创建(i,j)到(i,j-6)七个元素的char数组
char[]cd1= {array[i][j],array[i][j-1],array[i][j-2],array[i][j-3],array[i][j-4],array[i][j-5],array[i][j-6]};
String s1=new String(cd1);
if (s1.equals("LANQIAO")) {
count++;
}
}
}
//3.从上到下纵向查询LIANQIAO
for (int i = 0; i <94; i++) {
for (int j = 0; j <100; j++) {
//因为LANQIAO七个字符,创建(i,j)到(i+6,j)七个元素的char数组
char[]cd2= {array[i][j],array[i+1][j],array[i+2][j],array[i+3][j],array[i+4][j],array[i+5][j],array[i+6][j]};
String s2=new String(cd2);
if (s2.equals("LANQIAO")) {
count++;
}
}
}
//4.从下到上纵向查询LIANQIAO
for (int i = 99; i>=6; i--) {
for (int j = 0; j <100; j++) {
//因为LANQIAO七个字符,创建(i,j)到(i-6,j)七个元素的char数组
char[]cd2= {array[i][j],array[i-1][j],array[i-2][j],array[i-3][j],array[i-4][j],array[i-5][j],array[i-6][j]};
String s2=new String(cd2);
if (s2.equals("LANQIAO")) {
count++;
}
}
}
//5.从左上到右下倾斜查询LIANQIAO
//因为LANQIAO七个字符,创建(i,j)到(i+6,j+6)七个元素的char数组
for (int i = 0; i <94; i++) {
for (int j = 0; j <94; j++) {
char[]cd3= {array[i][j],array[i+1][j+1],array[i+2][j+2],array[i+3][j+3],array[i+4][j+4],array[i+5][j+5],array[i+6][j+6]};
String s3=new String(cd3);
if (s3.equals("LANQIAO")) {
count++;
}
}
}
//6.从左下到右上倾斜查询LIANQIAO
for (int i = 99; i>=6; i--) {
for (int j = 0; j <94; j++) {
//因为LANQIAO七个字符,创建(i,j)到(i-6,j+6)七个元素的char数组
char[]cd3= {array[i][j],array[i-1][j+1],array[i-2][j+2],array[i-3][j+3],array[i-4][j+4],array[i-5][j+5],array[i-6][j+6]};
String s3=new String(cd3);
if (s3.equals("LANQIAO")) {
count++;
}
}
}
//7.从右上到左下倾斜查询LIANQIAO
for (int i = 0; i<94; i++) {
for (int j =99; j>=6; j--) {
//因为LANQIAO七个字符,创建(i,j)到(i+6,j-6)七个元素的char数组
char[]cd3= {array[i][j],array[i+1][j-1],array[i+2][j-2],array[i+3][j-3],array[i+4][j-4],array[i+5][j-5],array[i+6][j-6]};
String s3=new String(cd3);
if (s3.equals("LANQIAO")) {
count++;
}
}
}
//8.从右下到左上倾斜查询LIANQIAO
for (int i = 99; i>=6; i--) {
for (int j = 99; j>=6; j--) {
//因为LANQIAO七个字符,创建(i,j)到(i-6,j-6)七个元素的char数组
char[]cd1= {array[i][j],array[i-1][j-1],array[i-2][j-2],array[i-3][j-3],array[i-4][j-4],array[i-5][j-5],array[i-6][j-6]};
String s1=new String(cd1);
if (s1.equals("LANQIAO")) {
count++;
}
}
}
System.out.println(count);
}
}