Advertisement
JosepRivaille

P57852: Màxim comú divisor de quatre

Mar 14th, 2015
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int mcd4(int a, int b, int c, int d) {
  6.     if (a < 0) a = -a;
  7.     if (b < 0) b = -b;
  8.     if (c < 0) a = -c;
  9.     if (d < 0) b = -d;
  10.     if (b > a) {
  11.         int aux = a;
  12.         a = b;
  13.         b = aux;
  14.     }
  15.     if (d > c) {
  16.         int aux1 = c;
  17.         c = d;
  18.         d = aux1;
  19.     }
  20.     int r1;
  21.     while (b != 0) {
  22.         r1 = a%b;
  23.         a = b;
  24.         b = r1;
  25.     }
  26.     int r2;
  27.     while (d != 0) {
  28.         r2 = c%d;
  29.         c = d;
  30.         d = r2;
  31.     }
  32.     if (c > a) {
  33.         int aux1 = a;
  34.         a = c;
  35.         c = aux1;
  36.     }
  37.     int r;
  38.     while (c != 0) {
  39.         r = a%c;
  40.         a = c;
  41.         c = r;
  42.     }
  43.     return a;
  44. }
  45.  
  46. //Pre: Llegeix 4 nombres
  47. //Post: Calcula l'mcd
  48. int main() {
  49.     int a, b, c, d;
  50.     cin >> a >> b >> c >> d;
  51.     cout << mcd4(a, b, c, d) << endl;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement