알고리즘/BOJ(C++)
[BOJ] 11576 :: Base Conversion
쿠마쿠마34
2018. 6. 19. 22:32
반응형
https://www.acmicpc.net/problem/11576
a진수의 수와 b진수의 수가 입력이 들어오면 a진수의 수를 b진수의 수로 바꾸어서 출력해주는 문제입니다.
항상 진법변환문제가 그렇듯, 0인경우만 처리를 잘 해주면 됩니다.
다른 마땅한 방법이 생각나지 않아서 a진수를 decimal값으로 변환한 후 다시 그 값을 b진수로 변환해 주었습니다.
좀..어렵게 푼것같기도 하네요
#include <iostream>#include <stack>#include <cmath>using namespace std;int main(){int a, b;cin >> a >> b;int len;cin >> len;int decimal = 0;bool start = true;for (int i = len - 1; i >= 0; i--){int temp;cin >> temp;decimal += pow(a, i) * temp;}//cout << decimal << endl;if (decimal == 0){cout << "0" << endl;return 0;}stack<int> s;while (decimal != 0){s.push(decimal % b);decimal /= b;}while (!s.empty()){cout << s.top() << " ";s.pop();}
}
반응형