原题链接:打印蛋糕
题目描述
思路
这个题比较简单,但刷题应力求把简单题刷精,在简单题中找到难题的影子。
- 使用集合接收多组数据
- 利用每组数据生成对应层数的“蛋糕”
- 生成时注意每层蛋糕中,层数、字符数目、空格数目的对应关系,即可轻松通过
注意:不要输出多余的空格,如每行字符后是没有空格的,否则答案不合法。
有其它问题可以在评论区交流。
代码实现
import java.util.ArrayList;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
ArrayList<Integer> array=new ArrayList<>();
while(scan.hasNextInt()){
array.add(scan.nextInt());
}
for (int N: array
) {
int space=N-1;
for (int i = 1; i <=N; i++,space--) {
for (int j = 1; j <=space ; j++) System.out.print(' ');
for (int j = 1; j <=2*i-1 ; j++) System.out.print('*');
System.out.println();
}
}
}
}