1743: Solution Equation description
A group of Ultraman defeated a group of small monsters, known all Ultraman have X1 head, y1 leg (mutation Ultraman), all the small monsters have x2 head, y2 leg. Battlefield on a total Q head, W leg, asked how many Ultraman, how many small monsters?
Input
The input data has several groups each group contains 6 positive integers, respectively, x1,y1,x2,y2,q,w; (0<=q,w<=1000000000); input data is guaranteed to have a unique solution. Read 0 0 0 0 0 0 end.
Output
The output takes up a row, containing two positive integers: the number of Ultraman and the small monster;
Sample Input749 405 263 991 816625 693657 0 0 0 0 0 0Sample Output986 297
1#include <stdio.h>2 3 intMainvoid)4 {5 Long Long intx1,y1,x2,y2,q,w;6 while(SCANF ("%lld%lld%lld%lld%lld%lld", &x1,&y1,&x2,&y2,&q,&w)! =EOF)7 {8 Long Long intI,k;//I is the number of Ultraman, K is the number of monsters9 if(x1==0&&y1==0&&x2==0&&y2==0&&q==0&&w==0)Ten { One Break; A } - Else - { the -k= (Q*Y1-W*X1)/(x2*y1-x1*y2); -I= (Q*Y2-W*X2)/(x1*y2-y1*x2); -printf"%lld%lld\n", i,k); + } - + } A return 0; at}
Let's take a look at this code: (enumeration?) Violence? )
1#include <stdio.h>2 3 intMainvoid)4 {5 intx1,y1,x2,y2,q,w;6 while(SCANF ("%d %d%d%d%d%d", &x1,&y1,&x2,&y2,&q,&w)! =EOF)7 {8 intI,k;//I is the number of Ultraman, K is the number of monsters9 if(x1==0&&y1==0&&x2==0&&y2==0&&q==0&&w==0)Ten { One return 1; A } - Else - { the inttemp1=q/x1,temp2=q/x2; - for(i=0; i<=temp1;i++) - { - for(k=0; k<=temp2;k++) + { - if(i*y1+k*y2==w&&i*x1+k*x2==q) + { Aprintf"%d%d\n", i,k); at Break; - } - } - } - } - } in return 0; -}
The number of Ultraman/monsters from scratch, although can also find data, at least can find the test data, but with the increase in data, the number of for execution will increase, so the use of direct solution, the equivalent of two of the first equation, the number of heads, the number of feet will be determined values, The input data will all be positive integers unless all 0 exits, so
1 k= (q*y1-w*x1)/(x2*y1-x1*y2); 2 i= (q*y2-w*x2)/(X1*Y2-Y1*X2);
The corresponding quantity is calculated directly by using the expression.
1743: Solution equation