USACO Section 3.2 Feed Ratios-three-layer for loop is OK!

Source: Internet
Author: User

Check the data range ~~ Up to 100... think about it... all the results obtained by the answer must be smaller than 100... so the three-layer for loop to brute force find the optimal solution to meet the requirements is OK .. it's disgusting .. the given (x, y, z) may have 0... so special processing...

Program:

/*
ID: zzyzzy12
LANG: C ++
TASK: ratios
*/
# Include <iostream>
# Include <istream>
# Include <stdio. h>
# Include <string. h>
# Include <math. h>
# Include <stack>
# Include <algorithm>
# Include <queue>
# Define OOS 2000000000
# Define ll long
Using namespace std;
Struct node
{
Int x, y, z;
} S [4];
Int I, j, k, ans [4], m;
Void getanswer ()
{
Ans [0] = oo;
For (I = 0; I <= 100; I ++)
For (j = 0; j <= 100; j ++)
For (k = 0; k <= 100; k ++)
If ((! S [0]. x | (s [1]. x * I + s [2]. x * j + s [3]. x * k) % s [0]. x = 0 )&&(! S [0]. y | (s [1]. y * I + s [2]. y * j + s [3]. y * k) % s [0]. y = 0 )&&(! S [0]. z | (s [1]. z * I + s [2]. z * j + s [3]. z * k) % s [0]. z = 0 ))
{
M = 0;
If (s [0]. x) m = (s [1]. x * I + s [2]. x * j + s [3]. x * k)/s [0]. x;
Else
{
If (s [1]. x * I + s [2]. x * j + s [3]. x * k) continue;
If (s [0]. y) m = (s [1]. y * I + s [2]. y * j + s [3]. y * k)/s [0]. y;
Else
{
If (s [1]. y * I + s [2]. y * j + s [3]. y * k) continue;
If (s [0]. z) m = (s [1]. z * I + s [2]. z * j + s [3]. z * k)/s [0]. z;
}
}
If (! M) continue;
If (s [1]. y * I + s [2]. y * j + s [3]. y * k = m * s [0]. y)
If (s [1]. z * I + s [2]. z * j + s [3]. z * k = m * s [0]. z)
If (ans [0]> m)
{
Ans [1] = I; ans [2] = j; ans [3] = k;
Ans [0] = m;
}
}
}
Int main ()
{
Freopen ("ratios. in", "r", stdin );
Freopen ("ratios. out", "w", stdout );
For (I = 0; I <4; I ++) scanf ("% d", & s [I]. x, & s [I]. y, & s [I]. z );
Getanswer ();
If (ans [0] = oo) printf ("NONE \ n"); else
Printf ("% d \ n", ans [1], ans [2], ans [3], ans [0]);
Return 0;
}
/*
ID: zzyzzy12
LANG: C ++
TASK: ratios
*/
# Include <iostream>
# Include <istream>
# Include <stdio. h>
# Include <string. h>
# Include <math. h>
# Include <stack>
# Include <algorithm>
# Include <queue>
# Define OOS 2000000000
# Define ll long
Using namespace std;
Struct node
{
Int x, y, z;
} S [4];
Int I, j, k, ans [4], m;
Void getanswer ()
{
Ans [0] = oo;
For (I = 0; I <= 100; I ++)
For (j = 0; j <= 100; j ++)
For (k = 0; k <= 100; k ++)
If ((! S [0]. x | (s [1]. x * I + s [2]. x * j + s [3]. x * k) % s [0]. x = 0 )&&(! S [0]. y | (s [1]. y * I + s [2]. y * j + s [3]. y * k) % s [0]. y = 0 )&&(! S [0]. z | (s [1]. z * I + s [2]. z * j + s [3]. z * k) % s [0]. z = 0 ))
{
M = 0;
If (s [0]. x) m = (s [1]. x * I + s [2]. x * j + s [3]. x * k)/s [0]. x;
Else
{
If (s [1]. x * I + s [2]. x * j + s [3]. x * k) continue;
If (s [0]. y) m = (s [1]. y * I + s [2]. y * j + s [3]. y * k)/s [0]. y;
Else
{
If (s [1]. y * I + s [2]. y * j + s [3]. y * k) continue;
If (s [0]. z) m = (s [1]. z * I + s [2]. z * j + s [3]. z * k)/s [0]. z;
}
}
If (! M) continue;
If (s [1]. y * I + s [2]. y * j + s [3]. y * k = m * s [0]. y)
If (s [1]. z * I + s [2]. z * j + s [3]. z * k = m * s [0]. z)
If (ans [0]> m)
{
Ans [1] = I; ans [2] = j; ans [3] = k;
Ans [0] = m;
}
}
}
Int main ()
{
Freopen ("ratios. in", "r", stdin );
Freopen ("ratios. out", "w", stdout );
For (I = 0; I <4; I ++) scanf ("% d", & s [I]. x, & s [I]. y, & s [I]. z );
Getanswer ();
If (ans [0] = oo) printf ("NONE \ n"); else
Printf ("% d \ n", ans [1], ans [2], ans [3], ans [0]);
Return 0;
}

 

From Jacob's zone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.