Love丶FFC's Blog

PAT乙级:1027 打印沙漏

2020-02-18 09:59:06
阅读:688   •   评论:11
标签:,

本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印

*****
 ***
  *
 ***
*****

所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。

给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。

输入格式:

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

输出格式:

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

输入样例:

19 *

输出样例:

*****
 ***
  *
 ***
*****
2

编程语言:Python

解题思想:

1.判断所能生成的沙漏的最大层数

2.按顺序输出沙漏

3.对每一行进行左缩进,也就是实现“居中”,满足题目要求的格式

时间复杂度:O(N)

代码如下:

  1. NOA, Symbol = input().split() # 输入符号个数、符号
  2. NOA = int(NOA) # 将字符型转为整型
  3.  
  4. MaxLayer = 1 # 沙漏的层数
  5. NOA -= 1 # 减去最中间那一层
  6.  
  7. # 计算最多有几层
  8. while NOA - 2 * (MaxLayer + 2) >= 0: # 所剩符号数可以再延伸2层
  9. NOA -= 2 * (MaxLayer + 2)
  10. MaxLayer += 2
  11.  
  12. NowLayer = MaxLayer # 沙漏上半部
  13. while NowLayer >= 3:
  14. String = (MaxLayer - NowLayer) // 2 * ' ' + NowLayer * Symbol # 将每一层都进行左缩进,实现居中
  15. print(String)
  16. NowLayer -= 2
  17.  
  18. print((MaxLayer - NowLayer) // 2 * ' ' + Symbol) # 沙漏中部,左缩进,实现居中
  19.  
  20. NowLayer = 3 # 沙漏下半部
  21. while NowLayer <= MaxLayer:
  22. String = (MaxLayer - NowLayer) // 2 * ' ' + NowLayer * Symbol # 将每一层都进行左缩进,实现居中
  23. print(String)
  24. NowLayer += 2
  25.  
  26. print(NOA) # 输出剩余符号数

评论板

共有 11 条评论

  1. Jorcift

    Hepatic toxicity 3 rogaine or propecia

  2. cheap louis vuitton online

    I just wanted to thank you for the fast service. or simply they look great. I received them a day earlier than expected. themselves I will definitely continue to buy from this site. manner in which I will recommend this site to my friends. Thanks!
    cheap louis vuitton online https://www.bestlouisvuittonoutlet.com/

  3. louis vuitton outlet online

    I just wanted to thank you for the fast service. or perhaps a they look great. I received them a day earlier than expected. since the I will definitely continue to buy from this site. either way I will recommend this site to my friends. Thanks!
    louis vuitton outlet online https://www.louisvuittonsoutletstore.com/

  4. cheap jordans

    I just wanted to thank you for the fast service. or they look great. I received them a day earlier than expected. similar to I will definitely continue to buy from this site. blue jays I will recommend this site to my friends. Thanks!
    cheap jordans https://www.cheaprealjordan.com/

  5. cheap real jordans

    I just wanted to thank you for the fast service. in addition they look great. I received them a day earlier than expected. most notably the I will definitely continue to buy from this site. in either case I will recommend this site to my friends. Thanks!
    cheap real jordans https://www.realcheapretrojordanshoes.com/

  6. cheap jordan shoes

    Read reviews and was a little hesitant since I had already inputted my order. or perhaps but thank god, I had no issues. appreciate the received item in a timely matter, they are in new condition. direction so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordan shoes https://www.cheapjordansstore.com/

  7. authentic cheap jordans

    Read reviews and was a little hesitant since I had already inputted my order. and even but thank god, I had no issues. prefer received item in a timely matter, they are in new condition. situation so happy I made the purchase. Will be definitely be purchasing again.
    authentic cheap jordans https://www.realjordansretro.com/

  8. cheap jordans for sale

    Read reviews and was a little hesitant since I had already inputted my order. or maybe a but thank god, I had no issues. including received item in a timely matter, they are in new condition. manner in which so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans for sale https://www.realjordansshoes.com/

  9. intuido

    medicamento priligy estudios clinicos crizotinib and thiothixene both increase QTc interval

  10. intuido

    cialis lamictal ilac nedir The great thing about feminism is that as a female, I can weigh in without being told my experience doesn t qualify me to be able to speak generic propecia Anderson Cancer Center, 1515 Holcombe Blvd

  11. IFBlNw

    overnight cialis delivery Section of Cardiovascular Diseases, Department of Experimental and Applied Medicine, University of Brescia, Brescia, Italy

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:497457