fission吧 关注:7贴子:429
  • 3回复贴,共1
度娘,我没有在水贴,别删我的帖子啊~~
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
vector<int> states;
int gcd(int a,int b){//辗转相除表示法,又称欧几里德算法
if(b!= 0) states.push_back(int(a / b));
return !b ? a : gcd(b, a % b);
}
int main(int argc, char *argv[])
{
states= vector<int>();
int a, b;
scanf("%d%d",&a,&b);
if(a < b){ a+=b;b=a - b;a-=b;}//只会出现一次 a<b的情况
int c = gcd(a,b);
printf("gcd: %d\n", c);
int x, y;
x = 1;
y = 0;
while(!states.empty()){
int x_old = x;int y_old = y;
x = y_old;
y = x_old - states.back() * y_old;
states.pop_back();
}
printf("ax+by=gcd: %d * %d + %d * %d = %d\n" ,a ,x ,b ,y ,c);
system("PAUSE");
return EXIT_SUCCESS;
}


IP属地:北京1楼2017-03-19 10:39回复
    改一下
    #include <cstdlib>
    #include <iostream>
    #include <vector>
    using namespace std;
    vector<int> states;
    int gcd(int a,int b){//辗转相除表示法,又称欧几里德算法
    if(b!= 0) states.push_back(int(a / b));
    return !b ? a : gcd(b, a % b);
    }
    int main(int argc, char *argv[])
    {
    states= vector<int>();
    int a, b;
    scanf("%d%d",&a,&b);
    if(a < b){ a+=b;b=a - b;a-=b;}//只会出现一次a<b的情况
    int c = gcd(a,b);
    printf("gcd: %d\n", c);
    int x, y;
    x = 1;
    y = 0;
    while(!states.empty()){//根据公式反求x,y
    int x_old = x;int y_old = y;
    x = y_old;
    y = x_old - states.back() * y_old;
    states.pop_back();
    }
    printf("ax+by=gcd: %d * %d + %d * %d = %d\n" ,a ,x ,b ,y ,c);
    system("PAUSE");
    return EXIT_SUCCESS;
    }


    IP属地:北京2楼2017-03-19 10:49
    回复
      真是666,看我的代码(防吞
      #include <cstdlib>
      #include <iostream>
      #include <vector>
      using namespace std;
      vector<int> states;
      int gcd(int a,int b){//辗转相除表示法,又称欧几里德算法
      if(b!= 0) states.push_back(int(a / b));
      return !b ? a : gcd(b, a % b);
      }
      int main(int argc, char *argv[])
      {
      start:
      states= vector<int>();
      int a, b;
      printf("Please input 2 numbers:");
      scanf("%d%d",&a,&b);
      if(a < b){ a+=b;b=a - b;a-=b;}//只会出现一次a<b的情况
      int c = gcd(a,b);
      int x, y;
      x = 1;
      y = 0;
      while(!states.empty()){//根据公式反求x,y
      int x_old = x, y_old = y;
      x = y_old;
      y = x_old - states.back() * y_old;
      states.pop_back();
      }
      printf("a * x + b * y = GCD\n%d * %d + %d * %d = %d\n\n" ,a ,x ,b ,y ,c);
      goto start;
      return EXIT_SUCCESS;
      }


      IP属地:北京3楼2017-03-19 11:05
      回复
        #include <cstdlib>
        #include <iostream>
        #include <vector>
        using namespace std;
        vector<int> states;
        int gcd(int a,int b){//辗转相除表示法,又称欧几里德算法
        if(b!= 0) states.push_back(int(a / b));
        return !b ? a : gcd(b, a % b);
        }
        int main(int argc, char *argv[])
        {
        start:
        states= vector<int>();
        int a, b;
        printf("Please input 2 numbers,Separated with space:");
        scanf("%d%d",&a,&b);
        if(a < b){ a+=b;b=a - b;a-=b;}//只会出现一次a<b的情况
        int c = gcd(a,b);
        int x, y;
        x = 1;
        y = 0;
        while(!states.empty()){//根据公式反求x,y
        int x_old = x, y_old = y;
        x = y_old;
        y = x_old - states.back() * y_old;
        states.pop_back();
        }
        printf("a * x + b * y = GCD\n%d * %d + %d * %d = %d\n\n" ,a ,x ,b ,y ,c);
        goto start;
        return EXIT_SUCCESS;
        }


        IP属地:北京4楼2017-03-19 11:09
        回复