C++ 大数运算(幂运算)
- 在C++中 int64 的最大值:9223372036854775807,所以不适合幂很大的运算。我们这里可以转换成字符串的形式来表示,求 n 的 num 次方。
#include<iostream>
#include<cstring>
using namespace std;
int main(int argc,char **argv){
int n,num;
cin>>n>>num;
string str = "1";
char ch[] = {'0','1','2','3','4','5','6','7','8','9'};
for(int i=0;i<num;i++){
int flag = 0;
for(int j=str.size()-1;j>=0;j--){
int temp = str[j] - '0';
temp = n*temp + flag;
flag = 0;
if(temp > 9){
flag = temp/10;
temp = temp%10;
}
str[j] = temp + '0';
}
if(flag>0)
str = ch[flag] + str;
}
cout<<str;
return 0;
}
}