๐Ÿ’  Cpp/[BOJ] ๋‹จ๊ณ„๋ณ„๋กœ ํ’€์–ด๋ณด๊ธฐ

[BOJ][C++] ๋ฐฑ์ค€ 11005๋ฒˆ: ์ง„๋ฒ• ๋ณ€ํ™˜ 2 (Bronze I)

์„ ๋‹ฌ 2024. 12. 25. 02:10
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

10์ง„๋ฒ• ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ์ˆ˜๋ฅผ B์ง„๋ฒ•์œผ๋กœ ๋ฐ”๊ฟ” ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.
10์ง„๋ฒ•์„ ๋„˜์–ด๊ฐ€๋Š” ์ง„๋ฒ•์€ ์ˆซ์ž๋กœ ํ‘œ์‹œํ•  ์ˆ˜ ์—†๋Š” ์ž๋ฆฌ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
A: 10, B: 11, ..., F: 15, ..., Y: 34, Z: 35

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N๊ณผ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ B ≤ 36) N์€ 10์–ต๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— 10์ง„๋ฒ• ์ˆ˜ N์„ B์ง„๋ฒ•์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

ํ’€์ด

// ํ’€์ด : https://whkakrkr.tistory.com

#include <iostream>
#include <vector>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
	cout.tie(NULL);
	cin.tie(NULL);
	
	int n, b;
	cin >> n >> b;
	
	vector<int> remainder;
	while(n>1) {
	    remainder.push_back(n%b);
	    n /= b;
	}
	if(n==1) {
	    remainder.push_back(1);
	}
	
	int length = remainder.size();
	string ans = "";
	for(int i=length-1; i>=0; i--) {
	    int value = remainder[i];
	    
	    char c;
	    if(value>=10) {
	        c = value-10+'A';
	    } else {
	        c = value + '0';
	    }
	    
	    ans += c;
	}
	
	cout << ans;
	
    return 0;
}
๋ฐ˜์‘ํ˜•