HDU3787 A+B

HDU3787
A+B
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6951 Accepted Submission(s): 3803

Problem Description
给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。
现在请计算A+B的结果,并以正常形式输出。

Input
输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。

Output
请计算A+B的结果,并以正常形式输出,每组数据占一行。

Sample Input
-234,567,890 123,456,789
1,234 2,345,678

Sample Output
-111111101
2346912

Source
浙大计算机研究生复试上机考试-2010年

AB大小有限制,所以最后计算可以直接用int加减法
输入含有’ - ’ , ’ , ’ 肯定是要用字符串保存输入的内容,再通过遍历将负数提出,逗号去掉,这样出来的字符串的长度才是真正的位数,就方便操作了。

这题比较顺利,一次过

#include
#include
#include
#include
using namespace std;
int toint(char sub[],int len);
int main(){
	char a[101];
	char b[101];
	while(scanf("%s %s",&a,&b)!=EOF){
		int alen = strlen(a);
		int blen = strlen(b);
		int inta;
		inta = toint(a,alen);
//		printf("%d",inta);
		int intb = toint(b,blen);
		printf("%d\n",inta+intb);
	}
	return 0;
}
int toint(char sub[],int len){
	int num = 0;
	int pos = 0;
	int lenth = len - 1;
	for(int i = 0 ;i

你可能感兴趣的:(HOJ)