Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<ctime>
- #include<iomanip>
- using namespace std;
- double stepen(double x, int n);
- double stepen2(double x, int n);
- double stepen(double x, int n)
- {
- if (n == 0)
- return 1;
- else
- return x * stepen(x, n - 1);
- }
- double stepen2(double x, int n)
- {
- if (n == 0)
- return 1;
- else
- {
- if (n % 2)
- return x * stepen2(x, n - 1);
- else return stepen2(x*x, n / 2);
- }
- }
- void main()
- {
- double x;
- int n;
- cout << "x = "; cin >> x;
- cout << "n = "; cin >> n;
- unsigned stime = clock();
- cout << x << "^" << n << "=" << pow(x, n) << endl;
- unsigned ctime = clock();
- cout << setprecision(8) << (double)(ctime - stime) / CLOCKS_PER_SEC << endl;
- stime = clock();
- cout << x << "^" << n << "=" << stepen(x, n) << endl;
- ctime = clock();
- cout << setprecision(8) << (double)(ctime - stime) / CLOCKS_PER_SEC << endl;
- stime = clock();
- cout << x << "^" << n << "=" << stepen2(x, n) << endl;
- ctime = clock();
- cout << setprecision(8) << (double)(ctime - stime) / CLOCKS_PER_SEC;
- cout << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement