[Java] Multidimensional array / inner product calculation (AOJ⑦ inner product)

Multidimensional array

// 3*4*3D array of 5
init [][][] list = new int [3][4][5]; 
//Initialize and declare a 2D array
int [][] list = [
  { 1, 2, 3 },
  { 4, 5, 6 },
  { 7, 8, 9 },
};
//Initialize with var and declare
var list = new int[][] [
  { 1, 2, 3 },
  { 4, 5, 6 },
  { 7, 8, 9 },
};
//Access 0 rows and 1 column
System.out.println(list[0][1]); //2

Jug array

var list = new int [3][];
list[0] = new int[2]; //2 elements in the 0th array
list[1] = new int[3]; //3 elements in the 0th array
list[2] = new int[4]; //4 elements in the 0th array
var list = new int [][] {
  { 1, 2 },
  { 3, 4, 5 },
  { 6, 7, 8, 9 },
};
System.out.println(list[1][2]); //5 * Size is 3

Number of residents in the official residence

University A manages 10 rooms on 1 floor and 4 official residences on 3 floors. Please create a program that reads the information on moving in and out of the official residence and outputs the number of residents in each room. N items of information are given. For each information, four integers b, f, r, v are given. This indicates that v people have moved in to the rth room on the f floor of building b. A negative value for v indicates that -v has been removed. Initially, it is assumed that no one is in all the rooms. Input The first line is given the number n of information. Then, n information is given. Each piece of information is given four integers b, f, r, v on one line, separated by whitespace. Output Please output the number of residents for 4 buildings. For each building, the number of residents is output in the order of the 1st floor, 2nd floor, and 3rd floor. For each floor, the number of residents in the 1st, 2nd, ..., 10th rooms is output in order. Please output one space before the number of residents. Also, separate each building with ################### (20 #). Constraints

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		//10 rooms, 4 three-story official residences
		int[][][] list = new int[4][3][10];
		
		for(int i = 0; i < n; i++) {
		    //v people added to the rth room on the f floor of building b
			int b = sc.nextInt();
			int f = sc.nextInt();
			int r = sc.nextInt();
			int v = sc.nextInt();
			
			list[b-1][f-1][r-1] += v;
		}

		for(int i = 0 ; i< 4; i++) {
			for(int j = 0; j < 3; j++) {
				for (int k = 0; k < 10; k++) {
					System.out.print(" " + list[i][j][k]);
				}
				System.out.println();
			}
			if (i != 3)
				System.out.println("####################");
		}
        }
}

Vector and matrix product

Input N and m are given on the first line, separated by spaces. Continue The n rows are given the elements aij of the matrix A, separated by spaces. The element bi of the vector b is given to each of the following m rows. Output The output consists of n lines. Output each element of vector c to one line

//Inner product calculation
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        int m=scan.nextInt();
        int a[][]=new int[n][m];
        //Matrix A
        for(int i=0;i<n;i++)for(int j=0;j<m;j++){
            a[i][j]=scan.nextInt();
        }
        //Vector b
        int b[]=new int[m];
        for(int i=0;i<m;i++)b[i]=scan.nextInt();
        //Vector c
        int c[]=new int[n];
        //c=A*b Inner product calculation
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                c[i]+=a[i][j]*b[j];
            }
        }
        for(int i=0;i<n;i++)System.out.println(c[i]);
        scan.close();
    }
}

Recommended Posts

[Java] Multidimensional array / inner product calculation (AOJ⑦ inner product)
[Java] Output multidimensional array / spreadsheet (AOJ⑥ spreadsheet)
[Java] Data type / matrix product (AOJ ⑧ Matrix product)
[Java] array
Java array
Java array
java (array)
Java array
[Java] Array
Java array
java array
[Java] Array
[Java beginner] About initialization of multidimensional array
java array variable
[Java] Array notes
Java learning 2 (learning calculation method)
Java inner class review
About Java Array List
[Java ~ Array ~] Study memo 4
Multidimensional array in Swift
Declaration of multidimensional array Let's try TypeScript for Java problem 4-4