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 gure 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 gure.
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 dene
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 dene two sub-mobiles: rst 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

就是给你一个天平,让你看左右平不平衡,没什么算法,主要体会递归的思路!

代码如下:

 #include <cstdio>
#include <cstring> using namespace std;
bool f;
int tree ()
{
int wl,dl,wr,dr;
scanf("%d%d%d%d",&wl,&dl,&wr,&dr);
if (wl==)
wl=tree();
if (wr==)
wr=tree();
if (wl*dl!=wr*dr)
f=false;
return wl+wr;
}
int main()
{
int t;
//freopen("de.txt","r",stdin);
scanf("%d",&t);
while (t--)
{
f=true;
tree();
if (f)
printf("YES\n");
else
printf("NO\n");
if (t)
printf("\n");
}
return ;
}

UVa 839 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.839 Not so Mobile ( 二叉树 DFS)

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

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

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

  4. uva 839 not so mobile——yhx

    Not so Mobile  Before being an ubiquous communications gadget, a mobile was just a structure made of ...

  5. Uva 839 Not so Mobile

    0.最后输出的yes no的大小写 1.注意 递归边界   一直到没有左右子树 即b1=b2=false的时候 才返回 是否 天平平衡. 2.注意重量是利用引用来传递的 #include <io ...

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

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

  7. 《编程简介(Java) &#183;10.3递归思想》

    <编程简介(Java) ·10.3递归思想> 10.3.1 递归的概念 以两种方式的人:男人和女人:算法是两种:递归迭代/通知: 递归方法用自己的较简单的情形定义自己. 在数学和计算机科学 ...

  8. Python算法——递归思想

    编程语言在构建程序时的基本操作有:内置数据类型操作.选择.循环.函数调用等,递归实际属于函数调用的一种特殊情况(函数调用自身),其数学基础是数学归纳法.递归在计算机程序设计中非常重要,是许多高级算法实 ...

  9. [剑指Offer]46-把数字翻译成字符串(递归思想,循环实现)

    题意 '0'到'25'翻译成'a'到'z',故一个字符串可以有多种翻译方式,如12258有五种翻译方式. 给定字符串,输出有多少种翻译方式 解题思路 递归思想 计f(i)为以第i个字符开始到原字符串结 ...

随机推荐

  1. Java Web学习总结(8)JSP(二)

    一,JSP中的九个内置对象 名称 类型 描述 out javax.servlet.jsp.JspWriter 用于页面输出 request javax.servlet.http.HttpServlet ...

  2. SGU 194 Reactor Cooling (无源上下界网络流)

    The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...

  3. 关于12306Bypass-分流抢票

    12306Bypass-分流抢票-2013-15年          官网:http://www.12306bypass.com 分流抢票是一款完全免费的抢票软件,请抵制淘宝贩卖等诈骗行为 作者不会授 ...

  4. Appium运行报错:No Chromedriver found that can automate Chrome '39.0.0'

    运行appium切换webview时候遇到报错:’No Chromedriver found that can automate Chrome 'xx.xx.xx' 此报错是因为Appium在运行过程 ...

  5. BZOJ 4484: [Jsoi2015]最小表示(拓扑排序+bitset)

    传送门 解题思路 \(bitset\)维护连通性,给每个点开个\(bitset\),第\(i\)位为\(1\)则表示与第\(i\)位联通.算答案时显然要枚举每条边,而枚举边的顺序需要贪心,一个点先到达 ...

  6. Java操作Redis小案例

    1.下载jar包. http://download.csdn.net/detail/u011637069/9594840包含本案例全部代码和完整jar包. 2.连接到redis服务. package ...

  7. (转)df命令

    转:http://man.linuxde.net/df df命令用于显示磁盘分区上的可使用的磁盘空间.默认显示单位为KB.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 语法 d ...

  8. (转)Navicat连接MySQL8.0亲测有效

    转:https://www.cnblogs.com/shiysin/p/shiysin.html 今天下了个 MySQL8.0,发现Navicat连接不上,总是报错1251: 原因是MySQL8.0版 ...

  9. asp label居中

    https://stackoverflow.com/questions/25245839/align-a-label-to-the-right-using-asp-net Try something ...

  10. 前端工具-让浏览器兼容ES6特性

    babel:将ES6翻译为ES5 问题: 可以处理import和export么? 不能,还是用Rollup或者webpack打包一下吧 可以处理Promise么? 不能,还是使用babel-plugi ...