Skip to content

Commit

Permalink
Feat: Week012 1210 PrefixSum
Browse files Browse the repository at this point in the history
  • Loading branch information
TalkingPotato90 committed Dec 10, 2024
1 parent 56a7b40 commit e84556b
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 3 deletions.
14 changes: 11 additions & 3 deletions HanByeolKo/week012/Week012.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

### 1209 - 누적합

|문제|분류|링크|
|---|---|---|
|꿀 따기|누적합|https://www.acmicpc.net/problem/21758|
| 문제 |분류| 링크 |
|--------|---|---------------------------------------|
| 꿀 따기 |누적합| https://www.acmicpc.net/problem/21758 |

### 1210 - 누적합

| 문제 | 분류 | 링크 |
|-----------|----------|---------------------------------------|
| 슈퍼 마리오 | 누적합 | https://www.acmicpc.net/problem/2851 |
| 공포의 면담실 | 누적합, 그리디 | https://www.acmicpc.net/problem/30088 |
| 2차원 배열의 합 | 누적합, 구현 | https://www.acmicpc.net/problem/2167 |
52 changes: 52 additions & 0 deletions HanByeolKo/week012/day1210_PrefixSum/Bj2167.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package week012.day1210_PrefixSum;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Bj2167 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());

int startX, startY, endX, endY;

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());
}
}

int k = Integer.parseInt(br.readLine());

for (int i = 0; i < k; i++) {
st = new StringTokenizer(br.readLine());
startX = Integer.parseInt(st.nextToken()) - 1;
startY = Integer.parseInt(st.nextToken()) - 1;
endX = Integer.parseInt(st.nextToken()) - 1;
endY = Integer.parseInt(st.nextToken()) - 1;

int sum = calculateSum(arr, startX, startY, endX, endY);

System.out.println(sum);
}
}

public static int calculateSum(int[][] arr, int sX, int sY, int eX, int eY) {
int sum = 0;

for (int i = sX; i <= eX; i++) {
for (int j = sY; j <= eY; j++) {
sum += arr[i][j];
}
}

return sum;
}
}
39 changes: 39 additions & 0 deletions HanByeolKo/week012/day1210_PrefixSum/Bj2851.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package week012.day1210_PrefixSum;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Bj2851 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] mushroom = new int[10];
int sum = 0;
int[] total = new int[10];

for (int i = 0; i < 10; i++) {
mushroom[i] = Integer.parseInt(br.readLine());
sum += mushroom[i];
total[i] = sum;
}

int small = 0;
int large = 0;

for (int i = 0; i < 10; i++) {
if (total[i] > 100) {
large = total[i];
small = total[i - 1];
break;
}
large = total[i];
}

int smallDif = Math.abs(100 - small);
int largeDif = Math.abs(100 - large);

int result = smallDif < largeDif ? small : large;

System.out.println(result);
}
}
43 changes: 43 additions & 0 deletions HanByeolKo/week012/day1210_PrefixSum/Bj30088.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package week012.day1210_PrefixSum;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Bj30088 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testCase = Integer.parseInt(br.readLine());
int[] sum = new int[testCase];

for(int i = 0; i < testCase; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
int human = Integer.parseInt(st.nextToken());
int time = 0;
for(int j = 0; j < human; j++){
time += Integer.parseInt(st.nextToken());
}
sum[i] = time;
}

Arrays.sort(sum);
int total = 0;
int[] prefixSum = new int[testCase];

for(int i = 0; i < testCase; i++){
total += sum[i];
prefixSum[i] = total;
}

long result = 0;

for(int i = 0; i < testCase; i++){
result += prefixSum[i];
System.out.println(sum[i]);
}

System.out.println(result);
}
}

0 comments on commit e84556b

Please sign in to comment.