-
题目描述:
-
给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。
-
输入:
-
输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。
-
输出:
-
请计算A+B的结果,并以正常形式输出,每组数据占一行。
-
样例输入:
-
-234,567,890 123,456,789 1,234 2,345,678
-
样例输出:
-
-111111101 2346912
第一种方法(自己写的,说实话,OJ真的很坑爹,真机调试一点问题都没有的,到上面就是不行。也是醉了):
#include<iostream>
#include <string>
using namespace std;
int Num(char N) //Áé¸ÐÀ´×ÔµÚ1010Ìâ
{
if (N=='0')
{
return 0;
}
if (N=='1')
{
return 1;
}
if (N=='2')
{
return 2;
}
if (N=='3')
{
return 3;
}
if (N=='4')
{
return 4;
}
if (N=='5')
{
return 5;
}
if (N=='6')
{
return 6;
}
if (N=='7')
{
return 7;
}
if (N=='9')
{
return 9;
}
else
return -1;
}
int main()
{
int i,j;
string str1;
string str2;
long num1=0,num2=0;
while (cin>>str1>>str2)
{
for (i=0;i<str1.length();i++)
{
if (str1[i]==','||str1[i]=='-') //²îµã¾ÍºöÂÔ¸ºÊýµÄʱºò£¡ÒòΪÊǶÔ×Ö·ûÊý×é½øÐвÙ×÷£¬ËùÒԵĿ¼ÂÇ·ûºÅ
//if(str1[i]>='9'&&str1[i]<='0')
{
continue;
// num1=num1*10+str1[i]-'0';
}
else
num1=num1*10+Num(str1[i]);
}
for (j=0;j<str2.length();j++)
{
//if (str1[j]>='9'&&str1[j]<='0')
if (str1[j]==','||str1[j]=='-')
{
continue;
// num2=num2*10+str2[j]-'0';
}
else
num2=num2*10+Num(str2[j]);
}
if (str1[0]!='-'&&str2[0]!='-') //ÒòΪÓиººÅµÄÔµ¹Ê£¬ËùÒÔÒªÅжÏÒ»ÏÂ
{
cout<<num1+num2<<endl;
}
if (str1[0]=='-'&&str2[0]!='-')
{
cout<<num2-num1<<endl;
}
if (str1[0]!='-'&&str2[0]=='-')
{
cout<<num1-num2<<endl;
}
num1=0;num2=0;
}
return 0;
}
第二种方法:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1,str2;
while(cin>>str1>>str2)
{
long num1 = 0;
int i ;
for(i=0; i < str1.length(); i++)
{
if(str1[i] <= '9' && str1[i] >= '0')
{
num1 = num1*10 + str1[i] - '0';
}
}
long num2 = 0;
for(i=0; i < str2.length(); i++)
{
if(str2[i] <= '9' && str2[i] >= '0')
{
num2 = num2*10 + str2[i] - '0';
}
}
//++
if(str1[0] != '-' && str2[0] != '-')
{
cout<<num1+num2<<endl;
}
//+-
if(str1[0] != '-' && str2[0] == '-')
{
cout<<num1-num2<<endl;
}
//-+
if(str1[0] == '-' && str2[0] != '-')
{
cout<<num2-num1<<endl;
}
//--
if(str1[0] == '-' && str2[0] == '-')
{
cout<<0-(num1+num2)<<endl;
}
}
return 0;
}
/**************************************************************
Problem: 1003
User: Carvin
Language: C++
Result: Accepted
Time:0 ms
Memory:1520 kb
****************************************************************/