[HDOJ]1002.A + B Problem II
A+B问题的变形,用同学的话说就是模拟大数加法问题,用数据结构接受两个无法用32位或者64位表示的大整型数,然后模拟整数相加的过程,最后把结果输出。
我在这里用两种方法,准确的说是用两种不同的数据结构解决这个大数相加问题,前者用STL,数据结构偏C++些,后者用char型数组接收两个大数,数据结构偏C
风格些,整体的流程是一样的,具体可参见下面的代码。
用char型数组接收大数:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int n,i = 1;
char a[1000],b[1000]; //用来接收两个大数
cin>>n;
while(i <= n)
{
if(i != 1) //控制格式
cout<<endl;
cin>>a>>b;
int carry = 0; //表示进位
int size,tsize,size1 = strlen(a),size2 = strlen(b);
if(size1 > size2)
size = size1;
else
size = size2;
tsize = size;
int *c = new int[size]; //新建数组存放最终结果
size1 = size1 - 1;
size2 = size2 - 1;
size = size -1;
//以下的while循环都是用来模拟加法
while(size1 >= 0&&size2 >= 0)
{
c[size] = (a[size1] - '0' + b[size2] - '0' + carry)%10;
carry = (a[size1] - '0' + b[size2] - '0' + carry)/10;
--size1;
--size2;
--size;
}
while(size1 >= 0)
{
c[size] = (a[size1] - '0' + carry)%10;
carry = (a[size1] - '0' + carry)/10;
--size1;
--size;
}
while(size2 >= 0)
{
c[size] = (b[size2] - '0' + carry)%10;
carry = (b[size2] - '0' + carry)/10;
--size2;
--size;
}
cout<<"Case "<<i<<":"<<endl;
cout<<a<<" + "<<b<<" = ";
if(carry)
cout<<carry;
for(int t = 0;t<tsize;++t)
cout<<c[t];
cout<<endl;
++i;
delete []c;
}
}
#include <cstring>
using namespace std;
int main()
{
int n,i = 1;
char a[1000],b[1000]; //用来接收两个大数
cin>>n;
while(i <= n)
{
if(i != 1) //控制格式
cout<<endl;
cin>>a>>b;
int carry = 0; //表示进位
int size,tsize,size1 = strlen(a),size2 = strlen(b);
if(size1 > size2)
size = size1;
else
size = size2;
tsize = size;
int *c = new int[size]; //新建数组存放最终结果
size1 = size1 - 1;
size2 = size2 - 1;
size = size -1;
//以下的while循环都是用来模拟加法
while(size1 >= 0&&size2 >= 0)
{
c[size] = (a[size1] - '0' + b[size2] - '0' + carry)%10;
carry = (a[size1] - '0' + b[size2] - '0' + carry)/10;
--size1;
--size2;
--size;
}
while(size1 >= 0)
{
c[size] = (a[size1] - '0' + carry)%10;
carry = (a[size1] - '0' + carry)/10;
--size1;
--size;
}
while(size2 >= 0)
{
c[size] = (b[size2] - '0' + carry)%10;
carry = (b[size2] - '0' + carry)/10;
--size2;
--size;
}
cout<<"Case "<<i<<":"<<endl;
cout<<a<<" + "<<b<<" = ";
if(carry)
cout<<carry;
for(int t = 0;t<tsize;++t)
cout<<c[t];
cout<<endl;
++i;
delete []c;
}
}
用string类型接收大数:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
#endif
int i,n;
string str1,str2; //用来接收两个大数
vector<int> ivec; //用来存放最终结果
i = 1;
cin>>n;
while(i <= n)
{
ivec.clear();
str1.clear();
str2.clear();
cin>>str1>>str2;
int size1 = str1.size() - 1,size2 = str2.size() - 1;
int carry = 0; //carry表示进位
//模拟加法过程
while(size1 >= 0&&size2 >= 0)
{
int temp;
temp = (str1.at(size1) - '0' + str2.at(size2) - '0' + carry)%10;
carry = (str1.at(size1) - '0' + str2.at(size2) - '0' + carry)/10;
ivec.push_back(temp);
--size1;
--size2;
}
while(size1 >= 0)
{
int temp;
temp = (str1.at(size1) - '0' + carry)%10;
carry = (str1.at(size1) - '0' + carry)/10;
ivec.push_back(temp);
--size1;
}
while(size2 >= 0)
{
int temp;
temp = (str2.at(size2) - '0' + carry)%10;
carry = (str2.at(size2) - '0' + carry)/10;
ivec.push_back(temp);
--size2;
}
if(carry)
ivec.push_back(carry);
if(i != 1)
cout<<endl;
cout<<"Case "<<i<<":"<<endl;
cout<<str1<<" + "<<str2<<" = ";
for(int t = ivec.size() - 1;t >= 0;--t)
cout<<ivec.at(t);
cout<<endl;
++i;
}
return 0;
}
#include <string>
#include <vector>
using namespace std;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
#endif
int i,n;
string str1,str2; //用来接收两个大数
vector<int> ivec; //用来存放最终结果
i = 1;
cin>>n;
while(i <= n)
{
ivec.clear();
str1.clear();
str2.clear();
cin>>str1>>str2;
int size1 = str1.size() - 1,size2 = str2.size() - 1;
int carry = 0; //carry表示进位
//模拟加法过程
while(size1 >= 0&&size2 >= 0)
{
int temp;
temp = (str1.at(size1) - '0' + str2.at(size2) - '0' + carry)%10;
carry = (str1.at(size1) - '0' + str2.at(size2) - '0' + carry)/10;
ivec.push_back(temp);
--size1;
--size2;
}
while(size1 >= 0)
{
int temp;
temp = (str1.at(size1) - '0' + carry)%10;
carry = (str1.at(size1) - '0' + carry)/10;
ivec.push_back(temp);
--size1;
}
while(size2 >= 0)
{
int temp;
temp = (str2.at(size2) - '0' + carry)%10;
carry = (str2.at(size2) - '0' + carry)/10;
ivec.push_back(temp);
--size2;
}
if(carry)
ivec.push_back(carry);
if(i != 1)
cout<<endl;
cout<<"Case "<<i<<":"<<endl;
cout<<str1<<" + "<<str2<<" = ";
for(int t = ivec.size() - 1;t >= 0;--t)
cout<<ivec.at(t);
cout<<endl;
++i;
}
return 0;
}
在用STL解决问题时,还遇到了关于string::size_type的一些小的细节问题,以前真没注意过,可参见我博客的这篇文章,仅供以后注意。
我没有什么雄心壮志,我只想给自己和关心自己的家人和朋友一个交代,仅此而已。