HDU 1002 分类: ACM 2015-06-18 23:03 9人阅读 评论(0) 收藏

昨天做的那题其实和大整数相加类似。记得当初我大一寒假就卡在这1002题上,结果之后就再也没写题。。。

到今天终于把大整数相加写了一遍。

不过写的很繁琐,抽时间改进一下写简洁一点。

#include<queue>

#include<math.h>

#include<stdio.h>

#include<string.h>

#include<iostream>

#include<algorithm>

using namespace std;

#define N 1215

char a[N],b[N],c[N];

char e[N],d[N];

void reverse(char a[])

{

    int len=strlen(a);

    for(int i=0;i<len/2;i++)

    {

        swap(a[i],a[len-i-1]);

    }

}

int main()

{

    int T,Case=1;

    cin>>T;

    while(T--)

    {

        scanf("%s%s",a,b);

        int len_a=strlen(a);

        int len_b=  strlen(b);

        strcpy(e,a);

        strcpy(d,b);

        reverse(a);

        reverse(b);

        int jin=0;

        for(int i=0;i<max(len_a,len_b);i++)

        {

//            cout<<b[i]<<endl;

            if(a[i]=='\0')

            {

                if(jin)

                {

                    c[i]=b[i]-'0'+1;

                    jin=0;

                    if(c[i]>=10)

                    {

                        if(i==max(len_a,len_b)-1)

                        {

                            c[i]=c[i]-10+'0';

                            c[i+1]='1';

                            c[i+2]=0;

                        }

                        else

                        {

                            c[i]=c[i]-10+'0';

                            jin=1;

                        }

                    }

                    else

                    {

                        c[i]=c[i]+'0';

                    }

                }

                else

                {

                    c[i]=b[i];

                    jin=0;

                }



            }

            else if(b[i]=='\0')

            {

//                printf("bbbbbjin=%d\n",jin);

                if(jin)

                {

                    c[i]=a[i]-'0'+1;

                    jin=0;

                    if(c[i]>=10)

                    {

                        if(i==max(len_a,len_b)-1)

                        {

                            c[i]=c[i]-10+'0';

                            c[i+1]='1';

                            c[i+2]=0;

                        }

                        else

                        {

                            c[i]=c[i]-10+'0';

                            jin=1;

                        }

                    }

                    else

                    {

                        c[i]=c[i]+'0';

                    }

                }

                else

                {

                    c[i]=a[i];

                    jin=0;

                }

            }

//            printf("%c %c %c\n",a[i],b[i],jin);

            else {



//            printf("%c %c\n",a[i],b[i]);

            c[i]=a[i]+b[i]+jin-'0'*2;

//            printf(" *  %d \n",c[i]);

            jin=0;

            if(c[i]>=10)

            {

                if(i==max(len_a,len_b)-1)

                {

                    c[i]=c[i]-10+'0';

                    c[i+1]='1';

                    c[i+2]=0;

                }

                else

                {

                    c[i]=c[i]-10+'0';

                    jin=1;

//                    printf("jin=%d",jin);

                }

            }

            else

                c[i]=c[i]+'0';

//            printf(" #  %d %c\n",c[i],c[i]);

        }



        }



        for(int i=strlen(c)-1;i>0;i--)

        {

            if(c[i]=='0')

                c[i]='\0';

            else

            {

                break;

            }

        }

        reverse(c);

        printf("Case %d:\n",Case++);

        printf("%s + %s = %s\n",e,d,c);

        if(T)puts("");

        memset(a,0,sizeof(a));

        memset(b,0,sizeof(b));

        memset(c,0,sizeof(c));

        memset(d,0,sizeof(d));

        memset(e,0,sizeof(e));



    }

    return 0;

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

上一篇:HDU 2034 人见人爱A-B 分类: ACM 2015-06-23 23:42 9人阅读 评论(0) 收藏


下一篇:使用OPC的方式去连接PLC进行AB SLC-5_04数据的采集