Last active
November 11, 2021 01:34
-
-
Save yanfeng42/cada0dd0f6ba0a3933e3e39f9fde9f4e to your computer and use it in GitHub Desktop.
algs4 1.3.5 简易的 数字进制转换 算法
This file contains hidden or 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
package life.yanfeng.study.algorithm; | |
import edu.princeton.cs.algs4.StdOut; | |
// for 1.3.5 | |
public class StackPrintCase { | |
public static void printCase(Integer N, int base) { | |
Stack<Integer> stack = new Stack<>(); | |
while (N > 0) { | |
stack.push(N % base); | |
N = N / base; | |
} | |
for (int d : | |
stack) { | |
StdOut.print(d); | |
} | |
StdOut.println(); | |
} | |
public static void main(String[] args) { | |
/* | |
实际使用时, 16进制要适当处为使用 字母 替代显示. | |
output: | |
10000000000 | |
2000 | |
1024 | |
400 | |
*/ | |
printCase(1024, 2); | |
printCase(1024, 8); | |
printCase(1024, 10); | |
printCase(1024, 16); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment