https://www.acmicpc.net/problem/11104
11104번: Fridge of Your Dreams
Eirik drinks a lot of Bingo Cola to help him program faster, and over the years he has burned many unnecessary calories walking all the way to the kitchen to get some. To avoid this he has just bought a small fridge, which is beautifully placed next to his
www.acmicpc.net
코드
#include <iostream>
#include <string>
using namespace std;
int main() {
int n, total;
string num;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> num;
total = stoi(num, nullptr, 2);
cout << total << "\n";
}
return 0;
}
풀이
입력받은 24비트의 이진수를 십진수로 변환하여 출력하는 문제이다.
string 라이브러리의 stoi 함수를 사용하면 이진수를 십진수로 편하게 변환이 가능하다.
이때 사용하면 stoi 함수는 다음과 같다
stoi ( 변환할 string형 이진수, 숫자가 아닌 부분의 포인터, 변환할 진수 ) 형태로 사용할 수 있다.
이때 두 번째 매개변수가 뜻하는 것은 예를 들어 num = "1101SD"일 때 맨 처음부터 숫자가 나오지 않는 부분인 S의 포인터인 4가 들어간다.
하지만 이 문제에선 사용하지 않는 부분이므로 nullptr 넣어 사용하지 않으면 된다.
마지막 진수 부분을 2로 하여 이진수로 변환하면 된다.
참고: https://blockdmask.tistory.com/333
[C++] stoi, stof, stol, stod 함수에 대해서 (string to int)
안녕하세요. BlockDMask 입니다. 지난시간에는 C/C++에 기존에 존재하던 atoi, atof, atol등 char* 타입의 문자열을 정수로, 실수로 (=숫자로) 변경하는 함수에 대해서 살펴 보았습니다. 오늘은! C++11에서 부
blockdmask.tistory.com
'문제 해결 > BaekJoon' 카테고리의 다른 글
[백준] [C++] 11367번 Report Card Time (0) | 2022.11.18 |
---|---|
[백준] [C++] 11319번 Count Me In (0) | 2022.11.17 |
[백준] [C++] 10926번 ??! (0) | 2022.11.17 |
[백준] [C++] 10817번 세 수 (0) | 2022.11.16 |
[백준] [C++] 10699번 오늘 날짜 (0) | 2022.11.16 |