Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

晕了,WA都N次了,谁能帮我看看@@

Posted by lefttime at 2007-07-05 07:54:54 on Problem 1396
#include <stdio.h>
#include <malloc.h>
#include <string.h>
char sign,t[1024],a[501],b[501],c[501];
void add()
{
    int i,j,k,len,lena,lenb;
    char *s,*pmax,*pmin;
    lena=strlen(a);lenb=strlen(b);
    pmax=a;pmin=b;
    if(lena<lenb)
    {
        lena^=lenb;
        lenb^=lena;
        lena^=lenb;
        pmax=b;pmin=a;
    }
    s=(char *)malloc(sizeof(char)*(lena+2));
    s[0]='0';s[lena+1]='\0';
    for(i=lena-1,j=lenb-1,k=lena;j>=0;i--,j--,k--)
        s[k]=pmax[i]-'0'+pmin[j];
    for(;i>=0;i--,k--)
        s[k]=pmax[i];
    for(i=lena;i>0;i--)
        if(s[i]>'9')
        {
            s[i]-=10;
            s[i-1]++;
        }
    len=lena+1;
    if(s[k]=='0') len=lena;
    lena=strlen(a);lenb=strlen(b);
    if((lenb+1)>len) k=1;
    else k=0;
    for(i=0;i<len-lena+k;i++) printf(" ");printf("%s\n",a);
    for(i=0;i<len-lenb-1+k;i++) printf(" ");printf("+%s\n",b);
    for(i=0;i<len+k;i++) printf("-");
    printf("\n");
    pmax=s;
    while(*pmax=='0'&&*pmax!='\0') pmax++;
    if(*pmax=='\0') pmax--;
    if(k) printf(" ");
    printf("%s\n",pmax);
    free(s);
}
void subtract()
{
    int i,j,k,flag=0,len,lena,lenb,max_len;
    char *s,*pmax,*pmin;
    lena=strlen(a);lenb=strlen(b);
    pmax=a;pmin=b;
    if(lena<lenb||(lena==lenb&&strcmp(a,b)<0))
    {
        lena^=lenb;
        lenb^=lena;
        lena^=lenb;
        pmax=b;pmin=a;
        flag=1;
    }
    s=(char *)malloc(sizeof(char)*(lena+1));
    s[lena]='\0';
    for(i=k=lena-1,j=lenb-1;j>=0;i--,j--,k--)
        s[k]=pmax[i]-(pmin[j]-'0');
    for(;i>=0;i--,k--)
        s[k]=pmax[i];
    for(i=lena-1;i>0;i--)
        if(s[i]<'0')
        {
            s[i]+=10;
            s[i-1]--;
        }
    pmax=s;
    while(*pmax=='0'&&*pmax!='\0') pmax++;
    if(*pmax=='\0')
    {
        pmax--;
        *pmax='0';
    }
    len=strlen(pmax);
    if(lena==lenb) max_len=lena+1;
    else max_len=lena;
    if(flag)
    {
        len+=1;
        max_len++;
    }
    lena=strlen(a);lenb=strlen(b);
    for(i=0;i<max_len-lena;i++) printf(" ");printf("%s\n",a);
    for(i=0;i<max_len-lenb-1;i++) printf(" ");printf("-%s\n",b);
    for(i=0;i<max_len;i++) printf("-");
    printf("\n");
    for(i=0;i<max_len-len;i++) printf(" ");
    if(flag) printf("-");
    printf("%s\n",pmax);
    free(s);
}
void multiply()
{
    int i,j,k,m,n,len,lena,lenb,max_len,sub_len;
    long *s,*p,*t[501];
    lena=strlen(a);lenb=strlen(b);
    for(i=0;i<lenb;i++)
    {
        t[i]=(long *)malloc(sizeof(long)*(lena+1));
        for(j=0;j<lena;j++) t[i][j]=0;
    }
    s=(long *)malloc(sizeof(long)*(lena+lenb));
    for(i=0;i<lena+lenb;i++) s[i]=0;
    for(i=0;i<lenb;i++)
      for(j=0,k=1;j<lena;j++,k++)
          s[i+j+1]+=(t[i][k]=(b[i]-'0')*(a[j]-'0'));
    for(i=lena+lenb-1;i>=0;i--)
        if(s[i]>=10)
        {
            s[i-1]+=s[i]/10;
            s[i]%=10;
        }
    for(i=lenb-1;i>=0;i--)
        for(j=lena;j>=0;j--)
            if(t[i][j]>=10)
            {
                t[i][j-1]+=t[i][j]/10;
                t[i][j]%=10;
            }
    i=0;
    while(i<lena+lenb&&s[i]==0) i++;
    if(i==lena+lenb) i--;
    len=lena+lenb-i;
    p=&s[i];
    max_len=len>lenb+1?len:lenb+1;
    if(max_len<lena) max_len=lena;
    for(i=0;i<max_len-lena;i++) printf(" ");printf("%s\n",a);
    for(j=0;j<max_len-lenb-1;j++) printf(" ");printf("*%s\n",b);
    n=0;
    for(i=lenb-1;i>=0;i--)
    {
        j=0;
        while(t[i][j]==0&&j<lena+1) j++;
        if(j==lena+1) j--;
        sub_len=lena+1-j;
        if(i==lenb-1)
        {
            k=sub_len>lenb+1?sub_len:lenb+1;
            for(m=0;m<max_len-k;m++) printf(" ");
            for(m=0;m<k;m++) printf("-");
            printf("\n");
        }
        for(m=0;m<max_len-sub_len-n;m++) printf(" ");
        while(j<lena+1) printf("%ld",t[i][j++]);
        printf("\n");
        n++;
    }
    if(lenb>1)
    {
        for(i=0;i<max_len-len;i++) printf(" ");
        for(i=0;i<len;i++) printf("-");
        printf("\n");
        for(i=0;i<max_len-len;i++) printf(" ");
        for(i=0;i<len;i++) printf("%ld",p[i]);
        printf("\n");
    }
    for(i=0;i<lenb;i++) free(t[i]);
    free(s);
}
int main()
{
    int i,j,T;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        gets(t);
        i=0;
        while(t[i]>='0'&&t[i]<='9') a[i]=t[i++];
        a[i]='\0';
        sign=t[i++];
        j=0;
        while(t[i]!='\0') b[j++]=t[i++];
        b[j]='\0';
        if(sign=='+') add();
        else if(sign=='-') subtract();
        else multiply();
        printf("\n");
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator