#include <iostream>
#include <list>
using namespace std;
void Calc(int base, int num)
{
list<int> sum;
int carry = 0; //进位
int ones = 0;
while(num > 0)
{
int mul = num *base + carry;
carry = mul/10;
ones = mul%10;
sum.push_front(ones);
num--;
}
while(carry > 0)
{
ones = carry % 10;
sum.push_front(ones);
carry = carry /10;
}
for(list<int>::iterator itr = sum.begin();itr != sum.end();++itr)
{
cout<<*itr;
}
cout<<endl;
}
int main()
{
int a,n;
cout<<"请输入a的值:"<<endl;
cin>>a;
cout<<"请输入n的值:"<<endl;
cin>>n;
Calc(a,n);
return 0;
}
算法采用了大数相加的思想,可以解决a值较大的情况下可能产生的数据溢出问题。
#include <list>
using namespace std;
void Calc(int base, int num)
{
list<int> sum;
int carry = 0; //进位
int ones = 0;
while(num > 0)
{
int mul = num *base + carry;
carry = mul/10;
ones = mul%10;
sum.push_front(ones);
num--;
}
while(carry > 0)
{
ones = carry % 10;
sum.push_front(ones);
carry = carry /10;
}
for(list<int>::iterator itr = sum.begin();itr != sum.end();++itr)
{
cout<<*itr;
}
cout<<endl;
}
int main()
{
int a,n;
cout<<"请输入a的值:"<<endl;
cin>>a;
cout<<"请输入n的值:"<<endl;
cin>>n;
Calc(a,n);
return 0;
}
算法采用了大数相加的思想,可以解决a值较大的情况下可能产生的数据溢出问题。