测试数据的答案与我对代码的答案不同

时间:2015-09-27 01:58:14

标签: c fractions mixed

问题在于http://judgegirl.csie.org/problem/0/202

当我扫描测试数据时:

-2
1
3
3
-1
3
7

答案是:

1
19
30

但我对我的代码的回答是:

1
-19
30

此外,我添加了绝对值的条件,但是徒劳无功。

#include <stdio.h>
#include <stdlib.h> 
int gcd(int,int);
main ()
{
int a,b,c,d,e,f,g,h, i,j,x,y,z,GCD,abs;
        scanf ("%d\n", &a); 
        scanf ("%d\n", &b); 
        scanf ("%d\n", &c); 
        scanf ("%d\n", &d); 
        scanf ("%d\n", &e); 
        scanf ("%d\n", &f); 
        scanf ("%d\n", &g); 
        if ((a != 0 )&&(e!=0)&&(a>=-100)&&(a<=100)&&(e>=-100)&&(e<=100)&&(b>=0)&&(f>=0)&&(b<=100)&&(f<=100)&&(c>0)&&(g>0)&&(c<=100)&&(g<=100)){
            switch ( d ){
                case 0 :
                    if (a>0 && e>0){
                        x = a + e +(b*g + f*c)/(c*g); 
                        y = (b*g + f*c)%(c*g);
                        z = c*g ;
                        break ;
                    }
                    if (a>0 && e<0){
                        x = a + e +(b*g - f*c)/(c*g);
                        y = (b*g - f*c)%(c*g);
                        z = c*g ;
                        break ;
                    }
                    if (a<0 && e>0){
                        x = a + e +((-b)*g + f*c )/(c*g);
                        y = ((-b)*g + f*c)%(c*g);
                        z = c*g ;
                        break ;
                    }
                    if (a<0 && e<0){
                        x = a + e +((-b)*g - f*c)/(c*g);
                        y = ((-b)*g - f*c)%(c*g);
                        z = c*g ;
                        break ;
                    }
                case 1 :
                    if (a>0 && e>0){
                        x = a - e + (b*g - f*c)/(c*g);
                        y = (b*g - f*c)% (c*g);
                        z = c*g;
                        break ;
                    }
                    if (a>0 && e<0){
                        x = a - e+(b*g + f*c)/(c*g) ;
                        y = (b*g + f*c )%(c*g);
                        z = c*g ;
                        break ;
                    }
                    if (a<0 && e>0){
                        x = a - e+((-b*g )- f*c)/(c*g) ;
                        y = ((-b)*g - f*c)%(c*g) ;
                        z = c*g ;
                        break ;
                    }
                    if (a<0 && e<0){
                        x = a - e +((-b)*g + f*c)/(c*g) ;
                        y = ((-b)*g + f*c )%(c*g);
                        z = c*g ;
                        break ;
                    }
                case 2 :
                    if (a>0 && e>0){
                        x = (c*a + b)*(e*g + f)/(c*g);
                        y = (c*a + b)*(e*g + f)%(c*g);
                        z = c*g;
                        break ;
                    }
                    if (a>0 && e<0){
                        x = (c*a + b)*(e*g - f)/(c*g);
                        y = (c*a + b)*(e*g - f)%(c*g);
                        z = c*g;
                        break ;
                    }
                    if (a<0 && e>0){
                        x = (c*a - b)*(e*g + f)/(c*g);
                        y = (c*a - b)*(e*g + f)%(c*g);
                        z = c*g;
                        break ;
                    }
                    if (a<0 && e<0){
                        x = (c*a - b)*(e*g - f)/(c*g);
                        y = (c*a - b)*(e*g - f)%(c*g);
                        z = c*g;
                        break ;
                    }
                case 3 :
                    if (a>0 && e>0){
                        x = ((c*a+b)*g)/(c*(e*g+f));
                        y = ((c*a+b)*g)%(c*(e*g+f));
                        z = c*(e*g+f);
                        break ;
                    }
                    if (a>0 && e<0){
                        x = ((c*a+b)*g)/(c*(e*g-f));
                        y = ((c*a+b)*g)%(c*(e*g-f));
                        z = c*(e*g-f);
                        break ;
                    }
                    if (a<0 && e>0){
                        x = ((c*a-b)*g)/(c*(e*g+f));
                        y = ((c*a-b)*g)%(c*(e*g+f));
                        z = c*(e*g+f);
                        break ;
                    }
                    if (a<0 && e<0){
                        x = ((c*a-b)*g)/(c*(e*g-f));
                        y = ((c*a-b)*g)%(c*(e*g-f));
                        z = c*(e*g-f);
                        break ;
                    }
        } 

        if (y>0){
            abs = y;
        }
        else {
            abs = -y;
        }
        GCD=gcd(abs,z);
        abs = abs/GCD;
        z = z/GCD;
        printf ("%d\n",x);
        printf ("%d\n",abs);
        printf ("%d\n",z);
        }
    }
    int gcd(int abs , int z)
    {
        if (!z) return (abs); 
        return gcd(z, abs % z);
    }

0 个答案:

没有答案