A+B

题目描述
给定两个整数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;
} 

*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值