| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 436 人关注过本帖
标题:超长整型数相加
取消只看楼主 加入收藏
香菱
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2014-4-4
结帖率:83.33%
收藏
已结贴  问题点数:10 回复次数:0 
超长整型数相加
#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
快速回复:超长整型数相加
数据加载中...
 
   



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

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