题目 1083:Hello, world!蓝桥杯刷题群已成立,微信后台回复【蓝桥杯】,即可进入。 如果加入了之前的社群不需要重复加入。
时间限制: 1Sec 内存限制: 64MB 1. 题目描述这是要测试的第一个问题。由于我们都知道ASCII码,因此您的工作很简单:输入数字并输出相应的消息。 2. 输入输入将包含一个由空格(空格,换行符,TAB)分隔的正整数列表。请处理到文件末尾(EOF)。整数将不少于32。 3. 输出输出相应的消息。请注意,输出末尾没有换行符。 4. 样例输入72 101 108 108 111 44 32 119 111 114 108 100 33
5. 样例输出Hello, world!
6. 解决方案「Python语言」 while True: try: num = list(map(int, input().strip().split())) for i in num: print(chr(i), end='') except: break
知识点「1. print() 函数」 def print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):
- 将
objects 以字符串表示的方式格式化输出到流文件对象file 里。其中所有非关键字参数都按str() 方式进行转换为字符串输出; - 关键字参数
sep 是实现分隔符,比如多个参数输出时想要输出中间的分隔字符; - 关键字参数
end 是输出结束时的字符,默认是换行符\n ; - 关键字参数
file 是定义流输出的文件,可以是标准的系统输出sys.stdout ,也可以重定义为别的文件; - 关键字参数
flush 是立即把内容输出到流文件,不作缓存。
【例子】没有参数时,每次输出后都会换行。 shoplist = ['apple', 'mango', 'carrot', 'banana'] print("This is printed without 'end'and 'sep'.") for item in shoplist: print(item)
# This is printed without 'end'and 'sep'. # apple # mango # carrot # banana
【例子】每次输出结束都用end 设置的参数& 结尾,并没有默认换行。 shoplist = ['apple', 'mango', 'carrot', 'banana'] print("This is printed with 'end='&''.") for item in shoplist: print(item, end='&') print('hello world')
# This is printed with 'end='&''. # apple&mango&carrot&banana&hello world
【例子】item 值与'another string' 两个值之间用sep 设置的参数& 分割。由于end 参数没有设置,因此默认是输出解释后换行,即end 参数的默认值为\n 。 shoplist = ['apple', 'mango', 'carrot', 'banana'] print("This is printed with 'sep='&''.") for item in shoplist: print(item, 'another string', sep='&')
# This is printed with 'sep='&''. # apple&another string # mango&another string # carrot&another string # banana&another string
「2. chr()函数」 def chr(i):
- 返回Unicode码位为整数
i 的字符的字符串格式。 - 参数
i :可以是10进制也可以是16进制的形式的数字,数字范围为0到1,114,111 (16 进制为 0x10FFFF)。如果i 超过这个范围,会触发ValueError 异常。
【例子】chr(97) 返回字符串 'a',chr(8364) 返回字符串 '€'。 print(chr(97)) # a print(chr(8364)) # €
「3. ord()函数」 def ord(c):
- 对表示单个Unicode字符的字符串,返回代表它 Unicode码点的整数。
【例子】ord('a') 返回整数97,ord('€') (欧元符号)返回8364。 print(ord('a')) # 97 print(ord('€')) # 8364
|