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

POJ 1503 Integer Inquiry

时间:2014-08-13 10:38:25      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:高精度

高精度加法。水。


给一长串的数字,加起来就好。


#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
#define LL long long

#define CLRi for(int i=0;i<n;i++)
#define CLRj for(int j=0;j<n;j++)
#define CLRk for(int k=0;k<n;k++)

#define debug puts("==fuck==");

#define acfun std::ios::sync_with_stdio(false)
#define Nmax 1001
#define Mmax 1001*1001
using namespace std;

char s1[1001],s2[1001];
void Add()
{
    stack<int>a;
    stack<int>b;
    stack<int>out;
    for(int i=0; s1[i]!='\0'; i++)
        a.push(s1[i]-'0');
    for(int i=0; s2[i]!='\0'; i++)
        b.push(s2[i]-'0');
    int na,nb,c=0;
    while(!a.empty()||!b.empty())
    {
        if(!a.empty())
        {
            na=a.top();
            a.pop();
        }
        else
            na=0;
        if(!b.empty())
        {
            nb=b.top();
            b.pop();
        }
        else
            nb=0;
        c+=na+nb;
        out.push(c%10);
        c/=10;
    }
    while(c)
    {
        out.push(c%10);
        c/=10;
    }
    int k=0;
    while(!out.empty())
    {
        int tmp=out.top();
        out.pop();
//        printf("%d",tmp);
        s1[k++]=tmp+'0';
    }
    s1[k]='\0';
}

int main()
{
    s1[0]='0';s1[1]='\0';
    while(scanf("%s",s2)!=EOF)
    {
        if(strcmp(s2,"0")==0)break;
        Add();
    }
    puts(s1);
}


POJ 1503 Integer Inquiry,布布扣,bubuko.com

POJ 1503 Integer Inquiry

标签:高精度

原文地址:http://blog.csdn.net/dongshimou/article/details/38531743

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