Tvor0zhok

Рекурсия I

Feb 10th, 2021 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. //нерекурсивная функция
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int gcd (int a, int b)
  6. {
  7. int x = a, y = b, r;
  8.  
  9. while (y)
  10. {
  11. r = x % y;
  12. x = y;
  13. y = r;
  14. }
  15.  
  16. return x;
  17. }
  18.  
  19. int main()
  20. {
  21. int a, b, c, d;
  22.  
  23. cout << "Введите числа a, b, c, d:\n";
  24. cin >> a >> b >> c >> d;
  25.  
  26. cout << "a/b + d/c = " << (a*c + b*d) / gcd(a*c + b*d, b*c) << "/" << b*c / gcd(a*c + b*d, b*c);
  27.  
  28. return 0;
  29. }
  30.  
  31. //рекурсивная функция
  32. #include <iostream>
  33. using namespace std;
  34.  
  35. int gcd (int a, int b)
  36. { if (b) gcd(b, a % b); else return a; }
  37.  
  38. int main()
  39. {
  40. int a, b, c, d;
  41.  
  42. cout << "Введите числа a, b, c, d:\n";
  43. cin >> a >> b >> c >> d;
  44.  
  45. cout << "a/b + d/c = " << (a*c + b*d) / gcd(a*c + b*d, b*c) << "/" << b*c / gcd(a*c + b*d, b*c);
  46.  
  47. return 0;
  48. }
  49.  
Add Comment
Please, Sign In to add comment