题目1 遍历数组并求和
定义一个数组,存储1,2,3,4,5
遍历数组得到每一个元素,求数组里面所有的数据和
package com.example; public class Demo { public static void main(String[] args){ int[] arr={1,2,3,4,5}; int sum=0; System.out.print("数组中的元素为:"); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]+" "); sum+=arr[i]; } System.out.println(); System.out.println("数组中的数据和为:"+sum); } }题目2 统计个数
定义一个数组,存储1,2,3,4,5,6,7,8,9,10
遍历数组得到每一个元素,统计数组里一共有多少个能被3整除的数字
package com.example; import java.util.Arrays; public class Demo { public static void main(String[] args){ int[] arr={1,2,3,4,5,6,7,8,9,10}; int count=0; System.out.println("数组中的每个元素为:"+ Arrays.toString(arr)); System.out.print("数组中能被3整除的数字为:"); for (int i = 0; i < arr.length; i++) { if(arr[i]%3==0){ count++; System.out.print(arr[i]+" "); } } System.out.println(); System.out.println("数组里一共有"+count+"个能被3整除的数字"); } }题目3 变化数据
定义一个数组,存储1,2,3,4,5,6,7,8,9,10
遍历数组得到每一个元素
要求:
1.如果是奇数,则将当前数字扩大两倍
2.如果是偶数,则将当前数字变成1/2
package com.example; import java.util.Arrays; public class Demo { public static void main(String[] args){ int[] arr={1,2,3,4,5,6,7,8,9,10}; System.out.println("原数组:"+Arrays.toString(arr)); for (int i = 0; i < arr.length; i++) { if(arr[i]%2==0){ arr[i]/=2; }else{ arr[i]*=2; } } System.out.println("变化之后的数组:"+Arrays.toString(arr)); } }题目4 求最值
需求:已知数组元素为{33,5,22,44,55}
请找出数组中最大值并打印在控制台
注意:
max的默认值不能为0,一定要是数组中的值
为了提高效率,减少一次循环的次数,循环开始条件可以写1
package com.example; import java.util.Arrays; public class Demo { public static void main(String[] args){ int[] arr={33,5,22,44,55}; int max=arr[0]; System.out.println("数组"+Arrays.toString(arr)); for (int i = 1; i < arr.length; i++) { if(arr[i]>max){ max=arr[i]; } } System.out.println("数组中的最大值为:" + max); } }题目5 遍历数组求和
需求:生成10个1-100之间的随机数存入数组
(1)求出所有数据的和
(2)求所有数据的平均数
(3)比平均数小的数据有多少个
package com.example; import java.util.Random; public class Demo { public static void main(String[] args){ int[] arr=new int[10]; int sum=0; Random r=new Random(); System.out.print("数组:"); for (int i = 0; i < arr.length; i++) { arr[i]=r.nextInt(100)+1; System.out.print(arr[i] + " "); sum+=arr[i]; } System.out.println(); System.out.println("所有数据的和:" + sum); int avg=sum/arr.length; System.out.println("所有数据的平均数"+avg); System.out.print("比平均数小的数据:"); int count=0; for (int i = 0; i < arr.length; i++) { if(arr[i]<avg){ count++; System.out.print(arr[i]+" "); } } System.out.println(",一共有" + count + "个"); } }题目6 交换数组中的数据
需求:定义一个数组,存入1,2,3,4,5。按照要求交换索引对应的元素
交换前:1,2,3,4,5
交换后:5,2,3,4,1
package com.example; import java.util.Arrays; public class Demo { public static void main(String[] args){ int[] arr={1,2,3,4,5}; System.out.println("原数组:"+Arrays.toString(arr)); int temp=arr[0]; arr[0]=arr[4]; arr[4]=temp; System.out.println("交换后:"+Arrays.toString(arr)); } }题目变形
交换前:1,2,3,4,5
交换后:5,4,3,2,1
package com.example; import java.util.Arrays; public class Demo { public static void main(String[] args){ int[] arr={1,2,3,4,5}; System.out.println("原数组:"+Arrays.toString(arr)); for (int i = 0,j=arr.length-1; i<j; i++,j--) { int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } System.out.println("交换后:"+Arrays.toString(arr)); } }题目7 打乱数组中的数据
需求:定义一个数组,存入1-5,要求打乱数组中所有数据的顺序
package com.example; import java.util.Random; import java.util.Arrays; public class Demo { public static void main(String[] args){ int[] arr={1,2,3,4,5}; Random r=new Random(); for (int i = 0; i < arr.length; i++) { int randomIndex=r.nextInt(5); int temp=arr[i]; arr[i]=arr[randomIndex]; arr[randomIndex]=temp; } System.out.println(Arrays.toString(arr)); } }题目8 创建并打印矩阵
要求:
创建一个3行4列的int[][],其中每个元素赋值为:
a[i][j] = i * j;然后按矩阵形式打印。
示例输出:
0 0 0 0 0 1 2 3 0 2 4 6package com.example; public class Demo { public static void main(String[] args){ int[][] a=new int[3][4]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { a[i][j]=i*j; System.out.print(a[i][j] + " "); } System.out.println(); } } }题目9 不规则二维数组
要求:
创建一个不规则二维数组:
第 0 行长度 1 第 1 行长度 2 第 2 行长度 3 第 3 行长度 4然后依次填入1, 2, 3, ...,最后输出每一行以及总元素个数。
示例输出:
1 2 3 4 5 6 7 8 9 10 总元素个数:10package com.example; public class Demo { public static void main(String[] args){ int[][] arr=new int[4][]; for (int i = 0; i < arr.length; i++) { arr[i]=new int[i+1]; } int count=0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { arr[i][j]=++count; System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println("总元素个数:" + count); } }题目10 找最大值及其位置
要求:
给定一个二维数组,找出最大值,并输出它第一次出现的行和列。
示例:
int[][] a = { {3, 5, 1}, {7, 2, 9}, {4, 8, 6} };输出:
最大值:9 位置:第1行,第2列package com.example; public class Demo { public static void main(String[] args){ int[][] a={{3,5,1},{7,2,9},{4,8,6}}; int max=a[0][0]; int row=0; int col=0; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if(a[i][j]>max){ max=a[i][j]; row=i; col=j; } } } System.out.println("最大值:" + max); System.out.println("位置:第" + row + "行,第" + col + "列"); } }题目11 矩阵转置
要求:
给定一个m行n列的矩阵,输出它的转置矩阵。
转置后变成n行m列。
示例输入:
1 2 3 4 5 6示例输出:
1 4 2 5 3 6提示:因为行列数变了,所以需要创建一个新的二维数组来存放转置结果
package com.example; public class Demo { public static void main(String[] args){ int[][] arr={ {1,2,3}, {4,5,6} }; int row=arr[0].length; int col=arr.length; int[][] trans =new int[row][col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { trans[i][j]=arr[j][i]; System.out.print(trans[i][j] + " "); } System.out.println(); } } }题目12 矩阵相加
要求:
给定两个行数和列数都相同的二维数组,输出它们相加后的矩阵。
示例:
A: 1 2 3 4 B: 5 6 7 8输出:
6 8 10 12package com.example; public class Demo { public static void main(String[] args){ int[][] A={ {1,2}, {3,4} }; int[][] B={ {5,6}, {7,8} }; int[][] C=new int[2][2]; for (int i = 0; i < C.length; i++) { for (int j = 0; j < C[i].length; j++) { C[i][j]=A[i][j]+B[i][j]; System.out.print(C[i][j]+" "); } System.out.println(); } } }题目13 杨辉三角
要求:
输入一个整数n,输出n行杨辉三角。
示例:n = 5
输出:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1提示:
先定大致数组框架,每行首尾
之后每个数 = 它上面两个数之和
也就是:a[i][j] = a[i-1][j-1] + a[i-1][j]
package com.example; import java.util.Scanner; public class Demo { public static void main(String[] args){ Scanner s=new Scanner(System.in); System.out.print("请输入一个整数:"); int num=s.nextInt(); int[][] arr=new int[num][]; for (int i = 0; i < num; i++) { arr[i]=new int[i+1]; arr[i][0]=1; arr[i][i]=1; for(int j=1;j<i;j++){ arr[i][j]=arr[i-1][j-1]+arr[i-1][j]; } } for (int i = 0; i < num; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } }题目14 螺旋遍历矩阵
要求:
给定一个m x n矩阵,按顺时针螺旋顺序输出所有元素。
示例矩阵:
1 2 3 4 5 6 7 8 9输出:
1 2 3 6 9 8 7 4 5提示:
1.定义四个边界:top、bottom、left、right
2.依次从左到右、从上到下,从右到左,从下到上
3.第一、二步有 while 循环条件兜底,而且 for 循环本身会自动跳过空范围;第三、四步如果不加 if,会在矩阵已经遍历完时,重复打印已经打印过的元素。
package com.example; public class Demo { public static void main(String[] args){ int[][] arr={ {1,2,3}, {4,5,6}, {7,8,9} }; int top=0; int bottom=arr.length-1; int left=0; int right=arr[0].length-1; while (left<=right && top<=bottom) { //1.从左到右 for (int j = left; j <=right; j++){ System.out.print(arr[top][j] + " "); } top++; //2.从上到下 for(int i=top;i<=bottom;i++){ System.out.print(arr[i][right] + " "); } right--; //3.从右到左 if (top<=bottom) { for(int j=right;j>=left;j--){ System.out.print(arr[bottom][j] + " "); } bottom--; } //4.从下到上 if (left<=right) { for(int i=bottom;i>=top;i--){ System.out.print(arr[i][left] + " "); } left++; } } } }题目15 矩阵顺时针旋转90度
要求:
给定一个n x n的二维矩阵,原地顺时针旋转 90 度。
示例:
1 2 3 4 5 6 7 8 9旋转后:
7 4 1 8 5 2 9 6 3提示
分为两步:
1.先沿主对角线转置
2.再把每一行左右翻转
package com.example; public class Demo { public static void main(String[] args){ int[][] a ={ {1,2,3}, {4,5,6}, {7,8,9} }; int n=a.length; //1.先按对角线转置 for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { int temp= a[i][j]; a[i][j]= a[j][i]; a[j][i]=temp; } } //2.每一行左右翻转 for (int i = 0; i < n; i++) { for (int j = 0; j < n/2; j++) { int temp= a[i][j]; a[i][j]= a[i][n-1-j]; a[i][n-1-j]=temp; } } //3.打印输出 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } } }题目16 找鞍点
要求:
在二维数组中找一个“鞍点”。
鞍点定义:该元素在所在行中最大,在所在列中最小。
如果存在,输出所有鞍点;如果不存在,输出“无鞍点”。
示例:
int[][] a = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };输出:
鞍点:3,位置:第0行,第2列package com.example; public class Demo{ public static void main(String[] args) { int[][] a = { {1,2,3}, {4,5,6}, {7,8,9} }; boolean found = false; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { // 1. 检查 a[i][j] 是否是第 i 行最大 boolean isRowMax = true; for (int k = 0; k < a[i].length; k++) { if (a[i][k] > a[i][j]) { isRowMax = false; break; } } if (!isRowMax) { continue; } // 2. 检查 a[i][j] 是否是第 j 列最小 boolean isColMin = true; for (int k = 0; k < a.length; k++) { if (a[k][j] < a[i][j]) { isColMin = false; break; } } // 3. 两个条件都满足,就是鞍点 if (isColMin) { System.out.println("鞍点:" + a[i][j] + ",位置:第" + i + "行,第" + j + "列"); found = true; } } } if (!found) { System.out.println("无鞍点"); } } }题目17.搜索有序二维矩阵
要求:
给定一个二维矩阵,满足:
- 每一行从左到右递增;
- 每一列从上到下递增。
判断目标值target是否存在于矩阵中。
示例矩阵:
1 4 7 2 5 8 3 6 9输入:
target = 5;输出:
truepackage com.example; public class Demo{ public static boolean searchMatrix(int[][] matrix, int target) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { return false; } int row = 0; // 从第 0 行开始 int col = matrix[0].length - 1; // 从最后一列开始,即右上角 while (row < matrix.length && col >= 0) { int cur = matrix[row][col]; if (cur == target) { return true; } else if (cur > target) { col--; // 当前值太大,向左移动 } else { row++; // 当前值太小,向下移动 } } return false; } public static void main(String[] args) { int[][] matrix = { {1, 4, 7}, {2, 5, 8}, {3, 6, 9} }; System.out.println(searchMatrix(matrix, 5)); // true System.out.println(searchMatrix(matrix, 10)); // false } }