0
点赞
收藏
分享

微信扫一扫

Java抽象类(abstract class)和接口(interface)的区别——面试

打卡记录

在这里插入图片描述


Plus and Multiply(模拟)

链接
要满足 a x + b ∗ y = n a^x + b * y = n ax+by=n 的关系,可以枚举满足 b ∗ y = n − a x b * y = n - a ^ x by=nax 的可余条件。

t = int(input())
for _ in range(t):
    n, a, b = map(int, input().split())
    if n == 1 or b == 1 or n % b == 1:
        print('YES')
    elif a == 1:
        if n % b == 1:
            print('YES')
        else:
            print('NO')
    else:
        mul = 1
        while mul <= n:
            if (n - mul) % b == 0:
                print('YES')
                break
            mul *= a
        else:
            print('NO')

到达首都的最少油耗(dfs)

链接
从每条路要被走过的车的次数来进行考虑。

class Solution:
    def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
        n = len(roads) + 1
        g = [[] for _ in range(n)]
        g[0] = [-1]
        for x, y in roads:
            g[y].append(x)
            g[x].append(y)
        ans = 0
        def dfs(x, fa):
            res = 0
            for y in g[x]:
                if y == fa:
                    continue
                nonlocal ans
                tmp = dfs(y, x) + 1
                ans += (tmp - 1) // seats + 1
                res += tmp
            return res
        dfs(0, -1)
        return ans    
举报

相关推荐

抽象(Abstract)和接口(interface)的区别

0 条评论