반응형
1. 백준 10986
[풀이]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int numN = Integer.parseInt(st.nextToken());
int numM = Integer.parseInt(st.nextToken());
int[][] numS = new int[numN+1][numN+1];
int j = 0;
for (int i = 1; i <= numN * numN; i++) {
int num = i%numN;
if (num == 1) {
st = new StringTokenizer(br.readLine());
j++;
}
if (num == 0) {
num = numN;
}
numS[j][num] = numS[j][num-1] + Integer.parseInt(st.nextToken());
}
int[] results = new int[numM];
for (int i = 0; i < numM; i++) {
st = new StringTokenizer(br.readLine());
int x1 = Integer.parseInt(st.nextToken());
int y1 = Integer.parseInt(st.nextToken());
int x2 = Integer.parseInt(st.nextToken());
int y2 = Integer.parseInt(st.nextToken());
for (j = x1; j <= x2; j++) {
results[i] += numS[j][y2] - numS[j][y1-1];
}
}
for (int result : results) {
System.out.println(result);
}
}
}
[다른풀이]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int numN = Integer.parseInt(st.nextToken());
int numM = Integer.parseInt(st.nextToken());
int[][] numS = new int[numN+1][numN+1];
int j = 0;
for (int i = 1; i <= numN * numN; i++) {
int num = i%numN;
if (num == 1) {
st = new StringTokenizer(br.readLine());
j++;
}
if (num == 0) {
num = numN;
}
numS[j][num] = numS[j][num-1] + numS[j-1][num] - numS[j-1][num-1] + Integer.parseInt(st.nextToken());
}
int[] results = new int[numM];
for (int i = 0; i < numM; i++) {
st = new StringTokenizer(br.readLine());
int x1 = Integer.parseInt(st.nextToken());
int y1 = Integer.parseInt(st.nextToken());
int x2 = Integer.parseInt(st.nextToken());
int y2 = Integer.parseInt(st.nextToken());
results[i] = numS[x2][y2] - numS[x1-1][y2] - numS[x2][y1-1] + numS[x1-1][y1-1];
}
for (int result : results) {
System.out.println(result);
}
}
}
2. 백준 10986
(A+B)%C는 ((A%C) +(B%C) %C 와 같다
nCr = n! / (n-r)! / r!
[풀이] 타임아웃
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int numN = Integer.parseInt(st.nextToken());
int numM = Integer.parseInt(st.nextToken());
long[] numS = new long[numN + 1];
st = new StringTokenizer(br.readLine());
for (int i = 1; i <= numN; i++) {
numS[i] = numS[i - 1] + Long.parseLong(st.nextToken());
}
int result = 0;
for (int i = numN; i > 0; i--) {
for (int j = i - 1; j >= 0; j--) {
if ((numS[i] - numS[j]) % numM == 0) {
result++;
}
}
}
System.out.println(result);
}
}
[다른풀이]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int numN = Integer.parseInt(st.nextToken());
int numM = Integer.parseInt(st.nextToken());
long[] numS = new long[numN + 1];
long[] count = new long[numM];
st = new StringTokenizer(br.readLine());
for (int i = 1; i <= numN; i++) {
numS[i] = numS[i - 1] + Integer.parseInt(st.nextToken());
long number = numS[i] % numM;
count[(int) number]++;
}
long result = count[0];
for (int i = 0; i < numM; i++) {
if (count[i] > 1) {
result += count[i] * (count[i] - 1) / 2;
}
}
System.out.println(result);
}
}
반응형
'ETC' 카테고리의 다른 글
코딩 테스트 (0) | 2023.10.03 |
---|---|
crawling 실습 (0) | 2023.03.29 |
crawling 과제 (0) | 2023.03.28 |
댓글