Love丶FFC's Blog

PAT乙级:1036 跟奥巴马一起编程

2020-02-26 11:51:17
阅读:608   •   评论:9
标签:,

美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统。2014 年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正方形。现在你也跟他一起画吧!

输入格式:

输入在一行中给出正方形边长 N(3≤N≤20)和组成正方形边的某种字符 C,间隔一个空格。

输出格式:

输出由给定字符 C 画出的正方形。但是注意到行间距比列间距大,所以为了让结果看上去更像正方形,我们输出的行数实际上是列数的 50%(四舍五入取整)。

输入样例:

10 a

输出样例:

aaaaaaaaaa
a        a
a        a
a        a
aaaaaaaaaa

编程语言:Python

解题思想:

1.先求出行数和列数

2.循环输出字符,首行、末行输出整行,其它行只输出首列、末列,中间用空格补齐

时间复杂度:O(N^2)

代码如下:

  1. SideLength, Character = input().split() # 输入边长及字符
  2. SideLength = int(SideLength) # 将字符型转为整型
  3.  
  4. ColumnLength = SideLength # 列数,等于边长
  5. RowLength = float(SideLength) * 0.5
  6.  
  7. # 四舍五入算法
  8. if RowLength - int(RowLength) >= 0.5: # 小数值大于等于0.5
  9. RowLength += 1
  10. RowLength = int(RowLength)
  11.  
  12. for i in range(RowLength): # 按行输出
  13. if i == 0 or i == RowLength - 1: # 首行或末行
  14. for j in range(ColumnLength): # 输出整行
  15. print(Character, end='')
  16. else:
  17. print(Character, end='')
  18. print((ColumnLength - 2) * ' ', end='') # 补足中间的空格
  19. print(Character, end='')
  20. print() # 换行

评论板

共有 9 条评论

  1. fruinly

    Shin A, Shu XO, Cai Q, Gao YT, Zheng W generic cialis no prescription

  2. original louis vuittons outlet

    I just wanted to thank you for the fast service. or possibly they look great. I received them a day earlier than expected. particularly I will definitely continue to buy from this site. you decide I will recommend this site to my friends. Thanks!
    original louis vuittons outlet https://www.louisvuittonsoutletstore.com/

  3. cheap louis vuitton outlet

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

  4. cheap retro jordans

    I just wanted to thank you for the fast service. to they look great. I received them a day earlier than expected. much like the I will definitely continue to buy from this site. an invaluable I will recommend this site to my friends. Thanks!
    cheap retro jordans https://www.realcheapretrojordanshoes.com/

  5. cheap retro jordans

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

  6. FoorryNox

    where to buy cialis I know how badly you would like to be able to restore her quality of life

  7. cheap retro jordans

    Read reviews and was a little hesitant since I had already inputted my order. as well but thank god, I had no issues. cherish the received item in a timely matter, they are in new condition. you ultimately choose so happy I made the purchase. Will be definitely be purchasing again.
    cheap retro jordans https://www.cheapauthenticjordanshoes.com/

  8. cheap jordans online

    Read reviews and was a little hesitant since I had already inputted my order. alternatively but thank god, I had no issues. similar to the reduction received item in a timely matter, they are in new condition. in either case so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans online https://www.realjordansshoes.com/

  9. Preliaf

    Malinoski, DJ, Slater, MS, Mullins, RJ propecia success rate In models of chemoresistant breast and ovarian cancer, ABC29460 decreases cell survival in a dose dependent manner

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名: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 -

已运行:

访问量:488796