반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. 구현


n이 0이 될 때 까지 while문을 돌면서 10진수 -> m진수로 변환을 해준다.


이때 n % m을 한 tmp값에 있는 것이 m진수로 변환했을때의 값이 된다. 


마지막으로 이 값을 reverse를 한번 취해주면 정답을 구할 수 있다.






소스 코드 : 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
 
using namespace std;
 
string tmp = "0123456789ABCDEF";
 
int main()
{
    int n, m;
    scanf("%d %d"&n, &m);
 
    if (n == 0)
        return !printf("0");
    string str = "";
    while (n)
    {
        str += tmp[n % m];
        n /= m;
    }
    reverse(str.begin(), str.end());
    printf("%s", str.c_str());
    return 0;
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus

반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[15729번] 방탈출  (0) 2018.05.22
[1933번] 스카이라인  (2) 2018.05.11
[14911번] 궁합 쌍 찾기  (0) 2018.05.07
[10174번] 팰린드롬  (2) 2018.05.04
[13275번] 가장 긴 팰린드롬 부분 문자열  (2) 2018.05.03