poj2142 The Balance
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int x,y;
int exgcd(int a,int b)
{
int t,gcd;
if(b==0)
{
x=1;
y=0;
return a;
}
gcd=exgcd(b,a%b);
t=x;
x=y;
y=t-a/b*y;
return gcd;
}
int main()
{
int a,b,c;
bool flag;
int ii;
while(scanf("%d %d %d",&a,&b,&c),a||b||c)
{
flag=true;
if(a<b)
{
flag=false;
ii=a;
a=b;
b=ii;
}
int d=exgcd(a,b);
x=x*c/d;
y=y*c/d;
int m1=b/d,m2=a/d;
int t1=y/m2,t2;
if(t1*m2-y>=0)
{
t1--;
}
t2=t1+1;
int temp1,temp2,temp3,temp4;
temp1=(x+m1*t1)>0?((x+m1*t1)):((x+m1*t1)*(-1));
temp2=(y-m2*t1)>0?((y-m2*t1)):((y-m2*t1)*(-1));
int xy1=temp1+temp2;
int ab1=temp1*a+temp2*b;
temp3=(x+m1*t2)>0?((x+m1*t2)):((x+m1*t2)*(-1));
temp4=(y-m2*t2)>0?((y-m2*t2)):((y-m2*t2)*(-1));
int xy2=temp3+temp4;
int ab2=temp3*a+temp4*b;
if(flag)
{
if(xy1>xy2)
{
printf("%d %d\n",temp3,temp4);
continue;
}
if(xy1<xy2)
{
printf("%d %d\n",temp1,temp2);
continue;
}
if(xy1==xy2)
{
if(ab1<ab2)
{
printf("%d %d\n",temp1,temp2);
continue;
}
if(ab1>ab2)
{
printf("%d %d\n",temp3,temp4);
continue;
}
}
}
else
{
if(xy1>xy2)
{
printf("%d %d\n",temp4,temp3);
continue;
}
if(xy1<xy2)
{
printf("%d %d\n",temp2,temp1);
continue;
}
if(xy1==xy2)
{
if(ab1<ab2)
{
printf("%d %d\n",temp2,temp1);
continue;
}
if(ab1>ab2)
{
printf("%d %d\n",temp4,temp3);
continue;
}
}
}
}
return 0;
}