Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include
  2. #include
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("cmmdc.in");
  7.  
  8. #define NMax 20
  9.  
  10. unsigned a[NMax], n;
  11.  
  12. void Citire()
  13. {
  14.  
  15. fin>>n;
  16. for (int i=0; i>a;
  17. fin.close();
  18. }
  19.  
  20. unsigned Euclid(unsigned x, unsigned y)
  21. {
  22. //calculeaza cmmdc(x, y) prin algoritmul lui Euclid
  23. unsigned r;
  24. while (y)
  25. {
  26. r = x % y;
  27. x = y;
  28. y = r;
  29. }
  30. return x;
  31. }
  32.  
  33. unsigned cmmdc(unsigned p, unsigned q)
  34. {
  35. int m;
  36. //functia intoarce cmmdc(a[p], ..., a[q])
  37. if (q-p <= 1) //cmmdc se poate calcula direct
  38. return Euclid(a[p], a[q]);
  39. m = (p + q) / 2;
  40. return Euclid(cmmdc(p, m), cmmdc(m+1, q));
  41. }
  42.  
  43. int main ()
  44. {
  45. Citire();
  46. cout<<"cmmdc= "<<cmmdc(0, n-1)<<'\n';
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement