分享

题目 1083:【蓝桥杯】【入门题】Hello, world!

 老马的程序人生 2021-01-20

题目 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异常。
  • ord()的逆函数。

【例子】chr(97) 返回字符串 'a',chr(8364) 返回字符串 '€'。

print(chr(97))  # a
print(chr(8364))  # €

「3. ord()函数」

def ord(c): 
  • 对表示单个Unicode字符的字符串,返回代表它 Unicode码点的整数。
  • chr()的逆函数。

【例子】ord('a')返回整数97,ord('€')(欧元符号)返回8364。

print(ord('a'))  # 97
print(ord('€'))  # 8364

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多