fork(2) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int licznika, licznikb;
  5. int tabdz[150];
  6. int dzielnik(int x);
  7. int nwd(int x[],int xl, int y[], int yl);
  8. int main()
  9. {
  10. int wynik,ile,a,b;
  11. cin >> ile;
  12. for(int i=1; i<=ile; i++)
  13. {
  14. cin >> a >> b;
  15.  
  16. if((a==0)&&(b==0)) cout << 0 << endl;
  17. else if (a==0) {}
  18. else if (b==0) {}
  19. else if((a==1)||(b==1)) cout << 1 << endl;
  20. else if(b%a==0) cout << a << endl;
  21. else if(a%b==0) cout << b << endl;
  22. else
  23. {
  24. licznika = dzielnik(a);
  25. int taba[licznika];
  26. for(int i=0;i<=licznika;i++) taba[i]=tabdz[i];
  27. licznikb = dzielnik(b);
  28. int tabb[licznikb];
  29. for(int i=0;i<=licznikb;i++) tabb[i]=tabdz[i];
  30. cout << nwd(taba,licznika, tabb,licznikb)<<endl;
  31. //for (int i=0;i<=licznika;i++) cout << "TEST TABA " << taba[i] << endl;
  32. //for (int i=0;i<=licznikb;i++) cout << "TEST TABB " << tabb[i] << endl;
  33.  
  34. //cout << "TEST LICZNIK A " << licznika << endl;
  35. //cout << "TEST LICZNIK B " << licznikb << endl;
  36. }
  37. }
  38. return 0;
  39. }
  40. int dzielnik(int x)
  41. {
  42. int licz=0;
  43. int *w;
  44. w=tabdz;
  45. *w=1;
  46. w++;
  47. int i=1,d=2,d2=2,kon=0;
  48. while(x!=1)
  49. {
  50. if(x%2==0) // Parzyste
  51. {
  52. d=2;
  53. if(x%d==0)
  54. {
  55. x=x/d;
  56. *w=d;
  57. w++;
  58. i++;
  59. licz++;
  60. }
  61. else
  62. {
  63.  
  64. }
  65. // Parzyste
  66. }
  67. else
  68. {
  69. d=3;
  70. kon=0;
  71.  
  72. while(kon!=1)
  73. {
  74.  
  75. if(x%d==0)
  76. {
  77. x=x/d;
  78. *w=d;
  79. w++;
  80. i++;
  81. licz++;
  82. //cout << "TEST LICZ " << licz << endl;
  83. kon=1;
  84. }
  85. else
  86. {
  87. d=d+2;
  88. }
  89. }
  90. }
  91. } // Koniec While
  92. return licz;
  93.  
  94. } // Koniec funkcji
  95.  
  96. int nwd(int x[],int xl, int y[], int yl)
  97. {
  98. int k=0,wynik=1,j=0,h=0;
  99. int tabc[150];
  100. for (int i=0;i<=xl;i++)
  101. {
  102. for(j=h;j<=yl;j++)
  103. {
  104. if(x[i]==y[j])
  105. {
  106. tabc[k]=x[i];
  107. k++;
  108. j++;
  109. h++;
  110. //cout << "TEST j " << j << endl;
  111. break;
  112.  
  113. }else
  114. {
  115.  
  116. //cout << "TEST I " << i << endl;
  117. }
  118. }
  119. }
  120. for(int i=0;i<k;i++)
  121. wynik*=tabc[i];
  122. for(int i=0;i<k;i++)
  123. //cout << "TEST TABC " << tabc[i] << " " << endl;
  124.  
  125. return wynik;
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
Success #stdin #stdout 0s 4556KB
stdin
5
1 4
4 1
12 48
48 100
123456 653421
stdout
1
1
12
4
3