1016 部分A+B Python——“优雅永不过时”by卡蜜尔
2023-12-22 19:33:52
if __name__ == '__main__':
s = input()
new_s = s.split(" ")
str_result1 = "0"
str_result2 = "0"
first_count = new_s[0].count(new_s[1])
second_count = new_s[2].count(new_s[3])
for i in range(first_count):
str_result1 += new_s[1]
for i in range(second_count):
str_result2 += new_s[3]
result = int(str_result1) + int(str_result2)
print(result)
-
读取和分割输入:
- 首先,代码通过
input()函数读取一行输入,并将其存储在变量s中。 - 使用
split(" ")方法将输入的字符串按空格分割,并将分割后的字符串列表存储在new_s中。
- 首先,代码通过
-
初始化结果字符串:
- 初始化两个字符串
str_result1和str_result2,它们都以"0"开始。
- 初始化两个字符串
-
计算字符出现次数:
- 使用
count()方法计算new_s[0]中new_s[1]出现的次数,存储在first_count中。 - 计算
new_s[2]中new_s[3]出现的次数,存储在second_count中。
- 使用
-
构建两个数字字符串:
- 使用两个
for循环分别构建两个数字字符串str_result1和str_result2。 - 第一个循环将
new_s[1]重复first_count次并追加到str_result1。 - 第二个循环将
new_s[3]重复second_count次并追加到str_result2。
- 使用两个
-
将字符串转换为整数并相加:
- 使用
int()函数将str_result1和str_result2转换为整数。 - 将这两个整数相加,得到结果
result。
- 使用
-
输出最终结果:
- 打印出相加后的结果
result。
- 打印出相加后的结果
文章来源:https://blog.csdn.net/weixin_61958146/article/details/135159528
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!