题目。。。
程序:
#include<iostream>
#include<string>
#include<cmath>
#include<list>
#include<utility>
using namespace std;
typedef pair<int,pair<int,int> > pass;
pass in();
int cal(pass&);
void show(int);
int ca_c(list<int>&,list<int>&,int);
int main(){
pass temp=in();
int result=cal(temp);
show(result);
return 0;
}
pass in(){
int a,b;
string base;
cout<<"a: ";
cin>>a;
cout<<"^: ";
cin>>base;
cout<<"b: ";
cin>>b;
pair<int,int> num(a,b);
return pass(base.size()+1,num);
}
int cal(pass& p){
int a,b,base;
a=p.second.first;
b=p.second.second;
base=p.first;
list<int> ca,cb;
while(a>=base){
ca.push_back(a%base);
a/=base;
}
ca.push_back(a);
while(b>=base){
cb.push_back(b%base);
b/=base;
}
cb.push_back(b);
int res;
if(ca.size()<cb.size())res=ca_c(ca,cb,base);
else res=ca_c(cb,ca,base);
return res;
}
void show(int r){
cout<<r;
}
int ca_c(list<int>& l1,list<int>& l2,int base){
int res=0;
list<int>::iterator j=l2.begin();
int k=0;
for(list<int>::iterator i=l1.begin();i!=l1.end();++i,++j,++k)
res+=((*i+*j)%base)*pow(double(base),double(k));
for(;j!=l2.end();++j,++k)
res+=(*j%base)*pow(double(base),double(k));
return res;
}