第4章 数组
笔记
一维数组
数组的定义
int[] a = new int[10];
float[] f = new float[33];
double[] d = new double[123];
char[] c = new char[21];
数组的初始化
int[] a = {0, 1, 2}; // 含有3个元素的数组,元素分别是0, 1, 2
int[] b = new int[3]; // 含有3个元素的数组,元素的值均为0
char[] d = {'a', 'b', 'c'}; // 字符数组的初始化
访问数组元素
int[] a = {0, 1, 2}; // 数组下标从0开始
System.out.printf("%d %d %d\n", a[0], a[1], a[2]); // 读取
a[0] = 5; // 写入
多维数组
多维数组就是数组的数组。
// 二维数组
int[][] a = new int[3][4]; // 大小为3的数组,每个元素是含有4个整数的数组
// 三维数组
int[][][] b = new int[10][20][30];
// 大小为10的数组,它的每个元素是含有20个数组的数组
// 这些数组的元素是含有30个整数的数组
初始化
int[][] a = { // 三个元素,每个元素都是大小为4的数组
{0, 1, 2, 3}, // 第1行的初始值
{4, 5, 6, 7}, // 第2行的初始值
{8, 9, 10, 11} // 第3行的初始值
};
数组的范围遍历
for (int[] row: a) { // 范围遍历
for (int x: row) // 范围遍历
System.out.printf("%d ", x);
System.out.println();
}
数组API
属性length
:返回数组长度,注意不加小括号
Arrays.sort()
:数组排序
Arrays.fill(int[] a, int val)
:填充数组
Arrays.toString()
:将数组转化为字符串
Arrays.deepToString()
:将多维数组转化为字符串
使用Arrays
需要import java.util.Arrays
注意数组创建后,长度不可变。
习题
AcWing 737. 数组替换
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] a = new int[10];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 10; i++)
a[i] = scanner.nextInt();
scanner.close();
for (int i = 0; i < 10; i++) {
if (a[i] <= 0) a[i] = 1;
System.out.printf("X[%d] = %d\n", i, a[i]);
}
}
}
AcWing 738. 数组填充
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
scanner.close();
for (int i = 0; i < 10; i++) {
System.out.printf("N[%d] = %d\n", i, a);
a *= 2;
}
}
}
AcWing 739. 数组选择
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 100; i++) {
double x = scanner.nextDouble();
if (x <= 10)
System.out.printf("A[%d] = %.1f\n", i, x);
}
scanner.close();
}
}
AcWing 743. 数组中的行
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double[] sum = new double[12];
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
String op = scanner.next();
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++)
sum[i] += scanner.nextDouble();
scanner.close();
if (op.equals("S")) System.out.printf("%.1f\n", sum[k]);
else System.out.printf("%.1f\n", sum[k] / 12.0);
}
}
AcWing 745. 数组的右上半部分
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double res = 0;
Scanner scanner = new Scanner(System.in);
String op = scanner.next();
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++) {
double x = scanner.nextDouble();
if (j > i) res += x;
}
scanner.close();
if (op.equals("M")) res /= 66.0;
System.out.printf("%.1f\n", res);
}
}
AcWing 747. 数组的左上半部分
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double res = 0;
Scanner scanner = new Scanner(System.in);
String op = scanner.next();
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++) {
double x = scanner.nextDouble();
if (j < 11 - i) res += x;
}
scanner.close();
if (op.equals("M")) res /= 66.0;
System.out.printf("%.1f\n", res);
}
}
AcWing 749. 数组的上方区域
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double res = 0;
Scanner scanner = new Scanner(System.in);
String op = scanner.next();
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++) {
double x = scanner.nextDouble();
if (i <= 4 && j > i && j < 11 - i) res += x;
}
scanner.close();
if (op.equals("M")) res /= 30.0;
System.out.printf("%.1f\n", res);
}
}
AcWing 751. 数组的左方区域
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double res = 0;
Scanner scanner = new Scanner(System.in);
String op = scanner.next();
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++) {
double x = scanner.nextDouble();
if (j >= 0 && j <= 4 && i > j && i < 11 - j) res += x;
}
scanner.close();
if (op.equals("M")) res /= 30.0;
System.out.printf("%.1f\n", res);
}
}
笔记
类似“数组的上方区域”这一题,把描述上方区域条件的i
和j
交换后,就能得到描述左方区域的条件。此外,在描述这类区域时,可模仿二重积分积分域的描述方法。
AcWing 753. 平方矩阵 I
import java.util.Scanner;
public class Main {
public static void print(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int di = Math.min(i + 1, n - i);
int dj = Math.min(j + 1, n - j);
System.out.printf("%d ", Math.min(di, dj));
}
System.out.println("");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true) {
int n = scanner.nextInt();
if (n == 0) break;
print(n);
System.out.println("");
}
scanner.close();
}
}
笔记
每个元素的值为当前位置距离四个边界的最小值
AcWing 740. 数组变换
import java.util.Scanner;
public class Main {
public static int n = 20;
public static int[] a = new int[n];
public static void reverse() {
for (int i = 0, j = n - 1; i < j; i++, j--) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < n; i++)
a[i] = scanner.nextInt();
scanner.close();
reverse();
for (int i = 0; i < n; i++)
System.out.printf("N[%d] = %d\n", i, a[i]);
}
}
AcWing 741. 斐波那契数列
import java.util.Scanner;
public class Main {
public static int n = 61;
public static long[] a = new long[n];
public static void init() {
a[0] = 0;
a[1] = 1;
for (int i = 2; i < n; i++)
a[i] = a[i - 2] + a[i - 1];
}
public static void main(String[] args) {
init();
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
for (int i = 0; i < m; i++) {
int k = scanner.nextInt();
System.out.printf("Fib(%d) = %d\n", k, a[k]);
}
scanner.close();
}
}
笔记
int
类型无法保存第60个数,需要用long
类型保存。
AcWing 742. 最小数和它的位置
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int min = 2000, pos = -1;
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
if (x < min) {
min = x;
pos = i;
}
}
scanner.close();
System.out.println("Minimum value: " + min);
System.out.println("Position: " + pos);
}
}
AcWing 744. 数组中的列
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double[] sum = new double[12];
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
String op = scanner.next();
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++)
sum[j] += scanner.nextDouble();
scanner.close();
if (op.equals("S")) System.out.printf("%.1f\n", sum[k]);
else System.out.printf("%.1f\n", sum[k] / 12.0);
}
}
AcWing 748. 数组的右下半部分
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double res = 0;
Scanner scanner = new Scanner(System.in);
String op = scanner.next();
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++) {
double x = scanner.nextDouble();
if (j > 11 - i) res += x;
}
scanner.close();
if (op.equals("M")) res /= 66.0;
System.out.printf("%.1f\n", res);
}
}
AcWing 746. 数组的左下半部分
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double res = 0;
Scanner scanner = new Scanner(System.in);
String op = scanner.next();
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++) {
double x = scanner.nextDouble();
if (j < i) res += x;
}
scanner.close();
if (op.equals("M")) res /= 66.0;
System.out.printf("%.1f\n", res);
}
}
AcWing 750. 数组的下方区域
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double res = 0;
Scanner scanner = new Scanner(System.in);
String op = scanner.next();
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++) {
double x = scanner.nextDouble();
if (i >= 7) {
int k = i - 7;
if (j >= 5 - k && j <= 6 + k)
res += x;
}
}
scanner.close();
if (op.equals("M")) res /= 30.0;
System.out.printf("%.1f\n", res);
}
}
AcWing 752. 数组的右方区域
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double res = 0;
Scanner scanner = new Scanner(System.in);
String op = scanner.next();
for (int i = 0; i < 12; i++)
for (int j = 0; j < 12; j++) {
double x = scanner.nextDouble();
if (j >= 7) {
if (j > i && i + j > 11)
res += x;
}
}
scanner.close();
if (op.equals("M")) res /= 30.0;
System.out.printf("%.1f\n", res);
}
}
AcWing 754. 平方矩阵 II
import java.util.Scanner;
public class Main {
public static void print(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.printf("%d ", Math.abs(i - j) + 1);
System.out.println("");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true) {
int n = scanner.nextInt();
if (n == 0) break;
print(n);
System.out.println("");
}
scanner.close();
}
}
AcWing 755. 平方矩阵 III
import java.util.Scanner;
public class Main {
public static int n = 16;
public static int[][] a;
public static void init() {
a = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = (int)Math.pow(2, i + j);
}
public static void print(int k) {
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++)
System.out.printf(a[i][j] + " ");
System.out.println("");
}
}
public static void main(String[] args) {
init();
Scanner scanner = new Scanner(System.in);
while(true) {
int k = scanner.nextInt();
if (k == 0) break;
print(k);
System.out.println("");
}
scanner.close();
}
}
AcWing 756. 蛇形矩阵
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
scanner.close();
int[][] a = new int[m][n];
int[] dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
int cnt = 1, direction = 0, x = 0, y = 0;
do{
a[x][y] = cnt;
cnt++;
int new_x = x + dx[direction], new_y = y + dy[direction];
if (new_x < 0 || new_x >= m || new_y < 0 || new_y >= n || a[new_x][new_y] > 0) {
direction = (direction + 1) % 4;
new_x = x + dx[direction];
new_y = y + dy[direction];
}
x = new_x;
y = new_y;
} while(cnt <= m * n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
System.out.printf(a[i][j] + " ");
System.out.println("");
}
}
}