UVa 839 - Not so Mobile
来源:互联网 发布:冰点还原 知乎 编辑:程序博客网 时间:2024/06/05 11:36
Not so Mobile
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.
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: Wl Dl Wr Dr
If Wl or Wr is zero then there is a sub-mobile hanging from that end and the following lines define the 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 both Wl and Wr 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
#include<cstdio>//输入一个子天平,返回天平是否平衡,参数W修改为子天平的总重量bool solve(int &W){ int W1,D1,W2,D2; bool b1=true,b2=true; scanf("%d %d %d %d",&W1,&D1,&W2,&D2); if(!W1) b1=solve(W1); if(!W2) b2=solve(W2); W=W1+W2; return b1&&b2&&(W1*D1==W2*D2);}int main(){ int T,W; scanf("%d",&T); while(T--){ printf("%s\n",solve(W)?"YES":"NO"); if(T) printf("\n"); } return 0;}
- uva 839 - Not so Mobile
- uva 839 Not so Mobile
- uva 839 - Not so Mobile
- UVa 839 - Not so Mobile
- UVa 839 - Not so Mobile
- uva-839 Not so Mobile
- uva 839 - Not so Mobile
- UVA 839 Not so Mobile
- uva 839 Not so Mobile
- Not so Mobile UVA 839
- uva 839 - Not so Mobile
- uva 839 Not so Mobile
- UVa 839 - Not so Mobile
- UVA - 839 Not so Mobile
- UVA 839 Not so Mobile
- UVA - 839 Not so Mobile
- UVA 839 - Not so Mobile
- UVA - 839 Not so Mobile
- 通过 jawin 调用dll库(no jawin in java.library.path)
- Android程序:使用HttpClient进行Get方式通信
- 网投简历制作要点(1)
- 真正的iframe 自适应高度,动态高度 js
- 我的Java基础知识总结1
- UVa 839 - Not so Mobile
- Python:简单的文件备份脚本
- 关于职业规划
- OpenGL Tutorial: (1) Setting up OpenGL with Visual Studio
- Activity生命周期
- 将博客搬至51CTO
- 最短路
- 第十四周项目2:用文件保存的学生名单
- Android ListView解决控件抢焦点导致的Listview不可点击