Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include<iostream>
  2. #include<ctime>
  3. #include<iomanip>
  4. using namespace std;
  5. double stepen(double x, int n);
  6. double stepen2(double x, int n);
  7. double stepen(double x, int n)
  8. {
  9. if (n == 0)
  10. return 1;
  11. else
  12. return x * stepen(x, n - 1);
  13. }
  14. double stepen2(double x, int n)
  15. {
  16. if (n == 0)
  17. return 1;
  18. else
  19. {
  20. if (n % 2)
  21. return x * stepen2(x, n - 1);
  22. else return stepen2(x*x, n / 2);
  23. }
  24. }
  25. void main()
  26. {
  27. double x;
  28. int n;
  29. cout << "x = "; cin >> x;
  30. cout << "n = "; cin >> n;
  31. unsigned stime = clock();
  32. cout << x << "^" << n << "=" << pow(x, n) << endl;
  33. unsigned ctime = clock();
  34. cout << setprecision(8) << (double)(ctime - stime) / CLOCKS_PER_SEC << endl;
  35. stime = clock();
  36. cout << x << "^" << n << "=" << stepen(x, n) << endl;
  37. ctime = clock();
  38. cout << setprecision(8) << (double)(ctime - stime) / CLOCKS_PER_SEC << endl;
  39. stime = clock();
  40. cout << x << "^" << n << "=" << stepen2(x, n) << endl;
  41. ctime = clock();
  42. cout << setprecision(8) << (double)(ctime - stime) / CLOCKS_PER_SEC;
  43. cout << endl;
  44. system("pause");
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement