【问题标题】:Print characters as a Matrix将字符打印为矩阵
【发布时间】:2017-03-30 06:25:57
【问题描述】:

下面的问题有一个字符列表和列数作为输入。列数不是一个常数,可以随每个输入而变化。 除了最后一行之外,输出的所有行都应该被完全占用。

list: a b c d e f g
colums: 3

Wrong:
a b c
d e f
g

Wrong:
a d g
b e
c f

Correct:
a d f
b e g
c

我在下面尝试过:

public static void printPatern(List<Character> list, int cols) {

    for (int i = 0; i < cols; i++) {

        for (int j = i; j < list.size(); j += cols) {
            System.out.print(list.get(j));

        }
        System.out.println();
    }
}

它给出的输出为(这是错误的):

a d g
b e
c f

我正在尝试使用一种算法来打印正确的输出。我想知道解决这个问题的不同方法是什么。时间和空间的复杂性无关紧要。我尝试的上述方法也是错误的,因为它以列作为参数,但实际上是作为行数。

仅供参考:这不是家庭作业问题。

【问题讨论】:

  • 您的要求输出没有任何意义?
  • 行号是否恒定为 3?
  • 不。这是列数,它可能会有所不同。
  • 好的,这是一个棘手的算法。会尽快回复您
  • @Dhiraj 看来真的不明白这个问题。需要根据列表的长度和所需的列数计算行数。

标签: java for-loop matrix


【解决方案1】:

终于可以针对这个问题设计算法了 请参考下面的java代码相同

 public class puzzle{
        public static void main(String[] args){

            String list[] = { "a", "b", "c","d","e","f","g","h","i","j" };
            int column = 3;

            int rows = list.length/column; //Calculate total full rows
            int lastRowElement = list.length%column;//identify number of elements in last row
            if(lastRowElement >0){
                rows++;//add inclomplete row to total number of full filled rows
            }
            //Iterate over rows
             for (int i = 0; i < rows; i++) {
                int j=i;
                int columnIndex = 1;
                while(j < list.length && columnIndex <=column ){
                    System.out.print("\t"+list[j]);
                    if(columnIndex<=lastRowElement){
                        if(i==rows-1 && columnIndex==lastRowElement){
                            j=list.length; //for last row display nothing after column index reaches to number of elements in last row
                        }else{
                            j += rows; //for other rows if columnIndex is less than or equal to number of elements in last row then add j value by number of rows
                        }

                    }else {
                       if(lastRowElement==0){
                          j += rows;
                       }else{
                        j += rows-1; //for column greater than number of element in last row add j = row-1 as last row will not having the column for this column index.
                       }
                    }

                    columnIndex++;//Increase column Index by 1;
                }

                System.out.println();
                }
        }
    }

【讨论】:

  • 它适用于奇数元素,但不适用于偶数。像列表 = a,b,c,d,e,f 和列 = 2。
  • 谢谢 Dhiraj。我很感激。
【解决方案2】:

这可能是家庭作业;所以我不会为你做这件事,但给你一些提示让你开始。这里有两点:

  • 计算正确的行数
  • 计算循环列表时所需的“模式”,以便打印预期结果

对于第一部分,您可以查看模运算;第二部分:开始“在纸上”迭代您的列表,并观察您如何手动打印正确的结果。

显然,第二部分更为复杂。如果您意识到“逐列”打印是直截了当的,这可能会有所帮助。因此,当我们采用您的正确示例并打印 indexes 而不是值时,您会得到:

0 3 6
1 4 7
2 5

针对不同的输入重复执行此操作;您很快就会发现需要“逐行”打印的索引模式。

【讨论】:

  • 我也试过这种方式。这里的问题是,奇数个元素的模式是:0 3 5, 1 4 6, 2 而对于偶数个元素,它就像:0 2 4, 1 3 5元素很奇怪。
  • 当然。因为它不像只是添加一个常量值那么简单。这里起作用的因素是您必须在最后一行中打印的元素数量。换句话说:为任何行选择要打印的列表索引的函数也取决于该值(不仅取决于列数!)
猜你喜欢
  • 2015-04-10
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-25
相关资源
最近更新 更多