| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 436 人关注过本帖
标题:超长整型数相加
只看楼主 加入收藏
香菱
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2014-4-4
结帖率:83.33%
收藏
已结贴  问题点数:10 回复次数:4 
超长整型数相加
#include <stdio.h>
#include <string.h>
#include<math.h>
char a[102],b[102];
int pd(char e[],char f[],int m,int n,int p)
{
        while(m<=p){
    if(e[m]>f[n])
        return 0;
    else if(e[m]<f[n])
        return 1;
    else{
        m++;
        n++;
        pd(e,f,m,n,p);
      
        }
        }
    return 1;
}
int main()
{
    int len1,len2;
    void zs(char a[],char b[],int len1,int len2);
    void fs(char a[],char b[],int len1,int len2);
    scanf("%s%s",a,b);
    len1=strlen(a)-1;
    len2=strlen(b)-1;
    if(a[0]!='-'&&b[0]!='-')
        zs(a,b,len1,len2);
    else
        fs(a,b,len1,len2);
    return 0;
}
void zs(char a[],char b[],int len1,int len2)
{
    char sum[102],c[102],d[102];
    int i=0,count=0,m1,m2,h;
    while(len1>=0||len2>=0){
        if(len1<0)
            c[i]='0';
        else
            c[i]=a[len1];
        if(len2<0)
            d[i]='0';
        else
            d[i]=b[len2];
        m1=c[i]-'0';
        m2=d[i]-'0';
        h=m1+m2+count;
        count=h/10;
        sum[i]=h%10+'0';
        len1--;
        len2--;
        i++;
    }
   if(h>=10)
      printf("1");
    for(i--;i>=0;i--)
       printf("%c",sum[i]);
    printf("\n");
}
void fs(char a[],char b[],int len1,int len2)
{
    char c[102],d[102],sum[103],e[102],f[102];
    int i=0,count=0,m1,m2,h,len3,len4,p,q,t=1,s,len5,len6;
    if(a[0]=='-'&&b[0]=='-')
    {
        strcpy(e,a+1);
        strcpy(f,b+1);
        len5=len3=strlen(e)-1;
        len6=len4=strlen(f)-1;
       while(len3>=0||len4>=0){
        if(len3<0)
            c[i]='0';
        else
            c[i]=e[len3];
        if(len4<0)
            d[i]='0';
        else
            d[i]=f[len4];
        m1=c[i]-'0';
        m2=d[i]-'0';
        h=m1+m2+count;
        count=h/10;
        sum[i]=h%10+'0';
        len3--;
        len4--;
        i++;
    }
   if((len5==len6)&&(e[0]=='0')&&(f[0]=='0'));
   else
      printf("-");
   if(h>=10)
      printf("1");
    for(i--;i>=0;i--)
       printf("%c",sum[i]);
    printf("\n");
    }
/***************************************************/
    else
    {
        if(a[0]=='-'&&b[0]!='-'){
        strcpy(e,a+1);
        p=len3=strlen(e)-1;
        strcpy(f,b);
        q=len4=len2;
        s=pd(e,f,i,i,p);
        while(len3>=0||len4>=0){
           if(len3<0)
                c[i]='0';
        else
            c[i]=e[len3];
        if(len4<0)
            d[i]='0';
        else
            d[i]=f[len4];
        m1=c[i]-'0';
        m2=d[i]-'0';
        if(p==1&&q==1){
            h=m1-m2;}
        else if(len3==len4){
            if(s==0){
                if(m1-count<m2){
                    h=m1+10-m2-count;
                    count=1;}
                else{
                    h=m1-m2-count;
                    count=0;}}
            else{
                if(m2-count<m1){
                    h=m2+10-m1-count;
                    count=1;}
                else{
                    h=m2-m1-count;
                    count=0;}}
        }
        else{
            if(len3>len4)
            {
                if(m1-count<m2){
                    h=m1+10-m2-count;
                    count=1;}
                else{
                    h=m1-m2-count;
                    count=0;}
            }
            else
            {
                 if(m2-count<m1){
                    h=m2+10-m1-count;
                    count=1;}
                 else{
                    h=m2-m1-count;
                    count=0;}  
            }
        }
        sum[i]=(int)fabs(h)+'0';
        len3--;
        len4--;
        i++;
        }
        if(p==1&&q==1&&m2<m1)
            printf("-");
        else if(len3==len4){
            if(s==0){
                printf("-");}}
        else{
            if(len3>len4)
                printf("-");}
        for(i--;i>=0;i--)
        {
            while(t&&i)
            {
                if(sum[i]=='0')
                    i--;
                else{
                  t=0;
                }
            }
            while(t==0&&i>0||i==0)
            {
            printf("%c",sum[i]);
            i--;
        }}
        printf("\n");
        }
/*****************************************************/
        if(b[0]=='-'&&a[0]!='-'){
        strcpy(e,b+1);
        strcpy(f,a);
        p=len3=strlen(e)-1;
        q=len4=len1;
        s=pd(e,f,i,i,p);
        while(len3>=0||len4>=0){
           if(len3<0)
                c[i]='0';
        else
            c[i]=e[len3];
        if(len4<0)
            d[i]='0';
        else
            d[i]=f[len4];
        m1=c[i]-'0';
        m2=d[i]-'0';
        if(p==1&&q==1){
            h=m1-m2;}
        else if(len3==len4){
            if(s==0){
                if(m1-count<m2){
                    h=m1+10-m2-count;
                    count=1;}
                else{
                    h=m1-m2-count;
                    count=0;}}
            else{
                if(m2-count<m1){
                    h=m2+10-m1-count;
                    count=1;}
                else{
                    h=m2-m1-count;
                    count=0;}}
        }
        else{
            if(len3>len4)
            {
                if(m1-count<m2){
                    h=m1+10-m2-count;
                    count=1;}
                else{
                    h=m1-m2-count;
                    count=0;}
            }
            else
            {
                 if(m2-count<m1){
                    h=m2+10-m1-count;
                    count=1;}
                 else{
                    h=m2-m1-count;
                    count=0;}  
            }
        }
        sum[i]=(int)fabs(h)+'0';
        len3--;
        len4--;
        i++;
        }
        if(p==1&&q==1&&m2<m1)
            printf("-");
        else if(len3==len4){
            if(s==0){
                printf("-");}}
        else{
            if(len3>len4)
                printf("-");}
        for(i--;i>=0;i--){
                while(t&&i)
            {
                    if(sum[i]=='0')
                        i--;
                    else{
                  t=0;
                    }
            }
            while(t==0&&i>0||i==0)
            {
            printf("%c",sum[i]);
            i--;
        }}
        printf("\n");
        }
        }
        }题目,输入两个数a,b,输出a+b的值,(|a|,|b|<=10^100),这个代码太长,有个测试用例老超时,求解,谢谢了
搜索更多相关主题的帖子: include return 
2014-05-07 22:29
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:4 
不懂

我不是砖家,要努力成为砖家。
2014-05-07 23:53
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:4 
两个大数相加用不了这么长的代码
你再重新挼一下思路先

DO IT YOURSELF !
2014-05-08 06:37
top398
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:427
专家分:857
注 册:2014-5-2
收藏
得分:4 
OMG orz
2014-05-08 22:55
top398
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:427
专家分:857
注 册:2014-5-2
收藏
得分:0 
又大概看了一下撸主的代码,觉得函数 pd 是否滥用递归呢?且在 main() 中声明两个函数的原型,也是权威的一大发明创造。

另外,撸主并未举例在什么情况下超时(搞不好就是递归出了问题)?因此不能继续深究了。
2014-05-09 09:33
快速回复:超长整型数相加
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035700 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved