Skip to content

Instantly share code, notes, and snippets.

@kenornotes
Created January 16, 2015 08:50
#include <stdio.h>
void print(int val, int base) {
int p = 0, i, out[1000]; //out: 儲存結果的陣列, p: 計算位數
for ( ; val > 0; val /= base ) { //以 ascii code 將val % base 儲存在out內
out[p++] = val % base > 9 ? val % base + 55 : val % base + 48 ;
for ( i = p - 1; i >= 0; i-- ) //從後面開始將每個字元印出來
printf("%c", out[i]);
printf("\n");
}
int main() {
int x, base;
scanf( "%d %d", &x, &base );
print( x, base );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment