Java 以矩阵形式打印字符
下面的问题有一个字符列表和列数作为输入。列数不是一个常数,可以随每个输入而变化。 输出应使除最后一行之外的所有行都被完全占用Java 以矩阵形式打印字符,java,for-loop,matrix,Java,For Loop,Matrix,下面的问题有一个字符列表和列数作为输入。列数不是一个常数,可以随每个输入而变化。 输出应使除最后一行之外的所有行都被完全占用 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
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();
}
}
我试图用一个算法来打印正确的输出。我想知道解决这个问题有哪些不同的方法。时间和空间的复杂性并不重要。我尝试过的上述方法也是错误的,因为它将列作为参数,但实际上是作为行数
仅供参考:这不是家庭作业问题。这可能是家庭作业;所以我不打算为你做这件事,但我会给你一些开始的提示。这里有两点:
- 计算正确的行数
- 计算循环列表时所需的“模式”,以便打印预期结果
0 3 6
1 4 7
2 5
对不同的输入重复这样做;您很快就会发现需要“逐行”打印的索引模式;所以我不打算为你做这件事,但我会给你一些开始的提示。这里有两点:
- 计算正确的行数
- 计算循环列表时所需的“模式”,以便打印预期结果
0 3 6
1 4 7
2 5
对不同的输入重复这样做;您将很快发现“逐行”打印所需的索引模式。最终能够为这个问题设计算法 请参考下面相同的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();
}
}
}
公共类难题{
公共静态void main(字符串[]args){
字符串列表[]={“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”};
int列=3;
int rows=list.length/column;//计算整行总数
int lastRowElement=list.length%column;//标识最后一行中的元素数
如果(lastRowElement>0){
rows++;//将incomplete行添加到完全填充行的总数中
}
//迭代行
对于(int i=0;i 而(j
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();
}
}
}
公共类难题{
公共静态void main(字符串[]args){
字符串列表[]={“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”};
int列=3;
int rows=list.length/column;//计算整行总数
int lastRowElement=list.length%column;//标识最后一行中的元素数
如果(lastRowElement>0){
rows++;//将incomplete行添加到完全填充行的总数中
}
//迭代行
对于(int i=0;i
虽然(j