Search

Jan 18, 2024

Bài tập sinh viên - Code Bezout, Nghịch đảo, Số dư Trung Hoa

 Bezout

#include<stdio.h>
#include<conio.h>

int Bezout(int a,int b)
{
    int d,x,y;
    int x1,x2,y1,y2,q,r;
    if(b<0)
    {
           return 0; /* Khong dung dieu kien */
           }
    else if(a<b || a==b)
    {
           return 0; /* Khong dung dieu kien */
           }     
    else if(b==0)
    {
         d = a; x = 1; y = 0;
         return (d,x,y);      
         }
    else
    {
        x1=0; x2=1; y1=1; y2=0;
        while(b>0)
        {
                  q=a/b; r=a-q*b; x=x2-q*x1; y=y2-q*y1;
                  a=b; b=r; x2=x1; x1=x; y2=y1; y1=y;
                  }
        d = a; x=x2; y=y2;
        return (d,x,y);
        }          
}       

int main()
{
    int d,x,y;
    Bezout(10,3);
    getch();
    }


Nghịch đảo

#include<stdio.h>
#include<conio.h>

int NghichDao(int a,int b)
{
    int d,x,y;
    int x1,x2,y1,y2,q,r,m;
    m=a;      
    if(b==0)
    {
         d = a; x = 1; y = 0;
         if(d>1)
         {
                 return 0; /* Khong ton tai */
                 }
         else if(d==1)
         {
                 return (y%m);
                 }          
         }
    else
    {
        x1=0; x2=1; y1=1; y2=0;
        while(b>0)
        {
                  q=a/b; r=a-q*b; x=x2-q*x1; y=y2-q*y1;
                  a=b; b=r; x2=x1; x1=x; y2=y1; y1=y;
                  }
        d = a; x=x2; y=y2;
        if(d>1)
        {
                 return 0; /* Khong ton tai */
                 }
        else if(d==1)
        {
                 return (y%m);
                 }      
        }             
}       

int main()
{
    int a,b;
    printf("%d", NghichDao(3,35));
    getch();
}


Số dư Trung Hoa

#include<stdio.h>
#include<conio.h>

int NghichDao(int a,int b)
{
    int d,x,y;
    int x1,x2,y1,y2,q,r,m;
    m=a;      
    if(b==0)
    {
         d = a; x = 1; y = 0;
         if(d>1)
         {
                 return 0; /* Khong ton tai */
                 }
         else if(d==1)
         {
                 return (y%m);  
                 }          
         }
    else
    {
        x1=0; x2=1; y1=1; y2=0;
        while(b>0)
        {
                  q=a/b; r=a-q*b; x=x2-q*x1; y=y2-q*y1;
                  a=b; b=r; x2=x1; x1=x; y2=y1; y1=y;
                  }
        d = a; x=x2; y=y2;
        if(d>1)
        {
                 return 0; /* Khong ton tai */
                 }
        else if(d==1)
        {
                 return (y%m);
                 }      
        }             
}

int SDTH(int m1, int m2, int m3, int n1, int n2, int n3)
{
    int tich = n1*n2*n3;
    int tich1 = n2*n3;
    int tich2 = n1*n3;
    int tich3 = n1*n2;
    int c1 = NghichDao(n1,tich1);
    int c2 = NghichDao(n2,tich2);
    int c3 = NghichDao(n3,tich3);
    int x0 = m1*tich1*c1 + m2*tich2*c2 + m3*tich3*c3;
    return x0;
}     

int main()
{
    printf("KQ = %d", SDTH(2, 3, 2, 3, 5, 7)); 
    getch();
}

No comments:

Post a Comment