2. 考虑以下变化: 作为一种投入,一些行文和插手,写在一行背后和整个行各处。 例如,提供的投入
请将我改为定制的齐格扎格印刷格式
我们用三行书写,这样,我们就这样说:
P s n t o s i z a i n r
l a e o v r m t a u t m z d i z g r n i g o m t
e c e e c o e g p t f a
(please dont edit this print out, this is required output, it is not a real zigzag, it is a customized zigzag)
最后,我们召集各行各业的竞赛,以获得由此造成的扼杀。
Psntosizainrlaeovrmtautmzdizgrnigomteceecoegptfa
我在此用假编码写出关于这个问题的算法。
it is asking about the relationship between {index} and {output row and col}
Def:
down = false;
up = false;
r=0;
c=0;
char[][] output;
there are three cases here:
case 1:
index % 4 == 0, it is the beginning of a down printing
r = 0;
output[r][c] = in.charAt(index);
c++;
down = true; up = false;
case 2:
index%4 != 0 && down = true;
r = index % 4;
output[r][c] = in.charAt(index);
if( r == 2){ up = true; down = false; c++;} // when come to the last row of a down formatting
case 3:
index%4 != 0 && up == true;
r = 4- index%4;
output[r][c] = in.charAt(index);
c++;
一般性解决办法,即可将4改为n Rows+1
;
虽然我认为这项工作是好的,但我对此感到不快。 是否有更明确或更快的算法来解决这一问题?