PAT(B)打印沙漏(20)

论坛 期权论坛 脚本     
匿名技术用户   2020-12-23 06:47   11   0

题目链接:https://www.nowcoder.com/pat/6/problem/4053

输入描述:

输入在一行给出1个正整数N(<=1000)和一个符号,中间以空格分隔。

输出描述:

首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。

输入例子:

19 *

输出例子:

*****

 ***

 *

 ***

*****

2

#include<iostream>
using namespace std;
int n;
char s;
//右侧不需要打印空格
int main(){
 cin>>n>>s;
 int row=1;
 while(2*row*row-1<=n){
  row++;
 }
 row--;
 //输出上半部分
 for(int i=row;i>=1;i--){
  for(int j=row-i;j>=1;j--){
   cout<<" ";
  }
  for(int j=2*i-1;j>=1;j--){
   cout<<s;
  }
  cout<<endl;
 } 
 //输出下半部分 
 for(int i=2;i<=row;i++){
  for(int j=row-i;j>=1;j--){
   cout<<" ";
  }
  for(int j=2*i-1;j>=1;j--){
   cout<<s;
  }
  cout<<endl;
 }
 cout<<n-2*row*row+1<<endl;
 return 0;
} 

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP