-2

我知道这可能很简单,但我似乎真的不明白如何垂直写整数。例如,有一个数组有 4 个整数,分别是 9、21、63 和 501,结果如下

     9  2  5  6
        1  0  3
           1  

这是我程序的一小步,可能是最简单的一步,但我不明白该怎么做:(有人可以帮助我或指导我,这样我就可以完成我的程序

4

3 回答 3

1

试试这个伪代码

int[] list = new int[] {9,21,63,501};
bool finished = false;

if (list.Count > 0) {

   for (var j=0;!finshed; j++) {
      finished = true;

      for (var i = 0; i<list.Count;i++) {
         String val = list[i].ToString();
         if (val.length>j) {
            write(val.charAt(j));
            finished = false;
         }
      }
   }
}
于 2013-02-20T02:16:59.907 回答
1

我创建了一个非常模块化且易于遵循的解决方案。

编辑:转换digitAtIndex()为纯数值计算。
保留原件并调用它digitAtStrIndex()

public class IntegerColumns {

  public IntegerColumns() {
    int[] arr = new int[] {9, 21, 501, 63};
    printColumnMajorOrder(arr);
  }

  public static void main(String[] args) {
    new IntegerColumns();
  }

  // --------------------- Primary Functions --------------------------

  // Prints out an Array of Integers, each in a vertical column
  public void printColumnMajorOrder(int[] arr) {
    int cols = arr.length;
    int rows = maxDigits(arr);
    for (int r = 0; r < rows; r++) {
      for (int c = 0; c < cols; c++) {
        int d = digitAtIndex(arr[c], r);
        System.out.printf("%s\t", d >= 0 ? Integer.toString(d) : " ");
      }
      System.out.println();
    }
  }

  // Returns the length of an Integer
  public int numDigits(int i) {
    if (i <= 0) return 0;
    return (int)Math.floor(Math.log10(i))+1;
  }

  // Numeric calculation to find a digit at a specified index
  public int digitAtIndex(int num, int index) {
    int digits = numDigits(num);
    int deg = digits - index - 1;
    int pow = (int)Math.pow(10, deg);
    return pow > 0 ? (int)(num/pow)%10 : -1;
  }

  // Returns the number of digits for the longest Integer in an Array
  public int maxDigits(int[] arr) {
    int max = 0;
    for (int i : arr) {
      int size = numDigits(i);
      if (size > max) max = size;
    }
    return max;
  }

  // ---------------------- Extra Functions ---------------------------

  // Hybrid of Integer and Substrings - String manipulation = slow
  public int digitAtStrIndex(int number, int i) {
    String n = Integer.toString(number);
    return n.length() > i ? Integer.parseInt(n.substring(i, i+1)) : -1;
  }

  // Prints the digits of a number vertically
  public void printNumberVertical(int num) {
    for (int i = 0; i < numDigits(num); i++)
      System.out.println(digitAtIndex(num, i));
  }
}
于 2013-02-20T02:45:38.387 回答
0

`公共类 VerticalPrintService {

private int[] data;

public VerticalPrintService( int[] intArray ) {

    this.data = intArray;

}

public void printVertically(){
    int cols = data.length; // # of columns
    int rows = getRows(); // # of rows

    System.out.println("cols: " + cols);
    System.out.println("rows: " + rows);

    String[][] matrix = new String[rows][cols];
    int rowIndex = 0;
    int colIndex = 0;

    // populate 2d array
    for ( int i : data ) {
        String str = String.valueOf(i);
        for ( int j = 0; j < str.length(); j++ ) {
            matrix[rowIndex][colIndex] = String.valueOf(str.charAt(j));
            rowIndex++;
        }

        colIndex++;
        rowIndex = 0;
    }

    // print
    for ( int i = 0; i < rows; i++ ) {
        for ( int j = 0; j < cols; j++ ) {
            if ( null == matrix[i][j] ) {
                System.out.print("\t");
            } else {
                System.out.print( matrix[i][j] + "\t" );
            }
        }
        System.out.println();
    }
}

private int getRows(){

    int max = 0;
    for ( int i : data ) {
        int len = String.valueOf(i).length();
        if ( len > max ) {
            max = len;
        }
    }

    return max;
}

}`

在你的主要方法中

`public static void main(String[] args) {

int[] array = { 9, 53, 501, 90 };
VerticalPrintService vps = new VerticalPrintService(array);
vps.printVertically();

}`

于 2013-02-20T04:21:16.657 回答