uva839--Balance __ACM

Source: Internet
Author: User

Description:

Before being a ubiquous communications gadget, a mobile was just a structure made of strings and wires suspending Ull things. This kind of the mobile is usually found hanging over cradles of small.

The figure illustrates a simple mobile. It is just a wire, suspended by a string, with an object on each side. It can also be seen as a kind's lever with the fulcrum on the point where the string ties the wire. From the lever principle we know this to balance a simple mobile the product of the weight of the objects by their Distanc E to the fulcrum must is equal. This is wlxdl = wRxdr where Dwas the left distance, DR are the right Distance, WL is the "left weight and wR" right weight.


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

Analysis: in short, enter a balance system, input from top to bottom, if the position has a sub balance, it is entered in order from left to right to determine whether the system is balanced (that is, left weight * left arm = right weight * Right arm) The problem is entered in a recursive definition, so it is also convenient to write the problems recursively. , where the code can be written concisely by using a value reference. P.S This problem submitted when encountered a little problem, to now do not understand, doubt has been written in the comments, ask the expert enlighten. Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < cmath> #include <string> #include <vector> #include <queue> #include <stack> #include <set > #include <map>//#define file #define PI 3.1415926 #define MAX (a,b) ((A) > (B)? (

A):(B)) #define LL long #define Fordo (A,B,C) for (int (A) = (B);(a) <= (C);(a) + +) using namespace Std;
    BOOL Solve (int& W) {int w1,d1,w2,d2;
    BOOL B1=true;
    BOOL B2=true;
    cin>>w1>>d1>>w2>>d2; if (! W1)//Here is very strange, when I use w1==0 VJ will report WA, only.
    W1 This notation can be passed, it seems that the two are no different ... b1=solve (W1);//When there are sub balances, solve. if (!
    W2) B2=solve (W2);
    w=w1+w2;//the weight of the balance is calculated by weight return b1&&b2&& (W1*D1==W2*D2);
Recursive solution: When the sub balance of the pallet is balanced and the balance is balanced, the balance of the balance is returned.
    int main () {#ifdef file Freopen ("Test.in", "R", stdin);
    Freopen ("Test.out", "w", stdout);
    #endif//file int t,w;
    cin>>t; while (t--) {if Solve (W) cout<< "YES" <<endl;
        else cout<< "NO" <<endl;
    if (T) 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.