Java Challenge
2차원 배열
백준 2738
- N*M크기의 두 행렬 A와 B가 주어졌을 때, 두 행렬을 더하는 프로그램을 작성하시오.
package 배열2;
import java.util.Scanner;
public class BOJ2738 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int [][]arrs = new int[n][m];
int [][]arrs2 = new int[n][m];
for(int i = 0; i <n; i++){
for(int j=0; j < m; j++){
arrs[i][j] = scanner.nextInt();
}
}
for(int i = 0; i <n; i++){
for(int j=0; j < m; j++){
arrs2[i][j] = scanner.nextInt();
arrs[i][j] = arrs[i][j] + arrs2[i][j];
}
}
for(int i = 0; i <n; i++){
for(int j=0; j < m; j++){
System.out.print(arrs[i][j] + " ");
}
System.out.println("");
}
}
}
백준 2566
- 9×9 격자판에 쓰여진 81개의 자연수 또는 0이 주어질 때, 이들 중 최댓값을 찾고 그 최댓값이 몇 행 몇 열에 위치한 수인지 구하는 프로그램을 작성하시오.
package 배열2;
import java.util.Scanner;
public class BOJ2566 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int [][]arrs = new int[9][9];
int max = 0;
int x = 0,y=0;
for(int i=0;i<9;i++){
for(int j=0; j<9;j++){
arrs[i][j] = scanner.nextInt();
if(max <arrs[i][j]){
max = arrs[i][j];
x = i;
y = j;
}
}
}
System.out.println(max);
System.out.println((x+1)+" "+(y+1));
}
}
백준 10798
- 칠판에 붙여진 단어들이 주어질 때, 영석이가 세로로 읽은 순서대로 글자들을 출력하는 프로그램을 작성하시오.
package 배열2;
import java.util.Scanner;
public class BOJ10798 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String []strs = new String[5];
int sum = 0;
for(int i = 0;i<5;i++){
strs[i] = scanner.next();
sum += (strs[i].length());
}
int i = 0;
StringBuilder sb = new StringBuilder();
while(sb.length() != sum){
for(int j = 0; j<5; j++){
if(i<strs[j].length()){
sb.append(strs[j].charAt(i));
}
}
i++;
}
System.out.println(sb);
}
}
백준 2563
- 색종이를 한 장 또는 여러 장 붙인 후 색종이가 붙은 검은 영역의 넓이를 구하는 프로그램을 작성하시오.
package 배열2;
import java.util.Arrays;
import java.util.Scanner;
public class BOJ2563 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int [][]arrs = new int[100][100];
for(int[]i:arrs)Arrays.fill(i , 0);
int count = scanner.nextInt();
int x,y;
int answer = 0;
for(int i = 0; i<count;i++){
x = scanner.nextInt();
y = scanner.nextInt();
for(int j = x; j < x+10; j++){
for(int k = y; k < y+10; k++){
if(arrs[j][k] == 0){
arrs[j][k] += 1;
answer++;
} else if (arrs[j][k] >0) {
continue;
}
}
}
}
System.out.println(answer);
}
}