题目描述
给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。 现在请计算A+B的结果,并以正常形式输出。
输入描述:
输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。
输出描述:
请计算A+B的结果,并以正常形式输出,每组数据占一行。
示例1
输入
-234,567,890 123,456,789
1,234 2,345,678
输出
-111111101
2346912
题目解析:首先提取出原数,注意提取符号正反,然后求和
代码:
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
int computer(string str, char fuhao){
int count = str.size() - 1;
int sum = 0;
for(int i = 0; i < str.size(); i++){
sum += (str[i] - '0') * pow(10,count);
count--;
}
if(fuhao == '-'){
return -sum;
}else{
return sum;
}
}
int main()
{
string str1,str2;
while(cin >> str1 >> str2){
char fuhao1 = str1[0];
char fuhao2 = str2[0];
int begin = 0;
if(fuhao1 == '-'){
begin = 1;
}
string newstr1;
for(int i = begin; i < str1.size(); i++){
if(str1[i] == ','){
continue;
}
newstr1 += str1[i];
}
int number1 = computer(newstr1,fuhao1);
string newstr2;
begin = 0;
if(fuhao2 == '-'){
begin = 1;
}
for(int i = begin; i < str2.size(); i++){
if(str2[i] == ','){
continue;
}
newstr2 += str2[i];
}
int number2 = computer(newstr2,fuhao2);
cout << (number1 + number2) << endl;
}
return 0;
}
/*
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<vector>
#include<regex>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
int computer(string number){
int sum = 0 , begin = 0 , count = 0;
if(number[0] == '-'){
begin = 1;
count = number.size() - 2 - ((number.size() - 1) / 4);
}else{
count = number.size() - (number.size() / 4) - 1;
}
for(int i = begin ; i < number.size() ; i++){
if(number[i] >= '0' && number[i] <= '9'){
sum += (number[i] - '0') * pow(10 , count);
count--;
}
}
if(begin == 1){
return -sum;
}else{
return sum;
}
}
int main()
{
string str1 , str2;
while(cin >> str1 >> str2){
int a = computer(str1);
int b = computer(str2);
cout << (a + b) << endl;
}
return 0;
}
*/