牛客网PAT练兵场-打印沙漏
题目地址:https://www.nowcoder.com/pat/6/problem/4053
题意:模拟题
1 /** 2 * Copyright(c) 3 * All rights reserved. 4 * Author : Ycute 5 * Date : 2019-11-04-17.21.03 6 * Description : 模拟题 7 */ 8 #include<iostream> 9 #include<cstdio> 10 #include<cmath> 11 #include<cstring> 12 #include<algorithm> 13 using namespace std; 14 int main(){ 15 int n; 16 char ch; 17 int a[100];//存每一层字符数 18 int b[100];//存总共字符数 19 a[1]=1; 20 b[1]=1; 21 scanf("%d %c",&n,&ch); 22 for(int i=2;i<100;i++){ 23 a[i]=a[i-1]+2; 24 b[i]=a[i]+b[i-1]; 25 } 26 int MAX=1; 27 while((b[MAX]*2-1)<=n){ 28 MAX++; 29 } 30 //反序打印 31 int j=0; 32 for(int i=MAX-1;i>=1;i--){ 33 int temp=j++; 34 while(temp--){ 35 printf(" "); 36 } 37 for(temp=1;temp<=a[i];temp++){ 38 printf("%c",ch); 39 } 40 printf("\n"); 41 } 42 //顺序打印 43 j=j-2; 44 for(int i=2;i<=MAX-1;i++){ 45 int temp=j--; 46 while(temp--){ 47 printf(" "); 48 } 49 for(temp=1;temp<=a[i];temp++){ 50 printf("%c",ch); 51 } 52 printf("\n"); 53 } 54 printf("%d\n",n-(b[MAX-1]*2-1)); 55 return 0; 56 }