Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. int potencia (int a, int n){
  6.     queue<int> fila;
  7.     int x, c=0, b;
  8.  
  9.     if (n%2!=0){
  10.         x=n-1;
  11.         fila.push(a);
  12.     }
  13.     else {
  14.         x=n;
  15.     }
  16.         while(x!=1){
  17.         while(x%2==0){
  18.             x=x/2;
  19.             c++;
  20.         }
  21.         if (x!=1){
  22.             x=x-1;
  23.             b=a;
  24.             for(int i=0; i<c; i++){
  25.                 a=a*a;
  26.             }
  27.             fila.push(a);
  28.             a=b;
  29.  
  30.         }
  31.         }
  32.  
  33.             for (int i=0;i<c;i++){
  34.                 a=a*a;
  35.             }
  36.             if (fila.size()>0){
  37.                 b=fila.size();
  38.                 for (int i=0; i<b; i++){
  39.                     a=a*fila.front();
  40.                     fila.pop();
  41.                 }
  42.  
  43.             }
  44.             return a;
  45.  
  46.  
  47.  
  48.  
  49.     }
  50.  
  51.  
  52. int main()
  53. {
  54.     int a, n;
  55.     cout<<"Insert A"<<endl;
  56.     cin>>a;
  57.     cout<<"Insert N"<<endl;
  58.     cin>>n;
  59.     cout<<potencia(a,n)<<endl;
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement