• 扩展欧几里得算法


    扩展欧几里得算法

    1. 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务
    2. 参考《密码工程》p112伪代码实现ExtendedGCD(int a, int b, int *k, int *u, int *v)算法(10’)
    3. 在测试代码中计算74模167的逆。(5‘)
    4. 提交代码和运行结果截图
    #include <stdio.h>
    struct EX_GCD { 
        int u;
        int v;
        int k;
    };
     
    struct EX_GCD extended_euclidean(int a, int b,int k, int u, int v) {
        struct EX_GCD ex_gcd;
        if (b == 0) { 
            ex_gcd.u = 1;
            ex_gcd.v = 0;
            ex_gcd.k = 0;
            return ex_gcd;
        }
        int old_r = a, r = b;
        int old_u = 1, u1 = 0;
        int old_v = 0, v1 = 1;
      
        while (r != 0) { 
            int q = old_r / r;
            int temp = old_r;
            old_r = r;
            r = temp - q * r;
            temp = old_u;
            old_u = u1;
            u1 = temp - q * u1;
            temp = old_v;
            old_v = v1;
            v1 = temp - q * v1;
        }
    
        ex_gcd.u = old_s;
        ex_gcd.v = old_t;
        ex_gcd.k = old_r;
        return ex_gcd;
        }
     
    int main(void) {
        int a, b;
        printf("输入两个整数:\n");
        scanf("%d%d", &a, &b);
        if (a < b) { 
            int temp = a;
            a = b;
            b = temp;
        }
        struct EX_GCD s = extended_euclidean(a, b);
        printf("%d*%d+%d*%d=%d\n", s.u, a, s.v, b, s.k);
         printf("%d %d %d\n", s.u,s.v,s.k);
        return 0;
    }
    

  • 相关阅读:
    C++处理Json串——jsoncpp库
    古典文学--本经阴符七术
    古典文学--素书
    网络编程之getaddrinfo
    网络编程之addrinfo
    跳表数据结构
    MySQL 编译安装并且开启DEBUG模式
    volatile关键字详解
    istringstream、ostringstream、stringstream 类简介
    Selenium入门21 Select操作
  • 原文地址:https://www.cnblogs.com/harperhjl/p/16359460.html
Copyright © 2020-2023  润新知