Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6. void DzielenieReszta(int a, int b);
  7. int NWD(int a, int b);
  8. int NWD2(int a, int b);
  9. int NWD3(int a, int b);
  10. int NWW(int a, int b);
  11. int fib(int n);
  12. int licznik = 0, licznik2 = 0, licznik3 = 0;
  13. int  main()
  14. {
  15.         DzielenieReszta(7, 2);
  16.         int n = 8;
  17.         int n2 = n+1;
  18.         cout<<fib(n) << " fiba od n"<< endl;
  19.         cout<<fib(n2)<<" fiba od n2" << endl;
  20.         cout << NWD(fib(n),fib(n2)) << " ";
  21.         cout<<licznik<< " nwd z dodawaniem"<<endl;
  22.         cout << NWD2(fib(n), fib(n2)) << " ";
  23.         cout<< licznik2 << " nwd z mnozeniem"<<endl;
  24.         cout << NWD3(fib(n), fib(n2)) << " ";
  25.         cout<< licznik3 << " nwd rekurencyjnie"<<endl;
  26.         cout << NWW(fib(n), fib(n2)) << " nww" << endl;
  27.         return 0;
  28. }
  29.  
  30. void DzielenieReszta(int a, int b)
  31. {
  32.         int r = a;
  33.         if (b == 0)
  34.         {
  35.                 cout << "b jest rowne 0!" << endl;
  36.                 return;
  37.         }
  38.         int q = 0;
  39.         while (r>=q)
  40.         {
  41.  
  42.                 q++;
  43.                 r = r - b;
  44.         }
  45.         cout << q <<"*" <<b <<"+" << r <<  "   dzielenie z reszta"<<endl;
  46. }
  47. int NWD(int a, int b)
  48. {
  49.         while (a != b)
  50.         {
  51.                 ++licznik;
  52.                 if (a < b)
  53.                         b = b - a;
  54.                 else
  55.                         a = a- b;
  56.  
  57.         }
  58.         return a;
  59. }
  60. int NWD2(int a, int b)
  61. {
  62.         int t = 0;
  63.  
  64.         while (b != 0)
  65.         {
  66.                 ++licznik2;
  67.                 t = b;
  68.                 b = a % b;
  69.                 a = t;
  70.  
  71.         }
  72.         return a;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement