Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int Euklid(int a, int b)
- {
- if(b==0) return a;
- else return Euklid(b, a%b);
- }
- int NWD(int *tab, int n) //NWD z wielu par liczb
- {
- int a=tab[0];
- int b=tab[1];
- int pom=Euklid(a,b);
- for(int i=2; i<n-1; i++)
- {
- pom=Euklid(pom, tab[i]);
- if(pom==1) break;
- }
- return pom;
- }
- //Chinskie twierdzenie o resztach
- int CHtw(int *tab1, int* tab2, int n)
- {
- if(NWD(tab2, n)>1) return -1;
- else{
- int ni=tab2[0];
- int yi=tab1[0];
- for(int j=0; j<n-1; j++)
- {
- bool nieznalazl=true;
- int i=0;
- while(nieznalazl)
- {
- if(yi%tab2[j+1]==tab1[j+1])
- {
- nieznalazl=false;
- ni=ni*tab2[j+1];
- }else{
- i++;
- yi=yi+ni;
- }
- }
- }
- return yi;
- }
- }
- int main()
- {
- int n=3;
- int tab[3]={3, 4, 1};
- int tab2[3]={4, 5, 7};
- cout<< CHtw(tab, tab2, n)<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement