奥克兰大学CS225(Auckland University Computer Science 225)的一些算法(2022)

免责声明:课余时间写的,仅仅是很多时候懒得手算,不能100%保证完全应对每种情况,请自行判断和使用,如果有任何bug或者功能修改可以向我反馈,我会尝试修复/添加

算法一览:
1.输入一个数,拿到所有的factor
例:8 = [1, 2, 4, 8]

2.输入一个数,拿到的prime formula
例:8 = 2 * 2 * 2

3.输入x和y,计算gcd(x, y)(greater common devisors)
例: gcd(120, 25) = 5

4.找到比x大的最小质数
例: find_bigger_prime(100000);

5.判断x是否为质数
例: is_prime(100003);

使用本程序请确保您有一定的编程基础
ps:不定期更新

# 1.拿到一个数的所有factor, 注意,输入必须大于1!
def factors_list(x):
    result_list = []
    for i in range(1, x+1):
        if x % i == 0:
            result_list.append(i)
    return result_list

# 2.输出一个数的prime formula, 注意,输入必须大于1!
def prime_formula(x):
    result_list = []
    i = 2
    while i <= x/i:
        while x % i == 0:
            result_list.append(i)
            x /= i
        i += 1
    if x > 1:
        result_list.append(int(x))
    return "*".join(str(i) for i in result_list)


# 3.输入要查询gcd的x和y,例如find_gcd(120, 25), 输出结果5
def find_gcd(x, y):
    quotient = x // y
    reminder = x - (quotient * y)
    print(f'x = {x}\ty = {y}\tq = {quotient}\tr = {reminder}')
    while reminder != 0:
        x = y
        y = reminder
        quotient = x // y
        reminder = x - (quotient * y)
        print(f'x = {x}\ty = {y}\tq = {quotient}\tr = {reminder}')
    return y


# 程序启动部分
def main(select_function):
    if select_function == 1:
        number = int(input("pls enter the number: "))
        print(f'factors_list for {number} = {factors_list(number)}')
    elif select_function == 2:
        number = int(input("pls enter the number: "))
        print(f'prime formula for {number} = {prime_formula(number)}')
    elif select_function == 3:
        x = int(input("pls enter the x: "))
        y = int(input("pls enter the y: "))
        print(f'gcd({x}, {y}) = {find_gcd(x, y)}')


#
if __name__ == "__main__":
    """
    算法一览:
    1.输入一个数,拿到所有的factor           
    例:8 = [1, 2, 4, 8]
                             
    2.输入一个数,拿到的prime formula  
    例:8 = 2*2*2           
                       
    3.输入x和y,计算gcd(x, y) (greater common devisors)  
    例: gcd(120, 25) = 5     
    
    """

    select_function = int(input("pls enter the function you want to use(input 1/2/3): "))
    main(select_function)
    //4.找到比当前数大的最小质数
    public static void find_bigger_prime(int x) {
        for (int i = x; ; i++) {
            boolean judge = true;
            for (int j = 2; (j * j) <= i; j++){
                if (i % j == 0) {
                    judge = false;
                    break;
                }
            }
            if (judge) {
                System.out.println(i);
                break;
            }
        }
    }
    
    //5.判断当前数是否是质数
    public static void is_prime(int x) {
        boolean judge = true;
        for (int j = 2; (j * j) <= x; j++){
            if (x % j == 0) {
                judge = false;
                break;
            }
        }
        if (judge) {System.out.println("Yes");}
        else {System.out.println("No");}
    }

