python题目记录(9.19)

发布于:2022-12-12 ⋅ 阅读:(668) ⋅ 点赞:(0)

1011 A+B 和 C

import sys
n = eval(input())
out = [None]*n
for i in range(n):
    a,b,c = map(int,sys.stdin.readline().split())
    if (a+b>c):
        out[i] = "true"
    else:
        out[i] = "false"
for i in range(n):
    print(f"Case #{i+1}: {out[i]}")
    

 有了昨天的题目轻松多了^-^。这个题注意的是不能用布尔值加入,因为输出的是“True”,“False”形式

如果用别的语言,还要考虑数据类型。

1016 部分A+B

想法:找出字符串里面有几个字母,组成新的字符串,在转int类型相加

第一次提交代码:

def num(x,y):
    n = 0
    for i in x:
        if y in i:
            n = n+1
    return n
        
a,da,b,db = input().split()
na = num(a,da)
nb = num(b,db)
ai = na*da
bi = nb*db
print(int(ai)+int(bi))

结果: 

应该是边界问题没考虑,一开始没找出来,参考了如下:

 https://blog.csdn.net/weixin_55730361/article/details/126225247

看到if条件,发现应该是边界0,也就是第二个测试用例没考虑

def num(x,y):
    n = 0
    for i in x:
        if y in i:
            n = n+1
    return n
        
a,da,b,db = input().split()
na = num(a,da)
nb = num(b,db)
if na == 0:
    ai = 0
else:
    ai = na*da
if nb == 0:
    bi = 0
else:
    bi = nb*db
print(int(ai)+int(bi))

这样就对了。注意一下计数函数,另外,python中没有x++。

不过我还是觉得我这个是完全按照题目表述思路写了,上面的帖子是一种很有启发的方法。

另外,还有数学方法:

def result_x(x,dx):
    re_x = 0
    while(x):
        if(x%10==dx):
            re_x = re_x*10 + dx
        x = x//10
    return re_x
a,da,b,db = map(int,input().split())
result_a = result_x(a,da)
result_b = result_x(b,db)
print(result_a+result_b)

注意:Python的整除是//