๋ฐ์ํ
๋ฌธ์
2์ง์๊ฐ ์ฃผ์ด์ก์ ๋, 8์ง์๋ก ๋ณํํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ 2์ง์๊ฐ ์ฃผ์ด์ง๋ค. ์ฃผ์ด์ง๋ ์์ ๊ธธ์ด๋ 1,000,000์ ๋์ง ์๋๋ค.
์ถ๋ ฅ
์ฒซ์งธ ์ค์ ์ฃผ์ด์ง ์๋ฅผ 8์ง์๋ก ๋ณํํ์ฌ ์ถ๋ ฅํ๋ค.
์๊ฐ ์ ํ | ๋ฉ๋ชจ๋ฆฌ ์ ํ | ์ ์ถ | ์ ๋ต | ๋งํ ์ฌ๋ | ์ ๋ต ๋น์จ |
---|---|---|---|---|---|
1 ์ด | 128 MB | 31251 | 12330 | 10156 | 41.016% |
https://www.acmicpc.net/problem/1373
ํ์ด
์ฝ๋
1๋ฒ์งธ ์๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Queue<Character> queue = new LinkedList<>();
StringBuilder sb = new StringBuilder();
String string = br.readLine();
int result = 0;
if(string.equals("0")) System.out.println(0);
for (int i = string.length() - 1; i >= 0; i--) {
queue.add(string.charAt(i));
if (queue.size() == 3) {
String index = "";
while (!queue.isEmpty()) {
index += queue.poll();
}
for (int j = index.length() - 1; j >= 0; j--) {
if (index.charAt(j) != '0') {
result += (int) Math.pow(2, j);
}
}
if(result != 0){
sb.append(result);
result = 0;
}
}
}
String index = "";
while (!queue.isEmpty()) {
index += queue.poll();
}
for (int j = index.length() - 1; j >= 0; j--) {
if (index.charAt(j) != '0') {
result += (int) Math.pow(2, j);
}
}
if(result != 0) {
sb.append(result);
}
sb.reverse();
System.out.println(sb);
br.close();
}
}
sb๋ก ์ถ๋ ฅ์ ํ๋ ์์ 0
์ด ์ถ๋ ฅ๋๋ ๊ฒฝ์ฐ๊ฐ ์์ด, BigInteger
๋ก ์ฒ๋ฆฌ๋ฅผ ํด๋ดค๋ค. ๊ทธ๋ ๊ฒ ์์ฑํ๋ ๋ฉ๋ชจ๋ฆฌ์ด๊ณผ๊ฐ ๋ฐ์ํ์ฌ, ๊ฒฐ๊ณผ๊ฐ์ด 0์ด ์๋๋๋ง sb.append()
๋ฅผ ํ๊ฒ ์์ฑํ์๋ค. ์ง์ ํ
์คํธํ ๋๋ ์ ์์ ์ผ๋ก ๋๋๋ฐ, ๋ฐฑ์ค์์ ๋๋ฆฌ๋ฉด ์คํจ๊ฐ ๋ฐ์ํ๋ค...
๋๋ฒ์งธ ์๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _2์ง์8์ง์ {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String number = br.readLine();
// ์
๋ ฅ๋ 2์ง์ ๋ฌธ์์ด์ ๊ธธ์ด ๊ตฌํ๊ธฐ
int length = number.length();
// ์
๋ ฅ๋ 2์ง์ ๋ฌธ์์ด์ 3์๋ฆฌ์ฉ ๋์ด 8์ง์๋ก ๋ณํํ๊ธฐ
int remainder = length%3;// ๋๋จธ์ง ๋ถ๋ถ์ ๊ธธ์ด๋ฅผ ๊ณ์ฐ
// ๋๋จธ์ง ๋ถ๋ถ์ด 1 ๋๋ 2์๋ฆฌ์ธ ๊ฒฝ์ฐ ๋ณํ
if(remainder > 0){
sb.append(Integer.parseInt(number.substring(0,remainder),2));
}
while (remainder < length){
// 2์ง์๋ฅผ 10์ง์๋ก ๋ณํํจ(3์๋ฆฌ์ฉ ๋์๊ธฐ๋๋ฌธ์ ์ต๋ 7์)
sb.append(Integer.parseInt(number.substring(remainder,remainder+3),2));
remainder +=3;
}
System.out.println(sb);
br.close();
}
}
gpt์ ๋์์ ๋ฐ์๋ค. ์ด๋ ต๋ค ์ง์ง...ใ ใ
๋ฐ์ํ