uva 465 overflow

题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=406

 

 

同样的代码,交了两遍一边AC一边WA。。。很蛋疼= =、、、

 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int main()
{
char a[3000],b[3000],o;
double max = 2147483647;
double x,y,res ;
while(scanf("%s %c %s",a,&o,b) != EOF)
{
printf("%s %c %s\n",a,o,b);
x = atof(a);
y = atof(b);
if(x > max)
puts("first number too big");
if(y > max)
puts("second number too big");
if(o == '+')
res = x+y;
else
res = x*y;
if(res > max)
puts("result too big");


}

return 0;
}





这个代码不知道哪里错了~~
#include<stdio.h>
#include<string.h>
char a[3001],b[3001],o[10],ans[3001];
char max[] = "2147483647";
del0(char a[])
{
    int i,j,len;
    len = strlen(a);
    for(i = 0;i < len;i++)
    {
        if(a[i] != '0')
            break;
    }
    for(j = 0;j < len-i;j++)
    {
        a[j] = a[j+i];
    }
    a[j] = '\0';
    if(strlen(a) == 0)
        strcpy(a,"0");
}
int jiafa(char a[],char b[])
{
    memset(ans,0,sizeof(ans));
    int i,j,k,m = 0,s,len1,len2,leap;
    leap = 0;
    len1 = strlen(a)-1;
    len2 = strlen(b)-1;

    while(len1 >= 0 && len2 >= 0)
    {
        k = (a[len1--]-'0') + b[len2--] -'0' + leap;
        ans[m++] = k%10 + '0';
        leap = k/10;
    }
    while(len1 >= 0)
    {
        k = a[len1--] - '0' + leap;
        ans[m++] = k%10 + '0';
        leap = k/10;
    }
    while(len2 >= 0)
    {
        k = b[len2--] - '0' + leap;
        ans[m++] = k%10 + '0';
        leap = k/10;
    }
    if(leap)
        ans[m++] = '0' + 1;
    return m-1;
}
int chengfa(char a[],char b[])
{
    int len1,len2,i,j;
    len1 = strlen(a)-1;
    len2 = strlen(b)-1;
    int max = 0;
    int count;
    for(i = 0;i < 3001;i++)
        ans[i] = '0';
    int m,wei;
    for(i = len2;i >= 0;i--)
    {
        
        count = len2-i;
        m = wei = 0;
        for(j = len1;j >= 0;j--)
        {
            m = (a[j]-'0')*(b[i]-'0') + ans[count]-'0' + wei;
            ans[count++] = m%10+ '0';
            wei = m/10;
        }
        if(wei)
            ans[count++] = wei + '0';
        if(max < count)
            max = count;

    }
    return max;
}

int judge(char a[])
{
    int len;
    len = strlen(a);
    if(len > 10 )
        return 1;
    if(len < 10)
        return 0;
    if(len == 10)
    {
        if(strcmp(a,max) > 0)
            return 1;
        else
            return 0;
    }
}

int main()
{
    while(scanf("%s%s%s",a,o,b) != EOF)
    {
        del0(a);
        del0(b);

        int m;
        int i;
        if(strcmp(o,"+") == 0)
        {
            m = jiafa(a,b);

            for(i = 0;i < (m+1)/2;i++)
            {
                char temp;
                temp = ans[i];
                ans[i] = ans[m-i];
                ans[m-i] = temp;
            }        
            del0(ans);
            printf("%s + %s\n",a,b);
        }
        
        if(strcmp(o,"*") == 0)
        {
            m = chengfa(a,b);

            for(i = 0;i < m/2;i++)
            {
                char temp;
                temp = ans[i];
                ans[i] = ans[m-i-1];
                ans[m-i-1] = temp;
            }        
            del0(ans);
            printf("%s * %s\n",a,b);
        }


        if(judge(a))
            puts("first number too big");
        if(judge(b))
            puts("second number too big");
        if(judge(ans))
            puts("result too big");
    }
    

    return 0;
}
posted @ 2012-07-08 00:03  某某。  阅读(263)  评论(0编辑  收藏  举报