백준 algorithm

백준 11576 - Base Conversion

cosmohoo 2020. 4. 5. 23:15
반응형

문제 설명

 

 

 

 

=> 해당 문제는 원래의 A진법의 수를 10진법으로 바꾼 후, 정이가 사용하는 B진법으로 바꾸는 문제이다. 

=> A진법을 10진법으로 바꾸기 위해서 아래의 문제를 확인하면 된다. 

https://codingham.tistory.com/117

 

백준 2745 - 진법 변환

=> 진법 변환 2와 달리 B 진법으로 표시되어 있는 수를 10진법으로 변환하면 되는 문제이다. *** 참고 : https://codingham.tistory.com/116 백준 11005 - 진법 변환 2 => 간단한 진법 변환 문제이다. => 진법 변..

codingham.tistory.com

 

 

=>10진법을 어떠한 n 진법으로 바꾸기 위해서 아래의 문제를 확인하면 된다. 

https://codingham.tistory.com/116

 

백준 11005 - 진법 변환 2

=> 간단한 진법 변환 문제이다. => 진법 변환의 알고리즘을 알아야한다. ex) 11 을 3진법으로 나타내고자 한다. 11 / 3 = 3 ... 2(나머지) 3 / 3 = 1 ... 0 1/3 = 0 ... 1 =>의 결과로 102(3) 의 결과를 얻을 수..

codingham.tistory.com

 

 

***알고리즘
A진법 => 10진법 -> B진법

 

 

#include <iostream>
#include <algorithm>
#include <stack>
#include <cmath>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    int A, B;
    cin >> A >>B; // future, jung
    
    int n; // 자리수의 개수
    cin >> n;
    
    int tmp;
    int answer=0;
    for(int i=1; i<=n; ++i)
    {
        cin>>tmp;
        answer += (pow(A, n-i) * tmp);
    }
    
    stack<int> st;
    
    while(answer!=0)
    {
        st.push(answer%B);
               answer /=B;
    }
    while(!st.empty())
    {
        cout << st.top()<<" ";
        st.pop();
    }
    cout<<'\n';
   
    
    return 0;
}

 

 

 

 

 

 

실행 예
실행 예2

 

반응형