kyopro_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub dyktr06/kyopro_library

:heavy_check_mark: Convert Base (進数変換)
(lib/math/convert_base.hpp)

Convert Base (進数変換)

概要

10 進数での $x$ を $b$ 進数に変換します。

計算量

Verified with

Code

#pragma once

/**
 * @brief Convert Base (進数変換)
 * @docs docs/math/convert_base.md
 */

template<typename T>
string convertBase(T x, int b) {
    string res;
    T t = 1, k = abs(b);
    while(x){
        T num = (x * t) % k;
        if(num < 0) num += k;
        res += num + (num < 10 ? 48 : 87);
        x -= num * t;
        x /= k;
        t *= b / k;
    }
    if(res.empty()) res = '0';
    reverse(res.begin(), res.end());
    return res;
}
#line 2 "lib/math/convert_base.hpp"

/**
 * @brief Convert Base (進数変換)
 * @docs docs/math/convert_base.md
 */

template<typename T>
string convertBase(T x, int b) {
    string res;
    T t = 1, k = abs(b);
    while(x){
        T num = (x * t) % k;
        if(num < 0) num += k;
        res += num + (num < 10 ? 48 : 87);
        x -= num * t;
        x /= k;
        t *= b / k;
    }
    if(res.empty()) res = '0';
    reverse(res.begin(), res.end());
    return res;
}
Back to top page