hdu 1753 题目链接
大明A+B
Time Limit:
1000MS
Memory Limit:
32768KB
64bit IO Format:
%I64d & %I64u
Description
话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。
这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。
现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。
这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。
现在,给你两个正的小数A和B,你的任务是代表大明计算出A+B的值。
Input
本题目包含多组测试数据,请处理到文件结束。
每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。
每一组测试数据在一行里面包含两个长度不大于400的正小数A和B。
Output
请在一行里面输出输出A+B的值,请输出最简形式。详细要求请见Sample Output。
Sample Input
1.1 2.9 1.1111111111 2.3444323343 1 1.1
Sample Output
4 3.4555434454 2.1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
string add(string a,string b)
{
string ans;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int m=0,carry=0,i=0;
while(a[i]&&b[i])
{
m=a[i]-'0'+b[i]-'0'+carry;
carry=m/10;
ans+=m%10+'0';
i++;
}
if(i==a.size())
{
while(i!=b.size())
{
m=b[i]-'0'+carry;
carry=m/10;
ans+=m%10+'0';
i++;
}
if(carry)
ans+=carry+'0';
}
else if(i==b.size())
{
while(i!=a.size())
{
m=a[i]-'0'+carry;
carry=m/10;
ans+=m%10+'0';
i++;
}
if(carry)
ans+=carry+'0';
}
reverse(ans.begin(),ans.end());
return ans;
}
int main()
{
string a,b;
string a1,a2,b1,b2;
int i,j;
while(cin>>a>>b)
{
a1="",a2="",b1="",b2="";
int len_a,len_b,l1,l2;
int len1=a.size(),len2=b.size();
for(i=0; a[i]!='.'&&i<len1; i++)
a1+=a[i];
while(*(a1.begin())=='0'&&a1.size()>1)
a1.erase(a1.begin());
for(j=i+1; j<len1; j++)
a2+=a[j];
if(a2=="")
a2="0";
len_a=a2.size();
for(i=0; b[i]!='.'&&i<len2; i++)
b1+=b[i];
while(*(b1.begin())=='0'&&b1.size()>1)
b1.erase(b1.begin());
for(j=i+1; j<len2; j++)
b2+=b[j];
if(b2=="")
b2="0";
len_b=b2.size();
l1=len_a,l2=len_b;
while(l1<l2)
{
a2=a2+"0";
l1++;
}
while(l2<l1)
{
b2=b2+"0";
l2++;
}
// cout<<a1<<endl<<a2<<endl<<b1<<endl<<b2<<endl;
string sum1="",sum2="",sum_1="";
sum1=add(a2,b2);
int
len_sum1=sum1.size();
if(len_sum1>len_a&&len_sum1>len_b)
{
sum2=add(add(a1,b1),"1");
// while(*(sum2.begin())=='0'&&sum2.size()>1)
// sum2.erase(sum2.begin()); //erase.
for(i=1; i<len_sum1; i++)
sum_1+=sum1[i];
int len=sum_1.size();
for( i=len-1; i>0; )
if(sum_1[i]=='0')
i--;
else
break;
sum_1.erase(sum_1.begin()+i+1,sum_1.end());
}
else
{
sum2=add(a1,b1);
// while(*(sum2.begin())=='0'&&sum2.size()>1)
// sum2.erase(sum2.begin());//erase.
sum_1=sum1;
int len=sum_1.size();
for( i=len-1; i>0; )
if(sum_1[i]=='0')
i--;
else
break;
sum_1.erase(sum_1.begin()+i+1,sum_1.end());
}
if(sum_1=="0")
cout<<sum2<<endl;
else
cout<<sum2<<'.'<<sum_1<<endl;
}
return 0;
}
本题测试数据(来自其他博客)
1.1 2.9
1.1111111111 2.34443233434
1 1.1
21.3423 2.13423
23432.32 1232
222 232
19.2 12.1
19.2 12.9
2013.2013 3029.3029
99.9 99.9
0.99 99.9
99.9 0.1
99.99 009.11
00098.8 3.32
1.2 1.9
0.1 0.9
1.0 0.10
1.0001 0010.00100
输出
4
3.45554344544
2.1
23.47653
24664.32
454
31.3
32.1
5042.5042
199.8
100.89
100
109.1
102.12
3.1
1
1.1
11.0011
大数题目需要考虑:
1.前导零
2.小数点之后的数值如何保留
1)保留几位
2)若全为0是否舍去
定义的字符串为string型,如何去除前导零:
代码:
while(*(a1.begin())=='0'&&a1.size()>1)
a1.erase(a1.begin());
代码:
int len=sum_1.size();
for( i=len-1; i>0; )
if(sum_1[i]=='0')
i--;
else
break;
sum_1.erase(sum_1.begin()+i+1,sum_1.end());
/***************************************************************************************************************************************************************************************/
STL--erase函数(转载)
erase函数的原型如下:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
也就是说有三种用法:
(1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
(2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
(3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)
下面给你一个例子:#include < iostream > #include < string > using namespace std; int main () { string str ( " This is an example phrase. " ); string ::iterator it; // 第(1)种用法 str.erase ( 10 , 8 ); cout << str << endl; // "This is an phrase." // 第(2)种用法 it = str.begin() + 9 ; str.erase (it); cout << str << endl; // "This is a phrase." // 第(3)种用法 str.erase (str.begin() + 5 , str.end() - 7 ); cout << str << endl; // "This phrase." return 0 ; }