Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- int potencia (int a, int n){
- queue<int> fila;
- int x, c=0, b;
- if (n%2!=0){
- x=n-1;
- fila.push(a);
- }
- else {
- x=n;
- }
- while(x!=1){
- while(x%2==0){
- x=x/2;
- c++;
- }
- if (x!=1){
- x=x-1;
- b=a;
- for(int i=0; i<c; i++){
- a=a*a;
- }
- fila.push(a);
- a=b;
- }
- }
- for (int i=0;i<c;i++){
- a=a*a;
- }
- if (fila.size()>0){
- b=fila.size();
- for (int i=0; i<b; i++){
- a=a*fila.front();
- fila.pop();
- }
- }
- return a;
- }
- int main()
- {
- int a, n;
- cout<<"Insert A"<<endl;
- cin>>a;
- cout<<"Insert N"<<endl;
- cin>>n;
- cout<<potencia(a,n)<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement