본문 바로가기
백준 알고리즘

[백준][C++] 11005번 : 진법 변환 2

by 탱글한푸딩 2024. 6. 21.
반응형

문제

https://www.acmicpc.net/problem/11005

 


코드

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	int n, b, temp;
	string s;

	cin >> n >> b;

	while (n != 0)
	{
		temp = n % b;
		if (temp > 9)
		{
			temp += ('A' - 10);
			s += temp;
		}
		else
		{
			s += '0' + temp;
		}
		n /= b;
	}
	reverse(s.begin(), s.end());

	cout << s << '\n';

	return 0;
}

풀이

reverse(s.begin(), s.end()) : s 문자열의 시작부터 끝까지 역순으로 변경한다.

reverse() 함수를 선언하려면 <algorithm> 헤더파일을 선언 해주어야 한다.

반응형