zoukankan      html  css  js  c++  java
  • 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]=='')
                {
                    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]=='')
                {
    //                printf("bbbbbjin=%d ",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 ",a[i],b[i],jin);
                else {

    //            printf("%c %c ",a[i],b[i]);
                c[i]=a[i]+b[i]+jin-'0'*2;
    //            printf(" *  %d ",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 ",c[i],c[i]);
            }

            }

            for(int i=strlen(c)-1;i>0;i--)
            {
                if(c[i]=='0')
                    c[i]='';
                else
                {
                    break;
                }
            }
            reverse(c);
            printf("Case %d: ",Case++);
            printf("%s + %s = %s ",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;
    }


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

  • 相关阅读:
    ExtJS+DWR+Spring+Hibernate开发HRMS(5)
    ExtJS实战(9)疑难杂症分析
    ExtJS实战(8)CRUD+分页+复杂查询+排序
    ExtJS实战(7)登陆
    ExtJS+DWR+Spring+Hibernate开发HRMS(1)
    ExtJS+DWR+Spring+Hibernate开发HRMS(3)
    ExtJS实战(10)项目总结
    ExtJS+DWR+Spring+Hibernate开发HRMS(2)
    Hibernate QBC高级查询
    ExtJS+DWR+Spring+Hibernate开发HRMS(4)
  • 原文地址:https://www.cnblogs.com/wmxl/p/4662701.html
Copyright © 2011-2022 走看看