Love丶FFC's Blog

PAT乙级:1026 程序运行时

2020-02-17 21:47:15
阅读:1021   •   评论:9
标签:,

要获得一个 C 语言程序的运行时间,常用的方法是调用头文件 time.h,其中提供了 clock() 函数,可以捕捉从程序开始运行到 clock() 被调用时所耗费的时间。这个时间单位是 clock tick,即“时钟打点”。同时还有一个常数 CLK_TCK,给出了机器时钟每秒所走的时钟打点数。于是为了获得一个函数 f 的运行时间,我们只要在调用 f 之前先调用 clock(),获得一个时钟打点数 C1;在 f 执行完成后再调用 clock(),获得另一个时钟打点数 C2;两次获得的时钟打点数之差 (C2-C1) 就是 f 运行所消耗的时钟打点数,再除以常数 CLK_TCK,就得到了以秒为单位的运行时间。

这里不妨简单假设常数 CLK_TCK 为 100。现给定被测函数前后两次获得的时钟打点数,请你给出被测函数运行的时间。

输入格式:

输入在一行中顺序给出 2 个整数 C1 和 C2。注意两次获得的时钟打点数肯定不相同,即 C1 < C2,并且取值在 [0,10​7​​]。

输出格式:

在一行中输出被测函数运行的时间。运行时间必须按照 hh:mm:ss(即2位的 时:分:秒)格式输出;不足 1 秒的时间四舍五入到秒。

输入样例:

123 4577973

输出样例:

12:42:59

编程语言:Python

解题思想:

1.思考一个算法,提供秒,可以得到对应的小时数、分钟数、秒数

2.对秒数进行四舍五入

3.不足2位的输出需要补0

时间复杂度:O(1)

代码如下:

  1. Start, End = map(int, input().split()) # 输入起始和结束时间
  2.  
  3. # 获取小时、分钟、秒的算法,/100的打点常数获得具体的秒数
  4. Hour = int((End - Start) / 100 / 3600)
  5. Minute = int((End - Start) / 100 % 3600 / 60)
  6. Seconds = (End - Start) / 100 % 3600 % 60
  7.  
  8. if Seconds - int(Seconds) >= 0.5: # 实现四舍五入,例如42.6秒,通过int转换后为42秒
  9. Seconds += int(Seconds) + 1 # 转为43秒
  10.  
  11. # 补足0,例如1:26:3需要补足为01:26:03
  12. Hour = (2 - len(str(Hour) + '')) * '0' + str(Hour)
  13. Minute = (2 - len(str(Minute) + '')) * '0' + str(Minute)
  14. Seconds = (2 - len(str(Seconds) + '')) * '0' + str(Seconds)
  15.  
  16. print(Hour + ':' + Minute + ':' + Seconds + '')
  17.  

评论板

共有 9 条评论

  1. cheap louis vuitton online

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

  2. cheap jordans online

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

  3. cheap retro jordans

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

  4. authentic louis vuitton outlet

    I just wanted to thank you for the fast service. in addition they look great. I received them a day earlier than expected. particularly I will definitely continue to buy from this site. you ultimately choose I will recommend this site to my friends. Thanks!
    authentic louis vuitton outlet https://www.bestlouisvuittonoutlet.com/

  5. louis vuitton outlet online

    Read reviews and was a little hesitant since I had already inputted my order. also but thank god, I had no issues. the same as received item in a timely matter, they are in new condition. either way so happy I made the purchase. Will be definitely be purchasing again.
    louis vuitton outlet online https://www.cheaplouisvuittonsale.com/

  6. cheap jordans online

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

  7. Gealpaste

    priligy dapoxetine buy For patients with symptom benefit, improvement was evident in 85 of patients 4 weeks after starting gefitinib

  8. intuido

    levitra achat 3 Thus, 6 of diuretic treated patients are suffering from a second, thiazide induced, iatrogenic disorder ie, K depletion that more than cancels out the primary purpose of this treatment, to protect from later cardiac morbidity

  9. JwarrwIJs

    Purging breakouts look like tiny, red bumps on the skin that are painful when touched cialis 5 mg It acts on the hypothalamic thermoregulatory center, inhibiting the synthesis of prostaglandins and the effects of endogenous pyrogen, leading to peripheral vasodilation, increased blood flow to the skin and increased sweating, which contribute to heat loss

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

已运行:

访问量:423766