0
点赞
收藏
分享

微信扫一扫

L1-002 打印沙漏 (20分)(Java版,带注释)

烟中雯城 2022-07-29 阅读 67


说是带点栈的思想?不太清楚(

L1-002 打印沙漏 (20分)(Java版,带注释)_java


L1-002 打印沙漏 (20分)(Java版,带注释)_java_02

import java.util.Scanner;
import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int num = input.nextInt();
String ch = input.next();
int i = 1, sum = 1, k = 0, cnt = 0;
int [] arr = new int[100];
while(sum <= num){
arr[k++] = i;
i += 2;
sum += i*2;

cnt++;

}
cnt--; //cnt == 2
sum = 0;
for (int f = 0; f <= cnt; f++){
sum += arr[f]*2;
}
sum--;

int lines = (arr[cnt] + 1)/2; //循环行数
// System.out.println(lines);
int temp = cnt;
for (int j = 0; j < lines; j++){
int temp2 = j;
while(temp2 != 0){
System.out.print(' ');
temp2--;
}

for (int h = 0; h < arr[temp]; h++){
System.out.print(ch);
}

if (temp == 0){

break;
}
temp--;
System.out.println();
}
if (lines != 1){
System.out.println();
}
lines = lines - 1; //下层行数比上层少一
temp = 1;
for (int q = lines - 1; q >= 0; q--){
int temp3 = q;
while(temp3 != 0){
System.out.print(' ');
temp3--;
}
for (int w = 0; w < arr[temp]; w++){
System.out.print(ch);
}
temp++;
if (temp > cnt){
break;
}
System.out.println();
}

System.out.println();
System.out.println(num-sum);
}

}

L1-002 打印沙漏 (20分)(Java版,带注释)_java_03


举报

相关推荐

0 条评论