当前位置:网站首页>PTA:自测-1 打印沙漏 (20分)
PTA:自测-1 打印沙漏 (20分)
2022-06-12 14:51:00 【苏木George】
自测-1 打印沙漏 (20分)
本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
*****
***
*
***
*****
所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。
给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。
输入格式:
输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。
输出格式:
首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。
输入样例:
19 *
输出样例:
*****
***
*
***
*****
2
基本思路:
首先计算出给定的数字,能组成一个几行的图形,通过推导式m=2n^2-1判断;
然后把输出结果分成两部分输出,先通过循环输出第一部分,同样的规律输出第二部分。
#include"stdio.h"
#include"math.h"
using namespace std;
int main(){
int n =0;
char c;
scanf("%d %c",&n,&c);
int i=0;
// 计算层数
for (i=1;(2*i*i-1)<=n;i++);
int m =i--;
// 算出最后剩下的符号数
int rest = n-2*i*i+1;
// 第一部分可以分为包括一个符号在内的上面部分。然后可以得知他为n层,外层循环控制层数。内
//内层循环控制空格数和符号数,m定义为i。但他和层数之间的查大于零时就可以得到输出空格。
for(int j=i;j>=0;j--){
m=i;
for(int z=1;z<=j*2-1;z++){
if(m-j >0)
{
printf(" ");
z--;
m--;
}else {
printf("%c",c);
}
}
if((j-1)!=0)
printf("\n");
}
// 同理输出后两层
for(int j=2;j<=i;j++){
m=i;
for(int z=1;z<=j*2-1;z++){
if(m-j >0)
{
printf(" ");
z--;
m--;
}else {
printf("%c",c);
}
}
printf("\n");
}
printf("%d",rest);
}
边栏推荐
- selenium之元素定位
- Easygui query the company name in the document
- 【LocalDate LocalTime LocalDateTime】1. Using immutability to achieve thread safety 2 Current date, current time, current date time 3 Since the time zone is not considered, you need to add 8 hours to th
- 能链智电登陆纳斯达克:贝恩是股东 成中国充电服务第一股
- Location (I) error: command erred out with exit status
- 程序构造和解释之第一章习题
- Ankai microelectronics rushes to the scientific innovation board: the annual revenue of 500million Xiaomi industry fund is the shareholder
- 数组相关内容
- C string
- [spark][core] what is an external shuffle service?
猜你喜欢
随机推荐
C常量,不能改变
Unit test (I) unit test with JUnit
Junit测试中常用的断言
模块八
ADB command (I) record
Use Baidu AIP to obtain the text in the specified area of the screen
能链智电登陆纳斯达克:贝恩是股东 成中国充电服务第一股
Appnium (I) basic use of appnium
Tu oses le croire? Il m'a fallu deux jours pour développer un système de gestion.
Mysql之索引和视图
【Environment】1. Get the configuration in YML through the environment in the configuration class
Browser fingerprint interpretation
Installation and use of mat
ADSL
Swap numbers, XOR, operator correlation
Jenkins' RPC test project
【Instant】1. Equivalent to date class 2 Represents a moment
结构体示例
How to add WWW to the domain name
Autofac初学(1)

![[wechat applet] 4 Introduction to wechat developer tools](/img/9d/0d6c5cc675fb70dde98b25649bd8d8.jpg)

![[wp][beginner level] attack and defense world game](/img/07/1ea54d14ba3caca25a68786d5be4a6.png)




![[writeup]buu SQL course1[entry level]](/img/eb/1b2541b04ca231cb07f1f3706f51c7.png)
