python求助

P1150 Peter 的烟

CZJR @ 2023-05-14 18:39:58


def main():
    global n,k,summ,num,rest,a
    summ=0
    num=0
    rest=0
    n, k = map(int, input().split())
    summ=summ+n
    num=num+n
    a=num/k
    while(a>0):
            summ=summ+num/k
            rest=num%k
            num=num/k
            num=num+rest
            rest=0
            a=num/k
    print(summ)
main()

死循环,求助大佬们帮忙修改


by ForgotDream_CHN @ 2023-05-14 18:44:09

请使用地板除法


by ForgotDream_CHN @ 2023-05-14 18:45:17

@wangjiaxuan888 py默认是浮点除法来着


by 蛋蛋工作室 @ 2023-05-14 19:28:48

@wangjiaxuan888 一种解法:

a=str(input())
a=a.split(' ')
a[0]=int(a[0])
a[1]=int(a[1])
summ=0
summ+=a[0]
while(a[0]//a[1]>0):
    summ+=(a[0]//a[1])
    a[0]=a[0]%a[1]+a[0]//a[1]
print(summ)

by 蛋蛋工作室 @ 2023-05-14 19:36:59

@wangjiaxuan888 把除号改成向下取证就行了


by CZJR @ 2023-05-15 12:42:01

谢谢


|