本文主要是介绍UVA839——天平,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
描述:
Before being an ubiquous communications gadget, a mobile was 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 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 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 is Wl×Dl = Wr×Dr where Dl is the left distance, Dr is the right distance, Wl is the left weight and Wr 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.
#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 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)//这里很奇怪,当我用W1==0时VJ就会报WA,只有!W1这种表示法可以过,貌似这两者没区别吧……b1=solve(W1);//当存在子天平,求解。if(!W2)b2=solve(W2);W=W1+W2;//由子天平重量计算该天平重量return b1&&b2&&(W1*D1==W2*D2);//递归求解:当该托盘的子天平都平衡,并且该天平平衡,则返回该天平平衡。
}int main()
{#ifdef filefreopen("test.in", "r", stdin);freopen("test.out", "w", stdout);#endif // fileint T,W;cin>>T;while(T--){if(solve(W))cout<<"YES"<<endl;elsecout<<"NO"<<endl;if(T)cout<<endl;}return 0;
}
这篇关于UVA839——天平的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!