码迷,mamicode.com
首页 > 其他好文 > 详细

题目1003:A+B

时间:2014-12-05 22:30:50      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   for   strong   

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

 

Code:

#include <iostream>
#include <string.h>
 
using namespace std;
 
int func(int x){
    int ans=1;
    for(int i=1;i<=x;i++){
        ans*=10;
    }
    return ans;
}
 
long toLong(char buf[]){
    int len=strlen(buf);
    int cnt=0;
    long ans=0;
    int i=len-1;
    while(i>=1){
        if(buf[i]>=0&&buf[i]<=9){
            ans+=(buf[i]-0)*func(cnt);
            ++cnt;
        }
        --i;
    }
    /*判别是否是负数*/
    if(buf[0]==-){
        ans=-ans;
    }
    if(buf[0]>=0&&buf[0]<=9){
        ans+=(buf[i]-0)*func(cnt);
    }
    return ans;
}
 
int main()
{
    char a[15],b[15];
    while(cin>>a>>b){
        long ans_a=toLong(a);
        long ans_b=toLong(b);
        cout<<ans_a+ans_b<<endl;
    }
    return 0;
}
 
/**************************************************************
    Problem: 1003
    User: lcyvino
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/

 

 

 

题目1003:A+B

标签:style   blog   io   ar   color   os   sp   for   strong   

原文地址:http://www.cnblogs.com/Murcielago/p/4147556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!