π λ¬Έ1) μνΌ κ²μ κ°λ°μ μ€λ 리λ ν° κ³ λ―Όμ λΉ μ‘λ€. κ·Έλ κ° λ§λ νλμ¦ μ€μ²μ±μ΄ λμ±κ³΅μ κ±°λμ§λ§, μμ¦ μ κ· μ¬μ©μμ μκ° κΈκ°ν κ²μ΄λ€. μμΈμ μ κ· μ¬μ©μμ κΈ°μ‘΄ μ¬μ©μ μ¬μ΄μ μ€ν μ΄μ§ μ°¨μ΄κ° λ무 ν° κ²μ΄ λ¬Έμ μλ€.
μ΄ λ¬Έμ λ₯Ό μ΄λ»κ² ν κΉ κ³ λ―Ό ν κ·Έλ λ λμ μΌλ‘ κ²μ μκ°μ λλ €μ λμ΄λλ₯Ό μ‘°μ νκΈ°λ‘ νλ€. μμ μνΌ κ°λ°μλΌ λλΆλΆμ λ‘μ§μ μ½κ² ꡬννμ§λ§, μ€ν¨μ¨μ ꡬνλ λΆλΆμμ μκΈ°μ λΉ μ§κ³ λ§μλ€. μ€λ 리λ₯Ό μν΄ μ€ν¨μ¨μ ꡬνλ μ½λλ₯Ό μμ±νλΌ.
μ 체 μ€ν μ΄μ§μ κ°μ N, κ²μμ μ΄μ©νλ μ¬μ©μκ° νμ¬ λ©μΆ°μλ μ€ν μ΄μ§μ λ²νΈκ° λ΄κΈ΄ λ°°μ΄ stagesκ° λ§€κ°λ³μλ‘ μ£Όμ΄μ§ λ, μ€ν¨μ¨μ΄ λμ μ€ν μ΄μ§λΆν° λ΄λ¦Όμ°¨μμΌλ‘ μ€ν μ΄μ§μ λ²νΈκ° λ΄κ²¨μλ λ°°μ΄μ return νλλ‘ solution ν¨μλ₯Ό μμ±νλΌ.
μ νμ¬ν
μ
μΆλ ₯ μ
N | stages | result |
---|---|---|
5 | [2,1,2,6,2,4,3,3] | [3,4,2,1,5] |
4 | [4,4,4,4,4] | [4,1,2,3] |
μ
μΆλ ₯ μ μ€λͺ
μ
μΆλ ₯ μ #1
1λ² μ€ν μ΄μ§μλ μ΄ 8λͺ μ μ¬μ©μκ° λμ νμΌλ©°, μ΄ μ€ 1λͺ μ μ¬μ©μκ° μμ§ ν΄λ¦¬μ΄νμ§ λͺ»νλ€. λ°λΌμ 1λ² μ€ν μ΄μ§μ μ€ν¨μ¨μ λ€μκ³Ό κ°λ€.
2λ² μ€ν μ΄μ§μλ μ΄ 7λͺ μ μ¬μ©μκ° λμ νμΌλ©°, μ΄ μ€ 3λͺ μ μ¬μ©μκ° μμ§ ν΄λ¦¬μ΄νμ§ λͺ»νλ€. λ°λΌμ 2λ² μ€ν μ΄μ§μ μ€ν¨μ¨μ λ€μκ³Ό κ°λ€.
λ§μ°¬κ°μ§λ‘ λλ¨Έμ§ μ€ν μ΄μ§μ μ€ν¨μ¨μ λ€μκ³Ό κ°λ€.
κ° μ€ν μ΄μ§μ λ²νΈλ₯Ό μ€ν¨μ¨μ λ΄λ¦Όμ°¨μμΌλ‘ μ λ ¬νλ©΄ λ€μκ³Ό κ°λ€.
[3,4,2,1,5]
μ
μΆλ ₯ μ #2
λͺ¨λ μ¬μ©μκ° λ§μ§λ§ μ€ν μ΄μ§μ μμΌλ―λ‘ 4λ² μ€ν μ΄μ§μ μ€ν¨μ¨μ 1μ΄λ©° λλ¨Έμ§ μ€ν μ΄μ§μ μ€ν¨μ¨μ 0μ΄λ€.
[4,1,2,3]
λμ νμ΄
package programmers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FailureRate {
public static int[] solution(int N, int[] stages) {
int totalUsers = stages.length;
int[] stageUserCount = new int[N + 2];
for (int stage : stages) {
stageUserCount[stage]++;
}
Map<Integer, Double> stageFailRates = new HashMap<>();
for (int i = 1; i <= N; i++) {
if (totalUsers != 0) {
double failRate = (double) stageUserCount[i] / totalUsers;
stageFailRates.put(i, failRate);
} else {
stageFailRates.put(i, 0.0);
}
totalUsers -= stageUserCount[i];
}
List<Map.Entry<Integer, Double>> entryList = new ArrayList<>(stageFailRates.entrySet());
entryList.sort(Map.Entry.<Integer, Double>comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey()));
int[] answer = new int[N];
for (int i = 0; i < N; i++) {
answer[i] = entryList.get(i).getKey();
}
return answer;
}
public static void main(String[] args) {
solution(5, new int[] {2,1,2,6,2,4,3,3});
}
}
λμ μκ°
int totalUsers = stages.length;
int[] stageUserCount = new int[N + 2];
for (int stage : stages) {
stageUserCount[stage]++;
}
λ§€κ°λ³μλ‘ μ£Όμ΄μ§λ int[] stages
λ₯Ό 보면 νμ¬ λ¨Έλ¬Όκ³ μλ μ€ν
μ΄μ§κ° λνλλ€. μλ₯Όλ€μ΄, {2,1,2,6,2,4,3,3}
μ΄λΌκ³ ν λ, 1λ²μ μ 2λ¨κ³, 2λ²μ μ 1λ¨κ³, 3λ²μ μ 2λ¨κ³, 4λ²μ μ 6λ¨κ³, 5λ²μ μ 2λ¨κ³, 6λ²μ μ 4λ¨κ³, 7λ²μ μ 3λ¨κ³, 8λ²μ μ 3λ¨κ³λ₯Ό μλ―Ένλλ°, λ¨κ³λ³ μΈμμλ₯Ό 체ν¬λ₯Ό forλ¬Έμ ν΅ν΄ λ¨κ³λ³ μΈμ μλ₯Ό 체ν¬ν μ μλ€.
0λ¨κ³ 0λͺ , 1λ¨κ³ 1λͺ , 2λ¨κ³, 3λͺ , 3λ¨κ³ 2λͺ , 4λ¨κ³ 1λͺ , 5λ¨κ³ 0λͺ , 6λ¨κ³ 1λͺ μ μλ―Ένλ€.
Map<Integer, Double> stageFailRates = new HashMap<>();
for (int i = 1; i <= N; i++) {
if (totalUsers != 0) {
double failRate = (double) stageUserCount[i] / totalUsers;
stageFailRates.put(i, failRate);
} else {
stageFailRates.put(i, 0.0);
}
totalUsers -= stageUserCount[i];
}
Map<Integer, Double> stageFailRates = new HashMap<>();
μ€ν
μ΄μ§ λ²νΈλ₯Ό key
λ‘, ν΄λΉ μ€ν
μ΄μ§μ μ€ν¨μ¨μ value
λ‘ νλ Mapμ μμ±, κ° μ€ν
μ΄μ§λ³λ‘ μ€ν¨μ¨μ κ³μ°νκ³ stageFailRates
μ μ μ₯νλλ°, μ€ν¨μ¨μ ν΄λΉ μ€ν
μ΄μ§μ λλ¬νμ§λ§, μμ§ ν΄λ¦¬μ΄νμ§ λͺ»ν νλ μ΄μ΄μ μλ₯Ό νμ¬ μ€ν
μ΄μ§μ λλ¬ν νλ μ΄μ΄ μλ‘ λλ κ°μ΄λ€.
List<Map.Entry<Integer, Double>> entryList = new ArrayList<>(stageFailRates.entrySet());
entryList.sort(Map.Entry.<Integer, Double>comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey()));
List<Map.Entry<Integer, Double>> entryList = new ArrayList<>(stageFailRates.entrySet());
λΌκ³ ν λ, μλ₯Όλ€μ΄, stageFailRates
κ° λ€μκ³Ό κ°λ€λ©΄:
{
1 -> 0.1,
2 -> 0.2,
3 -> 0.3,
4 -> 0.2
}
stageFailRates.entrySet()
[ 1=0.1, 2=0.2, 3=0.3, 4=0.2]
entryList.sort(Map.Entry.<Integer, Double>comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey()));:
μ΄ λΆλΆμ entryList
λ₯Ό μ λ ¬νλ μ½λλ‘
Map.Entry.<Interger, Double> comparingByValue()
λ μ€ν¨μ¨μ κΈ°μ€μΌλ‘ μ€λ¦μ°¨μ μ λ ¬νλ Comparatorλ₯Ό μμ±reversed()
λ μμ±λ Comparatorλ₯Ό λ€μ§μ΄ μ€ν¨μ¨μ΄ λμ μμλλ‘ λ΄λ¦Όμ°¨μ μ λ ¬ν¨thenComparing(Map.Entry.comparingByKey())
λ μ€ν¨μ¨μ΄ κ°μ κ²½μ° μ€ν
μ΄μ§ λ²νΈλ₯Ό κΈ°μ€μΌλ‘ μ€λ¦μ°¨μ μ λ ¬ν¨λ°λΌμ, μ΄ λ‘μ§μ ν΅ν΄ μ€ν¨μ¨μ΄ λμ μ€ν
μ΄μ§λΆν°, κ·Έλ¦¬κ³ μ€ν¨μ¨μ΄ κ°μ κ²½μ°μλ μ€ν
μ΄μ§ λ²νΈκ° μμ μμλ‘ entryList
κ° μ λ ¬λ¨
π λ¬Έ2) μ«μλλΌ κΈ°μ¬λ¨μ κ° κΈ°μ¬μκ²λ 1λ²λΆν° numberκΉμ§ λ²νΈκ° μ§μ λμ΄ μμ΅λλ€. κΈ°μ¬λ€μ 무기μ μμ 무기λ₯Ό ꡬ맀νλ €κ³ ν©λλ€.
κ° κΈ°μ¬λ μμ μ κΈ°μ¬ λ²νΈμ μ½μ κ°μμ ν΄λΉνλ 곡격λ ₯μ κ°μ§ 무기λ₯Ό ꡬ맀νλ € ν©λλ€. λ¨, μ΄μλλΌμμ νμ½μ μν΄ κ³΅κ²©λ ₯μ μ νμμΉλ₯Ό μ νκ³ , μ νμμΉλ³΄λ€ ν° κ³΅κ²©λ ₯μ κ°μ§ 무기λ₯Ό ꡬ맀ν΄μΌ νλ κΈ°μ¬λ νμ½κΈ°κ΄μμ μ ν 곡격λ ₯μ κ°μ§λ 무기λ₯Ό ꡬ맀ν΄μΌ ν©λλ€.
μλ₯Ό λ€μ΄, 15λ²μΌλ‘ μ§μ λ κΈ°μ¬λ¨μμ 15μ μ½μκ° 1, 3, 5, 15λ‘ 4κ° μ΄λ―λ‘, 곡격λ ₯μ΄ 4μΈ λ¬΄κΈ°λ₯Ό ꡬ맀ν©λλ€. λ§μ½, μ΄μλλΌμμ νμ½μΌλ‘ μ ν΄μ§ 곡격λ ₯μ μ νμμΉκ° 3μ΄κ³ μ νμμΉλ₯Ό μ΄κ³Όν κΈ°μ¬κ° μ¬μ©ν 무기μ 곡격λ ₯μ΄ 2λΌλ©΄, 15λ²μΌλ‘ μ§μ λ κΈ°μ¬λ¨μμ 무기μ μμ 곡격λ ₯μ΄ 2μΈ λ¬΄κΈ°λ₯Ό ꡬ맀ν©λλ€. 무기λ₯Ό λ§λ€ λ, 무기μ 곡격λ ₯ 1λΉ 1kgμ μ² μ΄ νμν©λλ€. κ·Έλμ 무기μ μμ 무기λ₯Ό λͺ¨λ λ§λ€κΈ° μν΄ νμν μ² μ 무κ²λ₯Ό 미리 κ³μ°νλ € ν©λλ€.
κΈ°μ¬λ¨μμ μλ₯Ό λνλ΄λ μ μ numberμ μ΄μλλΌμ νμ½μΌλ‘ μ ν΄μ§ 곡격λ ₯μ μ νμμΉλ₯Ό λνλ΄λ μ μ limitμ μ νμμΉλ₯Ό μ΄κ³Όν κΈ°μ¬κ° μ¬μ©ν 무기μ 곡격λ ₯μ λνλ΄λ μ μ powerκ° μ£Όμ΄μ‘μ λ, 무기μ μ μ£ΌμΈμ΄ 무기λ₯Ό λͺ¨λ λ§λ€κΈ° μν΄ νμν μ² μ 무κ²λ₯Ό return νλ solution ν¨μλ₯Ό μμ±νμμ€.
μ νμ¬ν
μ
μΆλ ₯ μ
number | limit | power | result |
---|---|---|---|
5 | 3 | 2 | 10 |
10 | 3 | 2 | 21 |
μ
μΆλ ₯ μ μ€λͺ
μ
μΆλ ₯ μ #1
μ
μΆλ ₯ μ #2
λμ νμ΄
μκ°μ΄κ³Όλ‘ μ€ν¨ν λ‘μ§
package programmers;
public class TemplarsWeapon {
public static int solution(int number, int limit, int power) {
int answer = 0;
int[] divisors = new int[number];
for(int i = 1; i <= number; i++) {
int divisor = 0;
for(int j = 1; j <=i ; j++) {
if(i % j == 0) {
divisor++;
}
divisors[i-1] = divisor;
}
}
int cnt = 0;
for(int a : divisors) {
cnt++;
if(a > limit) {
a = power;
divisors[cnt-1] = a;
}
answer +=a;
}
System.out.println(answer);
return answer;
}
public static void main(String[] args) {
solution(10, 3, 2);
}
}
λμ μκ°
μκ° λ³΅μ‘λλ₯Ό κ³ λ €ν λ‘μ§
package programmers;
public class TemplarsWeapon {
public static int solution(int number, int limit, int power) {
int answer = 0;
for(int i = 1; i <= number; i++) {
int divisor = 0;
int sqrt = (int)Math.sqrt(i);
for(int j = 1; j <= sqrt; j++) {
if( i % j == 0) {
divisor += (j == i / j) ? 1 : 2;
}
}
if(divisor > limit) {
answer += power;
} else {
answer += divisor;
}
}
return answer;
}
public static void main(String[] args) {
solution(10, 3, 2);
}
}
μλΌν μ€ν
λ€μ€μ 체(Sieve of Eratosthenes) λ°©μμ κΈ°λ°μΌλ‘ ν΄κ²°ν λ‘μ§
class Solution {
public int solution(int number, int limit, int power) {
int[] count = new int[number + 1];
for (int i = 1; i <= number; i++) {
for (int j = 1; j <= number / i; j++) {
count[i * j]++;
}
}
int answer = 0;
for (int i = 1; i <= number; i++) {
if (count[i] > limit) {
answer += power;
} else {
answer += count[i];
}
}
return answer;
}
}
π λ¬Έ3) λ¬Έμμ΄ sκ° μ λ ₯λμμ λ λ€μ κ·μΉμ λ°λΌμ μ΄ λ¬Έμμ΄μ μ¬λ¬ λ¬Έμμ΄λ‘ λΆν΄νλ €κ³ ν©λλ€.
λ¬Έμμ΄ sκ° λ§€κ°λ³μλ‘ μ£Όμ΄μ§ λ, μ κ³Όμ κ³Ό κ°μ΄ λ¬Έμμ΄λ€λ‘ λΆν΄νκ³ , λΆν΄ν λ¬Έμμ΄μ κ°μλ₯Ό return νλ ν¨μ solutionμ μμ±νμΈμ.
μ νμ¬ν
μ
μΆλ ₯ μ
s | result |
---|---|
"banana" | 3 |
"abracadabra" | 6 |
"aaabbaccccabba" | 3 |
μ
μΆλ ₯ μ μ€λͺ
μ
μΆλ ₯ μ #1
s
="banana"μΈ κ²½μ° ba - na - naμ κ°μ΄ λΆν΄λ©λλ€.
μ
μΆλ ₯ μ #2
s
="abracadabra"μΈ κ²½μ° ab - ra - ca - da - br - aμ κ°μ΄ λΆν΄λ©λλ€.
μ
μΆλ ₯ μ #3
s
="aaabbaccccabba"μΈ κ²½μ° aaabbacc - ccab - baμ κ°μ΄ λΆν΄λ©λλ€.
λμ μκ°
package programmers;
public class StrSplit {
public static int solution(String s) {
int answer = 0;
int i = 0, j = 0, len = s.length();
int countTarget = 0, countOther = 0;
while (i < len && j < len) {
char target = s.charAt(i);
if (s.charAt(j) == target) {
countTarget++;
} else {
countOther++;
}
if (countTarget == countOther) {
answer++;
i = j + 1;
countTarget = 0;
countOther = 0;
}
j++;
}
if (i < len) answer++;
return answer;
}
public static void main(String[] args) {
solution("banana");
}
}
π λ¬Έ4) λ μ μ X, Yμ μμμ μ리μμ 곡ν΅μΌλ‘ λνλλ μ μ k(0 β€ k β€ 9)λ€μ μ΄μ©νμ¬ λ§λ€ μ μλ κ°μ₯ ν° μ μλ₯Ό λ μμ μ§κΏμ΄λΌ ν©λλ€(λ¨, 곡ν΅μΌλ‘ λνλλ μ μ μ€ μλ‘ μ§μ§μ μ μλ μ«μλ§ μ¬μ©ν©λλ€). X, Yμ μ§κΏμ΄ μ‘΄μ¬νμ§ μμΌλ©΄, μ§κΏμ -1μ λλ€. X, Yμ μ§κΏμ΄ 0μΌλ‘λ§ κ΅¬μ±λμ΄ μλ€λ©΄, μ§κΏμ 0μ λλ€.
μλ₯Ό λ€μ΄, X = 3403μ΄κ³ Y = 13203μ΄λΌλ©΄, Xμ Yμ μ§κΏμ Xμ Yμμ 곡ν΅μΌλ‘ λνλλ 3, 0, 3μΌλ‘ λ§λ€ μ μλ κ°μ₯ ν° μ μμΈ 330μ
λλ€. λ€λ₯Έ μμλ‘ X = 5525μ΄κ³ Y = 1255μ΄λ©΄ Xμ Yμ μ§κΏμ Xμ Yμμ 곡ν΅μΌλ‘ λνλλ 2, 5, 5λ‘ λ§λ€ μ μλ κ°μ₯ ν° μ μμΈ 552μ
λλ€(Xμλ 5κ° 3κ°, Yμλ 5κ° 2κ° λνλλ―λ‘ λ¨λ 5 ν κ°λ μ§ μ§μ μ μμ΅λλ€.)
λ μ μ X, Yκ° μ£Όμ΄μ‘μ λ, X, Yμ μ§κΏμ returnνλ solution ν¨μλ₯Ό μμ±ν΄μ£ΌμΈμ.
μ νμ¬ν
μ
μΆλ ₯ μ
X | Y | result |
---|---|---|
100 | 2345 | -1 |
100 | 203045 | 0 |
100 | 123450 | 10 |
12321 | 42531 | 321 |
5525 | 1255 | 552 |
μ
μΆλ ₯ μ μ€λͺ
μ
μΆλ ₯ μ #1
μ
μΆλ ₯ μ #2
μ
μΆλ ₯ μ #3
μ
μΆλ ₯ μ #4
μ
μΆλ ₯ μ #5
λμ νμ΄
package programmers;
import java.util.Arrays;
public class NumberPair {
public static String solution(String X, String Y) {
int[] countX = new int[10];
int[] countY = new int[10];
int[] countPair = new int[10];
for (char c : X.toCharArray()) {
countX[c - '0']++;
}
for (char c : Y.toCharArray()) {
countY[c - '0']++;
}
for (int i = 0; i <= 9; i++) {
countPair[i] = Math.min(countX[i], countY[i]);
}
StringBuilder pair = new StringBuilder();
for (int i = 9; i >= 0; i--) {
for (int j = 0; j < countPair[i]; j++) {
System.out.println(j);
pair.append(i);
}
}
if (pair.length() == 0) {
return "-1";
}
boolean isOnlyZero = true;
for (char c : pair.toString().toCharArray()) {
if (c != '0') {
isOnlyZero = false;
break;
}
}
if (isOnlyZero) {
return "0";
}
int startIndex = 0;
while (pair.charAt(startIndex) == '0') {
startIndex++;
}
return pair.substring(startIndex);
}
public static void main(String[] args) {
solution("5525", "1255");
}
}
π λ¬Έ5) μΉ΄μΉ΄μ€ν‘μ λ¬ λ€ λ²μ§Έ λ³! μ¬μ¬ν λ? μΉ΄μΉ΄μ€ν‘ κ²μλ³~
μΉ΄μΉ΄μ€ν‘ κ²μλ³μ νλ°κΈ° μ κ· μλΉμ€λ‘ λ€νΈ κ²μμ μΆμνκΈ°λ‘ νλ€. λ€νΈ κ²μμ λ€νΈνμ λ€νΈλ₯Ό μΈ μ°¨λ‘ λμ Έ κ·Έ μ μμ ν©κ³λ‘ μ€λ ₯μ 겨루λ κ²μμΌλ‘, λͺ¨λκ° κ°λ¨ν μ¦κΈΈ μ μλ€.
κ° μ
μ¬ν 무μ§λ μ½λ© μ€λ ₯μ μΈμ λ°μ κ²μμ ν΅μ¬ λΆλΆμΈ μ μ κ³μ° λ‘μ§μ λ§‘κ² λμλ€. λ€νΈ κ²μμ μ μ κ³μ° λ‘μ§μ μλμ κ°λ€.
0~10μ μ μμ λ¬Έμ S, D, T, *, #λ‘ κ΅¬μ±λ λ¬Έμμ΄μ΄ μ λ ₯λ μ μ΄μ μλ₯Ό λ°ννλ ν¨μλ₯Ό μμ±νλΌ.
μ
λ ₯ νμ
"μ μ|보λμ€|[μ΅μ ]"μΌλ‘ μ΄λ£¨μ΄μ§ λ¬Έμμ΄ 3μΈνΈ.
μ) 1S2D*3T
μΆλ ₯ νμ
3λ²μ κΈ°νμμ μ»μ μ μ ν©κ³μ ν΄λΉνλ μ μκ°μ μΆλ ₯νλ€.
μ) 37
μ
μΆλ ₯ μμ
λμ νμ΄
package programmers;
import java.util.Arrays;
public class DartGame {
public static int solution(String dartResult) {
int[] score = new int[3];
int index = -1;
for(int i = 0; i < dartResult.length(); i++) {
char c = dartResult.charAt(i);
if(Character.isDigit(c)) {
index++;
if(c == '1' && dartResult.charAt(i+1) == '0') {
score[index] = 10;
i++;
}else {
score[index] = Character.getNumericValue(c);
}
}else if(c =='D') {
score[index] = (int)Math.pow(score[index],2);
}else if(c =='T') {
score[index] = (int)Math.pow(score[index], 3);
}else if(c =='*') {
if(index > 0) {
score[index - 1] *= 2;
}
score[index] *= 2;
}else if(c =='#') {
score[index] *= -1;
}
}
return score[0] + score[1] + score[2];
}
public static void main(String[] args) {
solution("1S*2T*3S");
}
}
λμ μκ°
λ§€κ°λ³μλ‘ μ£Όμ΄μ§λ dartResult
λ₯Ό νκΈμ μ© μ§€λΌ String λ§€κ°λ³μ dartResult
μ λ¨Όμ μ«μκ° ν¬ν¨λΌμλμ§ λ¨Όμ νμΈνλ€. λ§μ½, μ«μκ° μ‘΄μ¬νλ©΄ index++
λ₯Ό μ§ννλλ°,
λ§€κ°λ³μμλ μ«μκ° 3κ°λ§ μ‘΄μ¬νκΈ° λλ¬Έμ, int[] score = new int[3]
μΌλ‘ ν¬κΈ°λ₯Ό μ€μ νμ¬ indexλ₯Ό ν λΉνλ€.
if(c == '1' && dartResult.charAt(i+1) == '0') {
score[index] = 10;
i++;
}else {
score[index] = Character.getNumericValue(c);
}
ν΄λΉ λ‘μ§μ ν΅ν΄, μ«μκ° 1~10 κΉμ§ μ‘΄μ¬νκΈ°λλ¬Έμ, μ«μ 10
μ κ²μΆνλ λ°©λ²μΌλ‘ νμ¬μ λ¬Έμκ° 1
& λ€μ λ¬Έμκ° 0
μ΄λ©΄ 10μ μλ―ΈνκΈ° λλ¬Έμ, ν΄λΉνλ score[index]
κ°μ 10μ μΆκ°μν¨λ€. κ·Έλ μ§μμΌλ©΄, ν΄λΉ λ¬Έμλ₯Ό μ«μλ‘ λ°κΎΈλ Character.getNumericValue
λ©μλλ₯Ό μ¬μ©νμ¬ score[index]
μ λ΄λλ€.
else if(c =='D') {
score[index] = (int)Math.pow(score[index],2);
}else if(c =='T') {
score[index] = (int)Math.pow(score[index], 3);
}else if(c =='*') {
if(index > 0) {
score[index - 1] *= 2;
}
score[index] *= 2;
}else if(c =='#') {
score[index] *= -1;
}
c == 'S'
λ ν΄λΉνλ μ«μμ *1
μ μλ―ΈνκΈ° λλ¬Έμ, Double
, Triple
λ§ μ²΄ν¬ νλ€. κ·Έλ¦¬κ³ *
, #
λ₯Ό 체ν¬νμ¬ μ΅μ
μ΄ λ°μν κ²½μ°λ₯Ό μ²λ¦¬νλ€. *
κ° λμ¬ κ²½μ° νμ¬ μ μ score[index]
λ₯Ό 2λ°°λ‘ λ§λ€κ³ , λ§μ½ μ΄μ μ μ score[index -1]
μ΄ μ‘΄μ¬νλ€λ©΄ μ΄μ μ μλ 2λ°°λ‘ λ§λ¦. 쑰건문 if (index > 0)
μ indexκ° 0λ³΄λ€ ν΄ κ²½μ°μλ§ μ΄μ μ μλ₯Ό 2λ°°λ‘ λ§λλ μ²λ¦¬λ₯Ό ν¨. μ΄λ 첫 λ²μ§Έ μ μμ κ²½μ° μ΄μ μ μκ° μμΌλ―λ‘, μ΄μ μ μμ λν μ²λ¦¬λ₯Ό νμ§ μλλ‘ νκΈ°μν¨μ΄λ€.
μ’μ κΈ κ°μ¬ν©λλ€.