Advertisement
NS2A2

Tính lũy thừa - chia để trị

Dec 8th, 2021
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. double a;
  6. int n;
  7.  
  8. double TinhLuyThua(double A,int N)
  9. {
  10.     if (N==1)
  11.     {
  12.         return A;
  13.     }
  14.     else
  15.     {
  16.         int temp = TinhLuyThua(A,N/2);
  17.         if (N%2==0)
  18.         {
  19.             return temp*temp;
  20.         }
  21.         else
  22.             return temp*temp*A;
  23.     }
  24. }
  25.  
  26. int main()
  27. {
  28.     cout<<"Nhap co so a: ";
  29.     cin>>a;
  30.     cout<<"Nhap so mu n: ";
  31.     cin>>n;
  32.     cout<<"Luy thua la: "<<TinhLuyThua(a,n)<<endl;
  33.     return 0;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement