简体   繁体   English

右下乘法表

[英]Lower Right Multiplication Table

I'm writing a code to output the bottom right of a multiplication table...I can get the bottom left and the top right, but I get stuck trying to get the bottom right. 我正在编写代码以输出乘法表的右下角...我可以左下角和右上角,但是我在尝试获得右下角时遇到了麻烦。 I'm assuming that column needs to = 12, but whenever I try to input this, it gives me an error or the same answer as before. 我假设该列需要= 12,但是每当我尝试输入此列时,它都会给我一个错误或与以前相同的答案。 The output is supposed to look like the n's at the beginning of the code. 输出应该看起来像代码开头的n。

     n
    nn
   nnn

public static void main(String[] args) {
    //   // part 3: lower-triangular multiplication table
    for (int row=1; row<=12; row++) {
        for (int column=1; column <= row; column++)   
            System.out.printf("%5d", row*column);
        for (int column = row; column <= 0; column++)
        System.out.printf("%3c", ' ');
        System.out.println();

    }

To print the bottom (or top) right of a table, we need to iterate over the full table and omit entries 要打印表格的底部(或顶部),我们需要遍历整个表格并省略条目

The entries omitted are replaced with whitespace so that the other entries that are printed will line up correctly 省略的条目将替换为空格,以便其他已打印的条目将正确对齐

To determine if an entry is in a specific area, we can compare the X ( col ) and Y ( row ) values against the size ( size ) of the table 为了确定条目是否在特定区域中,我们可以将X( col )和Y( row )值与表的大小( size )比较

 1 2 ... X
1
2
...
Y

Top left includes (1,1), (1,Y), and (X,1), but not (X,Y) - X+Y < size+2 (+2 comes from 1 indexed vs 0 indexed) 左上角包括(1,1),(1,Y)和(X,1),但不包括(X,Y) X+Y < size+2 (+2来自1索引vs 0索引)

Bottom left includes (1,1), (1,Y), and (X,Y), but not (X,1) - X <= Y 左下角包括(1,1),(1,Y)和(X,Y),但不包括(X,1) X <= Y

Top right includes (1,1), (1,Y), and (X,1), but not (X,Y) - X >= Y 右上角包括(1,1),(1,Y)和(X,1),但不包括(X,Y) X >= Y

Bottom right includes (1,Y), (X,1), (X,Y), but not (1,1) - size-X < Y 右下角包括(1,Y),(X,1),(X,Y),但不包括(1,1) size-X < Y

Code: 码:

int size = 12; //size of the multiplication table
for(int row = 1; row <= size; row++) //go row by row
{
    for(int col = 1; col <= size; col++) //go column by column
        if(size - col < row) //in the bottom right area
            System.out.printf("%5d", row * col); //print the number
        else
            System.out.print("     "); //print whitespace for formatting
    System.out.println(); //end of the row, go to the next line
}

Outputs: 输出:

Size=4 大小= 4

               4
          6    8
     6    9   12
4    8   12   16

Size=12 大小= 12

                                                       12
                                                  22   24
                                             30   33   36
                                        36   40   44   48
                                   40   45   50   55   60
                              42   48   54   60   66   72
                         42   49   56   63   70   77   84
                    40   48   56   64   72   80   88   96
               36   45   54   63   72   81   90   99  108
          30   40   50   60   70   80   90  100  110  120
     22   33   44   55   66   77   88   99  110  121  132
12   24   36   48   60   72   84   96  108  120  132  144

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM