备战蓝桥杯JAVA B组Day11
前言
零基础小白备战蓝桥杯第十一天,刷题内容为:洛谷题单【入门4】数组。
目录
P5729 【深基5.例7】工艺品制作
AC代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int w = sc.nextInt();
int x = sc.nextInt();
int h = sc.nextInt();
int q = sc.nextInt();
int [][][] a = new int[w + 1][x + 1][h + 1];
for (int i = 0; i < q; i++) {
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int z1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int z2 = sc.nextInt();
for (int j = x1; j <= x2; j++) {
for (int k = y1; k <= y2; k++) {
for (int l = z1; l <= z2; l++) {
a[j][k][l] = 1;
}
}
}
}
int count = 0;
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= x; j++) {
for (int k = 1; k <= h; k++) {
if (a[i][j][k] == 0) {
count++;
}
}
}
}
System.out.print(count);
}
}
补充:
三维数组循环,将切割的标记,然后在循环统计未切割的个数。
P2550 [AHOI2001] 彩票摇奖
AC代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a,b;
int[]arr = new int[34];
int[]brr = new int[8];
for (int i = 0; i < 7; i++) {
a = sc.nextInt();
arr[a] = 1;
}
for (int i = 0; i < n; i++) {
int p = 0;
for (int j = 0; j < 7; j++) {
b = sc.nextInt();
if(arr[b] == 1){
p++;
}
}
brr[7-p]++;
}
for (int i = 0;i < 7;i++){
System.out.print(brr[i]+" ");
}
}
}
补充:
循环输入小明买彩票数,再用arr[]把中奖码标为一,再输入小明彩票的号码,如果arr[小明的彩票号]=1,就多对一个号码。再使用brr[]作为几等奖的数组,注意购买的彩票中奖号码并不叠加。
P1554 梦中的统计
AC代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long M = sc.nextLong();
long N = sc.nextLong();
int[] count = new int[10];
for (long i = M; i <= N; i++) {
long s = i;
while (s > 0) {
int digit = (int) (s % 10);
count[digit]++;
s /= 10;
}
}
for (int i = 0; i < 10; i++) {
System.out.print(count[i] + " ");
}
}
}
补充:
定义为long类型。
P2141 [NOIP2014 普及组] 珠心算测验
AC代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
boolean[] brr = new boolean[n];
int count = 0;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
brr[i] = false;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int sum = arr[i] + arr[j];
for (int k = 0; k < n; k++) {
if (arr[k] == sum && !brr[k]) {
count++;
brr[k] = true;
}
}
}
}
System.out.println(count);
}
}
补充:
有一个坑:例如2+3=5,1+4=5只能计算一次。所以使用两个数组,一个存储数据,一个用于判断重复。