解题思路:在我看来,本题的难点不在于打印方式,而在于接收字符串。字符串中的字符可以是空格,因此不能用next()来接收 ,应该用nextLine();另一方面,在接受完整数N时,会在键盘中输入换行符,因此还要注意对于此换行符对于接收字符串的影响,解决办法是在nextInt()后加入一个nextLine()。
import java.util.;
public class Main{
public static void main(String arg[]){
Scanner scan=new Scanner(System.in);
int N=scan.nextInt();
scan.nextLine();
String str=new String();
str=scan.nextLine();
int m=(N-1)/3;
int t1=0,t2=0;
int j=0;
for(int i=m-1;i>=0;i--){
while(t1>0) {
System.out.print(" ");
t1--;
}
t1=m-i;
System.out.print(str.charAt(j));
j++;
t2=2
i+1;
while(t2!=0){
System.out.print(" ");
t2--;
}
System.out.print(str.charAt(j));
j++;
System.out.println();
}
for(;j<N;j++){
t1=m;
while(t1>0){
System.out.print(" ");
t1--;
}
System.out.println(str.charAt(j));
}
}
}