2022/9/7:对1和2进行优化,降低时间复杂度

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我没有具体的代码,但是可以提供一些指导。红黑树的实现包括插入、搜索、删除和旋转,所以你可以考虑实现这些操作。一些可以参考的资源有:https://www.geeksforgeeks.org/red-black-tree-set-1-introduction-2/,https://www.tutorialspoint.com/data_structures_algorithms/red_black_tree_algorithm.htm,以及http://www.cs.auckland.ac.nz/software/AlgAnim/red_black.html。 ### 回答2: 红黑树(Red-Black Tree)是一种自平衡的二叉搜索树,通过在每个节点上添加一个额外的存储位表示节点的颜色(红色或黑色),并对其进行特定的调整和变色操作以保持平衡。以下是用JavaScript实现红黑树算法的示例代码: ```javascript class Node { constructor(value) { this.value = value; this.left = null; this.right = null; this.color = 1; // 默认新节点为红色(1),插入红黑树中的路径节点必须为黑色(0) this.parent = null; } } class RedBlackTree { constructor() { this.root = null; } // 左旋操作 rotateLeft(node) { const temp = node.right; node.right = temp.left; if (temp.left) { temp.left.parent = node; } temp.parent = node.parent; if (!node.parent) { this.root = temp; } else if (node === node.parent.left) { node.parent.left = temp; } else { node.parent.right = temp; } temp.left = node; node.parent = temp; } // 右旋操作 rotateRight(node) { const temp = node.left; node.left = temp.right; if (temp.right) { temp.right.parent = node; } temp.parent = node.parent; if (!node.parent) { this.root = temp; } else if (node === node.parent.right) { node.parent.right = temp; } else { node.parent.left = temp; } temp.right = node; node.parent = temp; } // 节点插入操作 insert(value) { const newNode = new Node(value); let current = this.root; let parent = null; while (current !== null) { parent = current; if (newNode.value < current.value) { current = current.left; } else { current = current.right; } } newNode.parent = parent; if (!parent) { this.root = newNode; } else if (newNode.value < parent.value) { parent.left = newNode; } else { parent.right = newNode; } if (!newNode.parent) { newNode.color = 0; return; } if (!newNode.parent.parent) { return; } this.fixTree(newNode); } // 红黑树修正 fixTree(node) { while ( node && node.parent && node.parent.color === 1 && node.color !== 0 ) { if (node.parent === node.parent.parent.left) { let uncle = node.parent.parent.right; if (uncle && uncle.color === 1) { node.parent.color = 0; uncle.color = 0; node.parent.parent.color = 1; node = node.parent.parent; } else { if (node === node.parent.right) { node = node.parent; this.rotateLeft(node); } node.parent.color = 0; node.parent.parent.color = 1; this.rotateRight(node.parent.parent); } } else { let uncle = node.parent.parent.left; if (uncle && uncle.color === 1) { node.parent.color = 0; uncle.color = 0; node.parent.parent.color = 1; node = node.parent.parent; } else { if (node === node.parent.left) { node = node.parent; this.rotateRight(node); } node.parent.color = 0; node.parent.parent.color = 1; this.rotateLeft(node.parent.parent); } } } this.root.color = 0; } } // 示例用法 const tree = new RedBlackTree(); tree.insert(7); tree.insert(3); tree.insert(18); tree.insert(10); tree.insert(22); tree.insert(8); tree.insert(11); tree.insert(26); console.log(tree.root); ``` 以上是用JavaScript实现红黑树算法的示例代码。通过调用`insert()`方法将节点插入红黑树中,该方法内部会根据红黑树的定义进行节点的调整和变色操作,最终实现自平衡的红黑树数据结构。你可以根据自己的需求,修改代码中的示例用法或扩展其功能。 ### 回答3: 红黑树是一种自平衡的二叉搜索树,它能够保持树的平衡性,从而提高搜索、插入和删除等操作的效率。下面是用 JavaScript 实现红黑树算法的简单示例代码: ```javascript // 定义红黑树的节点类 class Node { constructor(value, color) { this.value = value; this.left = null; this.right = null; this.parent = null; this.color = color; } } // 定义红黑树类 class RedBlackTree { constructor() { this.root = null; } // 左旋 rotateLeft(node) { let rightChild = node.right; node.right = rightChild.left; if (rightChild.left != null) { rightChild.left.parent = node; } rightChild.parent = node.parent; if (node.parent != null) { if (node == node.parent.left) { node.parent.left = rightChild; } else { node.parent.right = rightChild; } } else { this.root = rightChild; } rightChild.left = node; node.parent = rightChild; } // 右旋 rotateRight(node) { let leftChild = node.left; node.left = leftChild.right; if (leftChild.right != null) { leftChild.right.parent = node; } leftChild.parent = node.parent; if (node.parent != null) { if (node == node.parent.right) { node.parent.right = leftChild; } else { node.parent.left = leftChild; } } else { this.root = leftChild; } leftChild.right = node; node.parent = leftChild; } // 插入节点 insert(value) { let newNode = new Node(value, "red"); if (this.root == null) { this.root = newNode; this.root.color = "black"; return; } this.insertNode(this.root, newNode); this.enforceRBTreeProperties(newNode); } insertNode(node, newNode) { if (newNode.value < node.value) { if (node.left == null) { node.left = newNode; newNode.parent = node; } else { this.insertNode(node.left, newNode); } } else { if (node.right == null) { node.right = newNode; newNode.parent = node; } else { this.insertNode(node.right, newNode); } } } enforceRBTreeProperties(node) { if (node == this.root) { node.color = "black"; } else if (node.parent.color == "red") { let grandParent = node.parent.parent; let uncle = null; if (grandParent.left == node.parent) { uncle = grandParent.right; if (uncle != null && uncle.color == "red") { node.parent.color = "black"; uncle.color = "black"; grandParent.color = "red"; this.enforceRBTreeProperties(grandParent); } else { if (node == node.parent.right) { this.rotateLeft(node.parent); node = node.left; } node.parent.color = "black"; grandParent.color = "red"; this.rotateRight(grandParent); } } else { uncle = grandParent.left; if (uncle != null && uncle.color == "red") { node.parent.color = "black"; uncle.color = "black"; grandParent.color = "red"; this.enforceRBTreeProperties(grandParent); } else { if (node == node.parent.left) { this.rotateRight(node.parent); node = node.right; } node.parent.color = "black"; grandParent.color = "red"; this.rotateLeft(grandParent); } } } } } // 测试红黑树 let rbTree = new RedBlackTree(); rbTree.insert(10); rbTree.insert(20); rbTree.insert(30); rbTree.insert(40); // 可以继续插入其他节点并进行相关操作 ``` 这段代码实现了一个红黑树类,包括节点的定义、左右旋的操作以及插入节点时的自平衡操作。通过调用类中的 insert() 方法,可以将新的节点插入红黑树中,并自动进行相关的调整,以保持红黑树的平衡。这段代码只是一个简单示例,完整的红黑树实现可能还需要考虑其他操作,比如删除节点等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值