Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16주차 #42

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions HYUNSOO/BOJ/BOJ_13305.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

class Main {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

int cityNumber = Integer.parseInt(bufferedReader.readLine());

int[] nextDistance = new int[cityNumber];
int[] oilCost = new int[cityNumber];

StringTokenizer tokenizer = new StringTokenizer(bufferedReader.readLine());
for (int i = 0; i < cityNumber-1; i++) {
nextDistance[i] = Integer.parseInt(tokenizer.nextToken());
}

tokenizer = new StringTokenizer(bufferedReader.readLine());
for (int i = 0; i < cityNumber; i++) {
oilCost[i] = Integer.parseInt(tokenizer.nextToken());
}

long totalCost = getTotalCost(cityNumber, oilCost, nextDistance);

System.out.println(totalCost);
}

private static long getTotalCost(int cityNumber, int[] oilCost, int[] nextDistance) {
long totalCost = 0;
for (int i = 0; i < cityNumber; i++) {
int cOilCost = oilCost[i];
int nextCity = i+1;

for (; nextCity < cityNumber; nextCity++) {
if (cOilCost > oilCost[nextCity]) {
break;
}
}

int cNextDistance = 0;
for (int j = i; j < nextCity; j++) {
cNextDistance += nextDistance[j];
}

totalCost += ((long) cOilCost * cNextDistance);
i = nextCity - 1;
}
return totalCost;
}
}
49 changes: 49 additions & 0 deletions HYUNSOO/BOJ/BOJ_1541.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

class Main {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String str = bufferedReader.readLine();

List<Integer> numbers = new ArrayList<>();
List<Character> sign = new ArrayList<>();

StringBuilder number = new StringBuilder();
for (char c : str.toCharArray()) {
if (c == '-' || c == '+') {
numbers.add(Integer.parseInt(number.toString()));
sign.add(c);

number = new StringBuilder();
continue;
}
number.append(c);
}
numbers.add(Integer.parseInt(number.toString()));

int result = numbers.get(0);
int sum = 0;
for (int i = 1; i < numbers.size(); i++) {
if (sign.get(i-1) == '-') {
result -= sum;
sum = 0;
sum += numbers.get(i);

} else if (sign.get(i-1) == '+') {
if (sum == 0) {
result += numbers.get(i);
} else {
sum += numbers.get(i);

}
}
}
result -= sum;

System.out.println(result);
}
}