python时间转换、给定一个以秒为单位的时间t_题目 1470:【蓝桥杯】【入门题】【基础练习VIP】时间转换...

2023-05-16

题目 1470:字符逆序蓝桥杯刷题群已成立,微信后台回复【蓝桥杯】,即可进入。

如果加入了之前的社群不需要重复加入。

时间限制: 1Sec 内存限制: 128MB

1. 题目描述

给定一个以秒为单位的时间t,要求用  “< H> :< M> :< S> ”的格式来表示这个时间。< H> 表示小时,< M> 表示分钟,  而< S> 表示秒,它们都是整数且没有前导的“0”。例如,若t=0,则应输出是“0:0:0”;若t=3661,则输出“1:1:1”。

2. 输入

输入只有一行,是一个整数t(0< =t< =86399)。

3. 输出

输出只有一行,是以“:: ”的格式所表示的时间,不包括引号。

4. 样例输入5436

5. 样例输出1:30:36

6. 解决方案

思路:1分钟等于60秒,1小时等于3600秒。

「Python语言」while True:

try:

t = int(input())

h = t // 3600

a = t % 3600

m = a // 60

s = a % 60

print('{0}:{1}:{2}'.format(h, m, s))

except:

break

知识点

1 字符串格式化

「1.1 利用格式化操作符」

"%"是Python风格的字符串格式化操作符,非常类似C语言里的printf()函数的字符串格式化(C语言中也是使用%)。213644581_1_20210122095122303格式化操作符

【例子】print('%c' % 97)  # a

print('%c %c %c' % (97, 98, 99))  # a b c

print('%d + %d = %d' % (4, 5, 9))  # 4 + 5 = 9

print("我叫 %s 今年 %d 岁!" % ('小明', 10))

# 我叫 小明 今年 10 岁!

print('%o' % 10)  # 12

print('%x' % 10)  # a

print('%X' % 10)  # A

print('%f' % 27.658)  # 27.658000

print('%e' % 27.658)  # 2.765800e+01

print('%E' % 27.658)  # 2.765800E+01

print('%g' % 27.658)  # 27.658

【例子】比较%s,str()和%r,repr()之间的差异。str()得到的字符串是面向用户的,具有较好的可读性。

repr()得到的字符串是面向机器的。text = "I am %d years old." % 22

print("I said: %s." % text)

# I said: I am 22 years old..

print("I said: %r." % text)

# I said: 'I am 22 years old.'

text = "Hello\tWorld\n"

print("%s" % text)

# Hello World

#

print("%r" % text)  # 'Hello\tWorld\n'

「1.2 利用格式化操作符辅助指令」

通过"%"可以进行字符串格式化,但是"%"经常会结合下面的辅助指令一起使用。213644581_2_20210122095122803辅助指令

【例子】print('%5.1f' % 27.658)  # ' 27.7'

print('%.2e' % 27.658)  # 2.77e+01

print('%10d' % 10)  # '        10'

print('%-10d' % 10)  # '10        '

print('%+d' % 10)  # +10

print('%#o' % 10)  # 0o12

print('%#x' % 108)  # 0x6c

print('%010d' % 5)  # 0000000005

【例子】整型用十六进制(hexadecimal)和八进制表示(octal)。num = 100

print("%d to hex is %x" % (num, num))

# 100 to hex is 64

print("%d to hex is %X" % (num, num))

# 100 to hex is 64

print("%d to hex is %#x" % (num, num))

# 100 to hex is 0x64

print("%d to hex is %#X" % (num, num))

# 100 to hex is 0X64

print("%d to oct is %o" % (num, num))

# 100 to oct is 144

print("%d to oct is %#o" % (num, num))

# 100 to oct is 0o144

【例子】指定输出的宽度和对齐。students = [

{"name": "Tom", "age": 27},

{"name": "Will", "age": 28},

{"name": "June", "age": 27}

]

print("name: %10s, age: %10d" % (students[0]["name"], students[0]["age"]))

print("name: %-10s, age: %-10d" % (students[1]["name"], students[1]["age"]))

print("name: %*s, age: %0*d" % (10, students[2]["name"], 10, students[2]["age"]))

# name:        Tom, age:         27

# name: Will      , age: 28

# name:       June, age: 0000000027

【例子】格式化输出字典dict。

Python的格式化操作符,可以直接使用"%(key)s"(这里的s根据具体类型改变)的方式表示dict中对应的value。students = [

{"name": "Tom", "age": 27},

{"name": "Will", "age": 28},

{"name": "June", "age": 27}

]

for student in students:

print("%(name)s is %(age)d years old" % student)

# Tom is 27 years old

# Will is 28 years old

# June is 27 years old

「1.3 利用format() 格式化函数」

Python2.6开始,新增了一种格式化字符串的函数str.format(),通过这个函数同样可以对字符串进行格式化处理。class str(object):

def format(self, *args, **kwargs):在format()函数中,使用“{}”符号来当作格式化操作符。

【例子】位置参数print("{0} is {1} years old".format("Tom", 12))

print("{} is {} years old".format("Tom", 12))

print("Hi, {0}! {0} is {1} years old".format("Tom", 12))

# Tom is 12 years old

# Tom is 12 years old

# Hi, Tom! Tom is 12 years old

【例子】关键字参数print("{name} is {age} years old".format(name="Tom", age=12))

# Tom is 12 years old

str = "{0} Love {b}".format('I', b='Lsgogroup')  # 位置参数要在关键字参数之前

print(str)  # I Love Lsgogroup

【例子】填充与对齐^、分别是居中、左对齐、右对齐,后面带宽度。

:号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填充。print('{:>8}'.format('3.14'))

print('{:<8}'.format('3.14'))

print('{:^8}'.format('3.14'))

print('{:0>8}'.format('3.14'))

print('{:a>8}'.format('3.14'))

#     3.14

# 3.14

#   3.14

# 00003.14

# aaaa3.14

【例子】浮点数精度print('{0:.2f}{1}'.format(27.658, 'GB') )  # 27.66GB

print('{:.4f}'.format(3.1415926))  # 3.1416

print('{:0>10.4f}'.format(3.1415926))  # 00003.1416

【例子】进制,b、d、o、x分别是二进制、十进制、八进制、十六进制。print('{:b}'.format(11))  # 1011

print('{:d}'.format(11))  # 11

print('{:o}'.format(11))  # 13

print('{:x}'.format(11))  # b

print('{:#x}'.format(11))  # 0xb

print('{:#X}'.format(11))  # 0XB

【例子】千位分隔符print('{:,}'.format(15700000000))  # 15,700,000,000

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python时间转换、给定一个以秒为单位的时间t_题目 1470:【蓝桥杯】【入门题】【基础练习VIP】时间转换... 的相关文章

随机推荐