[JAVA] 백준 7단계
2024. 9. 10. 18:00ㆍ코딩 테스트(Coding Test)/백준
2738 행렬 덧셈
나의 답) 148ms
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
arr[i][j] += Integer.parseInt(st.nextToken());
sb.append(arr[i][j]).append(" ");
}
sb.append("\n");
}
System.out.print(sb);
}
}
다른 사람의 답)
public class Main {
static int read() throws Exception {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
public static void main(String... args) throws Exception {
StringBuilder sb = new StringBuilder();
int n = read(), m = read();
int[][] arr = new int[n][m];
for(int i = 0; i < n * 2; i++) {
for(int j = 0; j < m; j++) {
arr[i % n][j] += read();
if(i >= n) sb.append(arr[i % n][j]).append(" ");
}
if(i >= n) sb.append("\n");
}
System.out.print(sb);
}
}
2566 최댓값
나의 답) 100ms
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
int max = -1;
int n = 0, m = 0;
for (int i = 1; i <= 9; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 1; j <=9; j++) {
int temp = Integer.parseInt(st.nextToken());
if (temp > max) {
max = temp;
n = i;
m = j;
}
}
}
sb.append(max).append("\n").append(n).append(" ").append(m);
System.out.print(sb);
}
}
10798 세로읽기
나의 답) 104ms
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String[] words = new String[5];
for (int i = 0; i < 5; i++) {
words[i] = br.readLine();
}
for (int j = 0; j < 15; j++) {
for (int i = 0; i < 5; i++) {
if (j < words[i].length()) {
sb.append(words[i].charAt(j));
}
}
}
System.out.print(sb);
}
}
2563 색종이
나의 답) 100ms
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int n = Integer.parseInt(br.readLine());
boolean[][] paper = new boolean[100][100];
int area = 0;
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
for (int j = x; j < x + 10; j++) {
for (int k = y; k < y + 10; k++) {
if (!paper[j][k]) {
paper[j][k] = true;
area++;
}
}
}
}
System.out.print(area);
}
}
'코딩 테스트(Coding Test) > 백준' 카테고리의 다른 글
[JAVA] 백준 13단계 (0) | 2024.11.24 |
---|---|
[JAVA] 백준 10단계 (0) | 2024.11.12 |
[JAVA] 백준 6단계 (1) | 2024.09.09 |
[JAVA] 백준 5단계 (3) | 2024.09.09 |
[JAVA] 백준 4단계 (0) | 2024.09.05 |