当前位置:网站首页>B1027 打印沙漏
B1027 打印沙漏
2022-07-27 05:01:00 【叶辰 .】
1027 打印沙漏 (20 分)
本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
*****
***
*
***
*****
所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。
给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。
输入格式:
输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔
输出格式:
首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。
输入样例
19 *
输出样例
*****
***
*
***
*****
2
题目分析:
- 找到其中的数学规律即倒三角行数k满足2kk-1等于图形的点数
- 通过k值绑定循环进行打印
代码如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
char a;
cin>>n>>a;
int k=1;
while(k*k*2-1<=n){
k++;
}
k--;
for(int i=k;i>0;i--){
for(int m=0;m<k-i;m++){
cout<<" ";
}
for(int j=0;j<2*i-1;j++){
cout<<a;
}
cout<<endl;
}
for(int i=1;i<k;i++){
for(int m=0;m<k-i-1;m++){
cout<<" ";
}
for(int j=0;j<2*i+1;j++){
cout<<a;
}
cout<<endl;
}
int x=n-k*k*2+1;
(x)?printf("%d",x):printf("0");
return 0;
}
边栏推荐
- JVM上篇:内存与垃圾回收篇九--运行时数据区-对象的实例化,内存布局与访问定位
- JVM Part 1: memory and garbage collection part 14 -- garbage collector
- Use of file i/o in C
- Another skill is to earn 30000 yuan a month+
- Acceptance and neglect of events
- Network protocol details: IP
- Mysql表的约束
- Solution and principle analysis of feign call missing request header
- 34. 分析flexible.js
- Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
猜你喜欢

Acticiti中startProcessInstanceByKey方法在variable表中的如何存储

How to sinicize the JMeter interface?

How to create an applet project

JVM上篇:内存与垃圾回收篇十二--StringTable

来自“飞人”乔丹的启示!奥尼尔开启的另一个“赛场”

File processing (IO)

QT menu bar, toolbar and status bar

Inspiration from "flying man" Jordan! Another "arena" opened by O'Neill
![[error reporting] cannot read property 'parsecomponent' of undefined](/img/54/8d4225ec596d6b78348b181a3e636f.png)
[error reporting] cannot read property 'parsecomponent' of undefined

JVM上篇:内存与垃圾回收篇九--运行时数据区-对象的实例化,内存布局与访问定位
随机推荐
Be diligent in talking about what sidelines you can do now
Sunyanfang, co-founder of WeiMiao: take compliance as the first essence and become the "regular army" of financial and business education
35.滚动 scroll
Event filter
feign调用丢失请求头问题解决及原理分析
再一个技巧,每月稳赚3万+
牛客剑指offer--JZ12 矩阵中的路径
Could not autowire.No beans of ‘userMapper‘ type found.
Introduction to dynamic memory functions (malloc free calloc realloc)
Tcp server是如何一个端口处理多个客户端连接的(一对一还是一对多)
智慧展厅设计的优势及适用行业分析
事件(event)
How to copy Photoshop layers to other documents
QT 菜单栏、工具栏和状态栏
事件总结-常用总结
[acwing] solution to the 61st weekly match
What about PS too laggy? A few steps to help you solve the problem
How to test the payment process?
MySQL download and installation & perfect uninstall
"Photoshop2021 introductory tutorial" flatten "perspective images