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.

 
 
 
(picture copy failed,cou huo zhe kan ba.)

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.

 #include<cstdio>
bool slv(int &x) //读入和处理同时进行
{ //变量不是从上往下传,而是从下往上传。
int i,j,k,wl,dl,wr,dr;
bool b1=,b2=;
scanf("%d%d%d%d",&wl,&dl,&wr,&dr);
if (!wl) b1=slv(wl); //判定子问题的同时求出w1
if (!wr) b2=slv(wr);
x=wl+wr; //对于本层递归没有意义,但为上一层传值。
if (b1&&b2&&wl*dl==wr*dr) return ;
else return ;
}
int main()
{
int i,n,x;
scanf("%d",&n);
for (i=;i<=n;i++)
{
if (slv(x)) printf("YES\n");
else printf("NO\n");
if (i!=n) printf("\n");
}
}

极其精简的代码。算法没什么,具体实现见注释。

uva 839 not so mobile——yhx的更多相关文章

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

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

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

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

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

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

  4. Uva 839 Not so Mobile

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

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

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

  6. UVa 839 (递归方式读取二叉树) Not so Mobile

    题意: 递归的方式输入一个树状天平(一个天平下面挂的不一定是砝码还可能是一个子天平),判断这个天平是否能满足平衡条件,即W1 * D1 == W2 * D2. 递归的方式处理输入数据感觉很巧妙,我虽然 ...

  7. 【紫书】【重要】Not so Mobile UVA - 839 递归得漂亮

    题意:判断某个天平是否平衡,输入以递归方式给出. 题解:递归着输入,顺便将当前质量作为 &参数 维护一下,顺便再把是否平衡作为返回值传回去. 坑:最后一行不能多回车 附:天秀代码 #defin ...

  8. 天平 (Not so Mobile UVA - 839)

    题目描述: 题目思路: 1.DFS建树 2.只有每个树的左右子树都平衡整颗树才平衡 #include <iostream> using namespace std; bool solve( ...

  9. Not so Mobile UVA - 839

    题目链接:https://vjudge.net/problem/UVA-839 题目大意:输入一个树状天平,根据力矩相等原则,判断是否平衡.  如上图所示,所谓力矩相等,就是Wl*Dl=Wr*Dr.  ...

随机推荐

  1. ECMall如何支持SSL连接邮件服务器的配置

    首先,主要是ecmall使用的phpmailer版本太低,不支持加密连接. 然后,得对相应代码做一定调整. 1. 覆盖phpmailer 请从附件进行下载: http://files.cnblogs. ...

  2. (旧)子数涵数·PS——文字人物

    首先我们来看一下我用到的素材(在百度图库里下载的). 一.打开PS,在PS中打开素材. 二.复制一个图层(好习惯不解释). 三.图像->调整->阈值,或者按下图示按钮后选择阈值,弹出阈值窗 ...

  3. mongodb学习4---索引

    1,mongodb的性能分析 db.active.find({id:'sdfasdf6jh67j353g346hkfgh6'}).explain('executionStats') "mil ...

  4. XPM转换与查看工具

    X PixMap (XPM)是一种基于ASCII编码的图像格式,在X Window系统中的应用十分广泛.她最初由位于法国Sophia Antipolis的Bull研究中心的Daniel Dardail ...

  5. django 中间件

    django处理一个Request的过程是首先通过django 中间件,然后再通过默认的URL方式进行的.所以说我们要做的就是在django 中间件这个地方把所有Request拦截住,用我们自己的方式 ...

  6. [Angularjs]视图和路由(四)

    写在前面 关于angularjs的路由的概念基本上这篇就要结束了,通过学习,以及在实际项目中的实践,还是比较容易上手的.自己也通过angularjs做了一个在app上的一个模块,效果还是可以的. 系列 ...

  7. How to Use Telnet to Test SMTP Communication

    Topic Last Modified: 2005-05-24 Telnet is an extremely useful tool for troubleshooting issues relate ...

  8. 用doxygen+graphviz自动化生成代码文档(附详细教程)

    一.引子 用这两个工具可以自动的遍历代码,并且产生代码文档,我们先来看看效果,然后放出这两个工具的下载地址. 二.工具的下载地址 doxygen:http://www.stack.nl/~dimitr ...

  9. IP 协议首部格式与其配套使用的四个协议(ARP,RARP,ICMP,IGMP)

    目录 IP协议首部格式地址解析协议 ARP逆向地址解析协议 RARP网际控制报文协议 ICMP网际组管理协议IGMP IP 数据报首部 IP数据报首部格式: 最高位在左边,记为0 bit:最低位在右边 ...

  10. IOS真机测试(用证书进行真机测试)

    真机测试需要准备 1.证书 2.Iphone或者Ipad 3.到developer.apple.com注册开发者账号(不用money的) ------------------------------- ...