[λ°±μ€] π₯ 2303 μ«μ κ²μ
π λμ΄λ
π₯ Silver 4
π λ¬Έμ
https://www.acmicpc.net/problem/2303
π νμ΄
μ²μ ꡬνμ ꡬμν λ StringTokenizer
μμ λ°°μ΄μ λ°μμ λ£λ κ² κΉμ§λ μμνκ² νμλ€. μ΄ν λ°μ st
λ₯Ό μλ‘μ΄ ν¨μμ λ£μ΄μ 3κ°λ₯Ό λν 1μ μλ¦¬κ° κ°μ₯ ν¬κ² λμ€λ μ«μλ₯Ό ꡬνν return
νκ² κ΅¬μνμλ€.
νμ§λ§ μ΄μνκ² μ€λ₯κ° λ°μν΄μ λ΄€λλ° μκ³ λ³΄λ 1μ μλ¦¬κ° κ°μ₯ ν¬λ©΄μ κ°μ₯ ν° μ«μλ₯Ό print
νλμ€ μμλλ° μ«μκ° κ°μΌλ©΄ κ°μ₯ ν° λ²νΈμ λ²νΈλ₯Ό μΆλ ₯νλ κ²μ΄μλ€.
μ΄ν λ§κ² μμ μ νμ§λ§ μκ³ λ¦¬μ¦ μ체μμ ν° λ¬Έμ κ° μκ²Όλ€.
NumberGame()
λ 5κ°μ μ«μλ₯Ό λ°μμ λ°°μ΄μ λ£μν 3κ°λ₯Ό λν΄μ 1μ μλ¦¬κ° κ°μ₯ ν° μ«μλ₯Ό return
νλ ν¨μμΈλ°, 5κ°μ λΉκ³΅κ°μ€ 3κ°μ μλ¦¬κ° μλ€λ©΄ λͺ¨λ λ°λ³΅λ¬Έμ μ΅λκ° 3μ΄κ³ , μμ μλ¦¬κ° λ€λ‘ κ°μλ‘ λ€μ μ«μλ€μ μλ¦¬κ° 1κ°μ© μ€μ΄λλ―λ‘ λ€μκ³Ό κ°μ΄ ꡬμμ νμλ€.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3 - i; j++) {
for (int k = 0; k < 3 - j; k++) {
current = cards[i] + cards[j] + cards[k];
if (current % 10 > max % 10) {
max = current;
} else if (current % 10 == max % 10 && current > max) {
max = current;
}
}
}
}
νμ§λ§ μ€μ¬λ‘ μλμ ν΄λ³΄λ, κ° μ리λ§λ€ 000 ~ 333 μ 곡κ°λ§μ μ¬μ©ν μ μλ κ²μ μκ² λμλ€. λ°λΌμ μ΅μ’ μ μΌλ‘ λ€μκ³Ό κ°μ΄ μ½λλ₯Ό μμ±νμλ€.
νΉμλΌλ μΆκ° μμ κ° νμνλ©΄ λ€μ μμ λ μ μ©ν΄λ΄λ λλ€.
3
7 5 5 4 9
1 1 1 1 1
7 5 5 4 9
λ΅ : 3
π Code
package Implements;
import java.io.*;
import java.util.StringTokenizer;
public class N2303 {
public static int[] nums;
private static int i;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter((System.out)));
StringTokenizer st = new StringTokenizer(br.readLine());
int num = Integer.parseInt(st.nextToken());
nums = new int[num];
for (int i = 0; i < num; i++) {
st = new StringTokenizer(br.readLine());
nums[i] = NumberGame(st);
}
int max = 0;
int flag = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 10 >= max % 10) {
max = nums[i];
flag = i;
}
}
System.out.println(flag + 1);
}
public static int NumberGame(StringTokenizer st) {
int[] cards = new int[5];
int index = 0;
while (st.hasMoreTokens()) {
cards[index++] = Integer.parseInt(st.nextToken());
}
int max = 0;
int current;
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 4; j++) {
for (int k = j + 1; k < 5; k++) {
current = cards[i] + cards[j] + cards[k];
if (current % 10 > max % 10) {
max = current;
} else if (current % 10 == max % 10 && current > max) {
max = current;
}
}
}
}
return max;
}
}
package (μ΄λ¦); λ₯Ό λκ³ , class μ΄λ¦μ Main
μΌλ‘ λ³κ²½νλ©΄ λλ€.
κ°μΈ κ³΅λΆ κΈ°λ‘μ© λΈλ‘κ·Έμ
λλ€.
ν리거λ μ€λ₯κ° μμ κ²½μ° μ 보ν΄μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€.π