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. Juint test Case 的2种使用方式

    通常情况下,我们去测试一个类中的方法,首先是建立一个包,包中建立一个测试类,在建立测试类文件时,选择JUnit Test Case,如下: 建好之后写测试用例: 但是如果偏就想在编写方法的那个java ...

  2. kafka-consumer.properties

    # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreement ...

  3. tp5怎么验证手机号码

    直接上干货

  4. 术语-MOSS-微软协作工具:MOSS(微软协作工具)

    ylbtech-术语-MOSS-微软协作工具:MOSS(微软协作工具) MOSS -- Microsoft Office Sharepoint Server,是一款为企业客户而设计的.基于web的内容 ...

  5. Windows-计划任务-自动备份数据库和文件

    开始 -> 程序 -> 附件 -> 系统工具 -> 计划任务 .bat 文件如下: ::数据库+文件备份 @echo off ::日期时间 set yyyymmdd=%date ...

  6. JS-for..of

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of 刚刚上网上看到<V8 ...

  7. 115、TensorFlow变量的使用

    # To use the value of a tf.Variable in a Tesnorflow graph , simply treat it like a normal tf.Tensor ...

  8. 【玩转SpringBoot】异步任务执行与其线程池配置

    同步代码写起来简单,但就是怕遇到耗时操作,会影响效率和吞吐量. 此时异步代码才是王者,但涉及多线程和线程池,以及异步结果的获取,写起来颇为麻烦. 不过在遇到SpringBoot异步任务时,这个问题就不 ...

  9. zabbix配置-模板

    1.配置=>模板=>创建模板=>输入模板名称和群组 2.配置=>模板=>找到刚刚创建的模板=>点击应用集(applications)=>创建应用集=>输 ...

  10. 14. Jmeter-配置元件一

    jmeter-配置元件介绍与使用 CSV 数据文件设置 HTTP信息头管理器 HTTP Cookie 管理器 HTTP Cache Manager HTTP请求默认值 计数器 DNS Cache Ma ...