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 isWl×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

1

0 2 0 4
0 3 0 1
1 1 1 1
2 4 4 2
1 6 3 2

Sample Output

YES
题解:题意是让求这个杠杆是不是平衡;看了大神的代码,处理的很巧妙,如果当前质量是0,递归输入子天平,每个子天平判断是否平衡,就可以了;
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
#define SI(x) scanf("%d",&x)
#define mem(x,y) memset(x,y,sizeof(x))
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
typedef long long LL;
bool solve(int &w){
int w1,d1,w2,d2;
scanf("%d%d%d%d",&w1,&d1,&w2,&d2);
int temp1=true,temp2=true;
if(!w1)temp1=solve(w1);
if(!w2)temp2=solve(w2);
w=w1+w2;
if(w1*d1==w2*d2&&temp1&&temp2)return true;
else return false;
}
int main(){
int T;
SI(T);
while(T--){
int w;
if(solve(w))puts("YES");
else puts("NO");
if(T)puts("");
}
return 0;
}

  

uva-699 Not so Mobile (杠杆,巧妙递归)的更多相关文章

  1. UVa 839 -- Not so Mobile(树的递归输入)

    UVa 839 Not so Mobile(树的递归输入) 判断一个树状天平是否平衡,每个测试样例每行4个数 wl,dl,wr,dr,当wl*dl=wr*dr时,视为这个天平平衡,当wl或wr等于0是 ...

  2. UVa 699 The Falling Leaves(递归建树)

    UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每 ...

  3. UVA.839 Not so Mobile ( 二叉树 DFS)

    UVA.839 Not so Mobile ( 二叉树 DFS) 题意分析 给出一份天平,判断天平是否平衡. 一开始使用的是保存每个节点,节点存储着两边的质量和距离,但是一直是Runtime erro ...

  4. UVA.699 The Falling Leaves (二叉树 思维题)

    UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...

  5. UVA 839 Not so Mobile (递归建立二叉树)

    题目连接:http://acm.hust.edu.cn/vjudge/problem/19486 给你一个杠杆两端的物体的质量和力臂,如果质量为零,则下面是一个杠杆,判断是否所有杠杆平衡. 分析:递归 ...

  6. UVa 839 Not so Mobile (递归思想处理树)

    Before being an ubiquous communications gadget, a mobilewas just a structure made of strings and wir ...

  7. 二叉树的递归遍历 The Falling Leaves UVa 699

    题意:对于每一棵树,每一个结点都有它的水平位置,左子结点在根节点的水平位置-1,右子节点在根节点的位置+1,从左至右输出每个水平位置的节点之和 解题思路:由于上题所示的遍历方式如同二叉树的前序遍历,与 ...

  8. 【紫书】 The Falling Leaves UVA - 699 递归得简单

    题意:给你一颗二叉树的前序遍历,空子树以-1表示,将左右子树的权值投影到一维数轴上,左儿子位置为根位置-1,右儿子+1求个个整点上的和: 题解:递归,整个过程只需维护一个sum数组. 更新根,更新le ...

  9. UVA 699 The Falling Leaves (递归先序建立二叉树)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/19244 #include <iostream> #include <cstdio> # ...

随机推荐

  1. Javascript 字符串浏览器兼容问题

    先看下不兼容的写法,若我想获取某个字符串的第几位 var str='aavvvcc'; console.info(str[0]); 这种写法 在IE 7以下的浏览器都不兼容,以下提供浏览器全兼容的方式 ...

  2. 迁移到gitbook

    现在要迁移到gitbook啦, 一些note类分享就只在gitbook发了, 其他一些比较长的分享会第一时间发到gitbook,但也会在这边同步 我的gitbook

  3. SQL Server 日志截断

    截断事务日志是逻辑操作,只是把日志的一部分标记为‘不再需要’所以可以重用这个空间,截断不是物理操作,不会减少磁盘上文件的大小, 要减小物理大小必定要进行收缩. ----------- 有时就算是备份都 ...

  4. 转: ES6异步编程: co函数库的含义与用法

    转: ES6异步编程: co函数库的含义与用法 co 函数库是著名程序员 TJ Holowaychuk 于2013年6月发布的一个小工具,用于 Generator 函数的自动执行. 比如,有一个 Ge ...

  5. Eclipse Rcp

    http://blog.csdn.net/soszou/article/details/7996748

  6. ajax与算法,sql的group处理

    function correctAction(){ $semester_id = $this->_getParam("semester_id"); $day = $this- ...

  7. iOS6和iOS7代码的适配(2)——status bar

    用Xcode5运行一下应用,第一个看到的就是status bar的变化.在iOS6中,status bar是系统在处理,应用中不需要考虑这部分,iOS7之后是应用在处理,每个ViewControlle ...

  8. Coursera机器学习课程(2016 )错题集

    Unit 4 Neural Networks (×) 分析:估计D项错误,因为神经网络在处理逻辑运算的时候是range(0,1),但是处理别的运算的时候就不是这个范围了 (√) (对) week 6 ...

  9. POJ 3692 Kindergarten (二分图 最大团)

    Kindergarten Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5660   Accepted: 2756 Desc ...

  10. 【Linux学习】Ubuntu下嵌入式交叉编译环境arm-linux-gcc搭建

    (1)首先选择一个路径用来存放arm-linux-gcc.我选用的是/home/book,并在以下建立一个目录arm-linux-gcc. (2)利用cp EABI-4.3.3_Emdedsky_20 ...