Not so Mobile, notsomobile

Source: Internet
Author: User

Not so Mobile, notsomobile

Not so Mobile

Before being an ubiquous communications gadget,MobileWas just a structure made of strings and wires suspending colourfull things. This kind of mobile is usually found hanging over cradles of small babies.

The figure has strates a simple mobile. it is just a wire, suincluded by a string, with an object on each side. it can also be seen as a kind of lever with the fulcrum on the point where the string ties the wire. from the lever principle we know that to balance a simple mobile the product of the weight of the objects by their distance to the fulcrum must be equal. that isWDL =WDR whereDL is the left distance,DR is the right distance,WL is the left weight andWR is the right weight.


In a more complex mobile the object may be replaced by a sub-mobile, as shown in the next figure. in this case it is not so straightforward to check if the mobile is balanced so we need you to write a program that, given a description of a mobile as input, checks whether the mobile is in equilibrium or not.

Input The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. this line is followed by a blank line, and there is also a blank line between two consecutive inputs.


The input is composed of several lines, each containing 4 integers separated by a single space. The 4 integers represent the distances of each object to the fulcrum and their weights, in the format:WLDLWRDR

IfWL orWR is zero then there is a sub-mobile hanging from that end and the following lines define the sub-mobile. in this case we compute the weight of the sub-mobile as the sum of weights of all its objects, disregarding the weight of the wires and strings. if bothWL andWR are zero then the following lines define two sub-mobiles: first the left then the right one.

Output For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.


Write'YES'If the mobile is in equilibrium, write'NO'Otherwise.

Sample Input
10 2 0 40 3 0 11 1 1 12 4 4 21 6 3 2

Sample Output
YES
 
Binary Tree recursion: Give the Left and Right nodes of the root node, w1 = 0, d1 = 2, w2 = 0, d2 = 4; if the left node is zero, the left and right children's values of the current left node are read. The left and right children have values until they know;
 
# Include <iostream> # include <cstdio> using namespace std; int flag; int read () {int w1, d1, w2, d2; cin> w1> d1> w2> d2; if (w1 & w2 & d1 & d2) // if both left and right children exist, then the question is judged; {if (w1 * d1! = W2 * d2) {flag = 1; return 0;} else return (w1 + w2); // return the value of the root node;} else {if (! W1) w1 = read (); // The value of the node is zero to continue reading data; if (! W2) w2 = read (); if (w1 * d1! = W2 * d2) {flag = 1; return 0;} else return (w1 + w2) ;}} int main () {int n; cin >> n; while (n --) {flag = 0; read (); // read data; if (! Flag) cout <"YES" <endl; else cout <"NO" <endl; if (n! = 0) cout <endl;} return 0 ;}


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.