记录编号 |
70367 |
评测结果 |
AAAAAAAAAA |
题目名称 |
增强的加法问题 |
最终得分 |
100 |
用户昵称 |
Gain |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.004 s |
提交时间 |
2013-09-26 21:54:21 |
内存使用 |
0.31 MiB |
显示代码纯文本
#include <string>
#include<fstream>
using namespace std;
ifstream fi("add.in");
ofstream fo("add.out");
string AddStr( string s1, string s2 )
{
if( s1.length() > s2.length() )
{
string ts = s1;
s1 = s2;
s2 = ts;
}
int i ;
int j ;
for( i = s2.length()-1,j = s1.length()-1 ; j>=0; j--, i-- )
{
int index = i;
int sum = (s2[i]-'0') + ( s1[j]-'0' );
while( sum>=10 )
{
s2[index] = (char)(sum%10 + '0' );
if( index <= 0 )
{
s2 = "1"+s2;
i++;
index++;
break;
}
index--;
sum = (s2[index]-'0') + 1;
}
s2[index] = (char)( sum%10 + '0' );
}
return s2;
}
void split( string &II, string &FF, string s )
{
int i;
bool mark = false;
for( i=0; i<s.length(); i++ )
{
if( s[i]=='.' )
{
mark = true;
II = s.substr( 0, i );
FF = s.substr( i+1, s.length()-i-1 );
return;
}
}
if( mark==false )
{
II = s;
FF = "";
}
}
string updateFF( string fs )
{
int i=fs.length()-1;
for( ; i>=0; i-- )
{
if( fs[i]=='0' )
{
fs.erase( fs.begin()+i );
}
else
{
break;
}
}
return fs;
}
int main()
{
string s1,s2;
while( fi>>s1>>s2 )
{
string I1, F1, I2, F2;
string FF, II;
split( I1, F1, s1 );
split( I2, F2, s2 );
int lenF1 = F1.length();
int lenF2 = F2.length();
if( lenF1==0 )
{
FF = F2;
}
if( lenF2==0 )
{
FF = F1;
}
if( lenF1==lenF2 && lenF1!=0 && lenF2!=0 )
{
FF = AddStr( F1,F2 );
}
else if( lenF1 > lenF2 && lenF2!=0)
{
string subF1 = F1.substr( 0, lenF2 );
FF = AddStr(subF1, F2) + F1.substr( lenF2, lenF1 - lenF2 );
}
else if( lenF1 < lenF2 && lenF1!=0 )
{
string subF2 = F2.substr( 0, lenF1 );
FF = AddStr(subF2, F1) + F2.substr( lenF1, lenF2 - lenF1 );
}
string jinWei="";
if( FF.length() > lenF1 && FF.length() > lenF2 )
{
jinWei = FF[0];
FF.erase( FF.begin() + 0 );
}
II = AddStr( I1, I2 );
II = AddStr( II, jinWei );
fo<<II;
FF = updateFF( FF );
if( FF.length()>0 )
{
fo<<"."<<FF;
}
fo<<endl;
}
return 0;
}