题目链接: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;
}
|