Develop/백준 (Cpp)
[백준] 1212번 : 8진수 2진수 (C++)
Tarra
2023. 2. 7. 17:08
1212번 : 8진수 2진수
문제 )
8진수가 주어졌을 때, 2진수로 변환하는 프로그램을 작성하시오.
입력 :
첫째 줄에 8진수가 주어진다. 주어지는 수의 길이는 333,334을 넘지 않는다.
출력 :
첫째 줄에 주어진 수를 2진수로 변환하여 출력한다. 수가 0인 경우를 제외하고는 반드시 1로 시작해야 한다.
풀이)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
cin.tie(0);
::ios::sync_with_stdio(0);
string str;
cin >> str;
string answer = "";
int length = str.length();
for (int i = 0; i < length; i++) {
int n = stoi(str.substr(i, 1));
string part = "";
while (n != 0 && n != 1) {
part = char(n % 2 + '0') + part;
n /= 2;
}
if (n != 0) {
part = '1' + part;
}
while (part.length() != 3) {
part = '0' + part;
}
answer += part;
}
bool flag = 0;
for (auto& ele : answer) {
if (ele == '1') {
flag = 1;
}
if (flag) {
cout << ele;
}
}
if (str == "0") {
cout << 0;
}
return 0;
}
|
cs |
출처 : https://www.acmicpc.net/problem/1212
1212번: 8진수 2진수
첫째 줄에 8진수가 주어진다. 주어지는 수의 길이는 333,334을 넘지 않는다.
www.acmicpc.net