题解5:数组(工艺品制作、彩票摇奖)
2026/9/8 18:24:16 网站建设 项目流程

题解5:数组(工艺品制作、彩票摇奖)

工艺品制作

题解:

import java.util.Scanner; public class Main09021 { //程序主入口 public static void main(String[] args) { Scanner sc=new Scanner(System.in); //输入读取长宽高w、x、h int w=sc.nextInt(); int x=sc.nextInt(); int h=sc.nextInt(); //输入读取切割次数q int q=sc.nextInt(); //将坐标数据存入三维数组flag[t][j][k] //true表示该编号的小方块已被蒸发 //由题意,方块的标号从1开始,故数组的长度+1,舍弃0下标 boolean [][][]flag=new boolean[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(); //遍历三维数组,将符合条件的小方块编号为true for (int t=x1;t<=x2;t++){//遍历x方向 for (int j=y1;j<=y2;j++){//遍历y方向 for (int k=z1;k<=z2;k++){//遍历z方向 flag[t][j][k]=true; } } } } int remain=0;//设置计数器,用来表示剩余的方块数 //遍历全部存在的小方块编号 for (int t=1;t<=w;t++){ for (int j=1;j<=x;j++){ for (int k=1;k<=h;k++){ //!flag[t][j][k]表示未被蒸发的小正方体被标记为false if(!flag[t][j][k]){ remain++; } } } } //打印输出剩余的小正方体个数 System.out.println(remain); sc.close(); } }

输出结果

彩票摇奖


题解:

package Luogu; import java.util.Scanner; /** * @author jinhong * @date 2026/9/3 21:42 * @description 彩票摇奖 */ public class Main { //程序主入口 public static void main(String[] args) { Scanner sc = new Scanner(System.in); //输入购入的彩票张数n int n = sc.nextInt(); //将中奖的号码存入isWin数组中,设置长度为34 boolean[] isWin = new boolean[34]; //在第二行输入中奖号码,并标记中奖号码为true for (int i = 0; i < 7; i++) { int num = sc.nextInt(); isWin[num] = true; } //定义:win[0]为特等奖,win[1]为一等奖...以此类推 //定义长度为7的数组用来存放输入的7个数据 int[] win = new int[7]; //依据n控制第二行以后输入的数据行数为n for (int t = 0; t < n; t++) { //设置计数器,记录每张彩票中奖号码的个数 int count = 0; //输入每张彩票中的号码并依次遍历每张彩票中的号码 for (int i = 0; i < 7; i++) { int x = sc.nextInt(); //如果彩票中的号码与事先存入数组的中奖的号码相等 if (isWin[x]) { //个数+1 count++; } } //由题可知,由于输出时,从最左侧到最右侧代表特等(下标0),一等(下标1)...每张彩票中需要与中奖号码相等 // 的个数为7、6、5、4、3、2、1 //因此,打印出的数据的下标(index)应为每行输入数字个数(7)-每张彩票中需要与中奖号码相等的个数(count) if (count >= 1) { int index = 7 - count; //下标+1,打印下一个数据 win[index]++; } } //遍历输出中奖张数 for (int i = 0; i < 7; i++) { System.out.print(win[i]+" "); } } }

输出结果:

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询