#include <iostream>
#include <string>
#include <cstring>
using namespace std;
string add(string add1,string add2)
{
int a=add1.length();
int b=add2.length();
int temp;
if(a>b)
{
for (int i=0;i<a-b;i++)
{
add2='0'+add2;
}
add1='0'+add1;
add2='0'+add2;
}
else if(a==b)
{
add1='0'+add1;
add2='0'+add2;
}
else if(a<b)
{
for (int i=0;i<b-a;i++)
{
add1='0'+add1;
}
add1='0'+add1;
add2='0'+add2;
}
int up=0;
for (int i=add1.length()-1;i>=0;i--)
{
temp=(add1[i]-'0')+(add2[i]-'0')+up;
if(temp<10)
{
up=0;
add1[i]=temp+'0';
continue;
}
else{
up=1;
temp=temp%10;
add1[i]=temp+'0';
continue;
}
}
while(add1[0]=='0')
{
if(add1.length()==1) break;
add1=add1.substr(1,add1.length()); //cut the subString of original String.
}
return add1;
}
int main()
{
string mu1,mu2;
string ffake;
int number;
cin>>number;
while(number--)
{
cin>>mu1>>mu2;
string ini="0";
for (int i=0;i<mu2.length();i++)
{
int buwei=mu2.length()-i-1;
string fakemu1=mu1;
for(int j=0;j<buwei;j++)
{
fakemu1=fakemu1+'0'; //big error!!
}
if(mu2[i]=='0') continue; //big error!! haven't consider the case of 0 and 1
else if(mu2[i]=='1')
{
ffake=fakemu1;
}
else
{
int nummu2=mu2[i]-'0'-1;
ffake=fakemu1;
while (nummu2--)
{
ffake=add(ffake,fakemu1);
}
}
ini=add(ini,ffake);
}
while(ini[0]=='0'){
if(ini.length()==1) break;
ini=ini.substr(1,ini.length());
}
cout<<ini<<endl;
}
return 0;
}
略坑爹,在string操作加法基础上封装add加法操作,将乘法分解成加。。
1. 注意对string类型的“0000098” 去0,操作:
while(str[0]=='0'){
str=str.substr(1,str.length()); //cut from pos.1
}
2. String类型 首位/尾部 添0 :
str='0'+str+"0000000000";
3. char转int
int a=char[i]-'0';