当前位置:网站首页>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);
}
边栏推荐
猜你喜欢

左对齐,右对齐,随机数,goto,比较输出bool

【LDA】基础知识笔记——主要是AE、VAE
![JS (I) error [err\u module\u not\u found]: cannot find package 'UUID' imported](/img/a4/ef2d73576e027a2179ec9251167fa4.jpg)
JS (I) error [err\u module\u not\u found]: cannot find package 'UUID' imported

Easygui query the company name in the document

Function recursion example

Assertion of selenium webdriver

C 字符串

MH32F103ARPT6软硬件兼容替代STM32F103RCT6

selenium-webdriver之断言

ADB command (2) use monkey to test
随机推荐
Pointer related concepts
JMeter (V) pressure test of Excel file upload interface
指针相关概念
三维重建系统 | L3增量运动恢复结构(增量SFM)
数据的收集
selenium之元素定位
tc菜单分割
Left aligned, right aligned, random number, goto, compare output bool
Detailed explanation of factory pattern (simple factory pattern, factory method pattern, abstract factory pattern) Scala code demonstration
安凯微电子冲刺科创板:年营收5亿 小米产业基金是股东
Unit test (I) unit test with JUnit
启明智显分享| 2.8寸手持中控屏应用方案
【Environment】1. Get the configuration in YML through the environment in the configuration class
Swap numbers, XOR, operator correlation
JS (III) convert ES6 syntax to Es5 syntax
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Autofac Beginner (1)
【LDA】基础知识笔记——主要是AE、VAE
数组相关内容
Shardingsphere practice (6) - elastic scaling