Official

C - 三十六進法/Hexatridecimal Editorial by tatyam


\(N < 36^3\) であることから、答えは \(3\) 桁以下です。

\(36^0\) の位は \(N \bmod 36\)
\(36^1\) の位は \(\left\lfloor \frac{N}{36} \right\rfloor \bmod 36\)
\(36^2\) の位は \(\left\lfloor \frac{N}{36^2} \right\rfloor \bmod 36\)
で求めることができるので、これを \(36\) 進法の数字に変換して出力すればよいです。

先頭に余分な \(0\) を出力しないように気をつけてください。

回答例 (C++)

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

char base36(int x){
    if(x < 10) return '0' + x;
    return 'A' + x - 10;
}
int main(){
    int n;
    cin >> n;
    if(n == 0){
        cout << "0" << endl;
        return 0;
    }
    string ans;
    while(n){
        ans = base36(n % 36) + ans;
        n /= 36;
    }
    cout << ans << endl;
}

回答例 (Python)

def base36(x):
    if x < 10:
        return str(x)
    else:
        return chr(ord('A') + x - 10)

n = int(input())
if n == 0:
    print("0")
    exit()

ans = ""
while n:
    ans = base36(n % 36) + ans
    n //= 36
print(ans)

回答例 (Ruby)

puts gets.to_i.to_s(36).upcase

posted:
last update: