A+B HDU3787

A+B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3225    Accepted Submission(s): 1796


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
对字符串的处理-----注意一下负号的处理就好了
#include <stdio.h>
#include <string.h>

int n,flag1,n1,n2,len1,flag2,len2,i;
char number1[20];
char number2[20];
int main()
{
    while(scanf("%s",number1) != EOF && scanf("%s",number2) != EOF)
    {
        n1 = n2 = 0;
        len1 = strlen(number1);
        len2 = strlen(number2);
        if(number1[0] == '-') flag1 = -1;else flag1 =1;
        if(number2[0] == '-') flag2 = -1;else flag2 = 1;

       for(i = 0; i < len1 ;i++)
       {
           if(number1[i] >= '0' && number1[i] <='9')
            n1 = n1 * 10 +number1[i] - '0';
       }

       for(i = 0; i <len2 ;i++)
       {
           if(number2[i] >= '0' && number2[i] <='9')
            n2 = n2 * 10 +number2[i] - '0';
       }
      n1*=flag1;n2*=flag2;
      printf("%d\n",n1+n2);
    }
     return 0;
}

你可能感兴趣的:(C语言,ACM,HDU)