티스토리 뷰

공부

[Math] changeProposition?

승가비 2018. 9. 8. 18:15
728x90

[Math] changeProposition?


**진법을 10진법으로 변환한다. 

10 ~ 36 진법까지 가능하며, 알파벳 표현은 Z 까지다.


예제: https://www.acmicpc.net/problem/2745


// 진법변환
// 숫자를 나타내는 문자가 오고, 그 다음에 진법이 온다.
// 10 진법으로 숫자를 변경.

// input: ZZZZZ 36
// output: 60466175

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

int toNumber(char n) {
char base;
if(n >= '0' && n <= '9') {
base = '0';
} else if(n >= 'A' && n <= 'Z') {
base = 'A' - 10;
}

return n - base;
}

int main() {
string number;
int proposition;
cin >> number >> proposition;

int result = 0;
for(int i=0; i<number.length(); i++) {
result *= proposition;
result += toNumber(number.at(i));
}

cout << result << endl;

return 0;
}


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


// 진법변환2
// 숫자가 오고, 그 다음에 진법이 온다.
// 해당 진법으로 숫자를 변경.

// input: 60466175 36
// output: ZZZZZ

#include <iostream>
#include <stack>
using namespace std;

int toChar(int n) {
char base;
if(n >= 0 && n <= 9) {
base = '0';
} else if(n >= 10 && n <= 36) {
base = 'A' - 10;
}

return n + base;
}

int main() {
int number;
int proposition;
cin >> number >> proposition;

stack<char> result;
while(number != 0) {
int mod = number % proposition;
result.push(toChar(mod));
number /= proposition;
}

while(result.size()) {
cout << result.top();
result.pop();
}
cout << endl;

return 0;
}


728x90

'공부' 카테고리의 다른 글

[SQL] join?  (0) 2018.09.08
[SQL] where & orderBy & limit  (0) 2018.09.08
[Graph] Floyd Warshall?  (0) 2018.09.08
[Sort] 수열에서 자기 위치에 위치하는 원소가 있는지 확인?  (0) 2018.09.07
[Cache] fibonacci?  (0) 2018.09.07
댓글