Created
January 16, 2015 08:45
-
-
Save kenornotes/eb5a39ce3d6057370272 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void print(int val, int base) { | |
// 把原始值複製一份(tempVal),除一次看結果會有幾位(digit) | |
int digit = 0, tempVal = val; | |
while( tempVal > 0 ) { | |
tempVal /= base; | |
digit++; | |
} | |
// 讓一位可以存一個位址,並依序取餘數後存起來 | |
int i, result[digit]; | |
for(i = 0; i < digit; i++) { | |
result[i] = val % base; | |
val /= base; | |
} | |
// 反過來輸出,當數字超過 9 的話,以大寫英文輸出 | |
for(i = digit-1; i >= 0; i--) { | |
// 因 'A' = 65, 'B' = 66, 'C' = 67...,因此當值大於 9 ,就將它加上 55 以字元輸出 | |
if(result[i] > 9) | |
printf("%c", result[i] + 55); | |
else | |
printf("%d", result[i]); | |
} | |
printf("\n"); | |
} | |
int main() { | |
int n, base; | |
scanf("%d%d", &n, &base); | |
print(n, base); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment