Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 700 Bytes

Ex_1_3_05.md

File metadata and controls

40 lines (30 loc) · 700 Bytes
title date draft tags categories
Algorithm4 Java Solution 1.3.05
2019-07-04 05:47:10 +0800
false
JAVA
TECH
archives

1.3.05

Problem:

What does the following code fragment print when N is 50? Give a high-level description of what it does when presented with a positive integer N.

Solution:

show the binary string representation of integer N.

  public static void showBinaryN(int N) {
    _VarySizedCapacityStack<Integer> stack = new _VarySizedCapacityStack<>();
    while (N > 0) {
      stack.push(N % 2);
      N = N / 2;
    }
    for (int d :
        stack) {
      StdOut.printf("%d", d);
    }
    StdOut.println();
  }

Reference: