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. 用PHP实现Windows域验证

    系统集成中,可能会有这种需求 Windows 域验证本质上是LDAP验证 但在网上居然找不到详细的技术文档,可见不受待见之极.

  2. Html 网页布局(一)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  3. python pip 升级每个包

    pip本身不自带升级所有包的功能, 但可以通过下面的脚本实现. import pip from subprocess import call for dist in pip.get_installed ...

  4. Html==>>一些经典

    1.CSS overflow 属性 2.<input>标签 <input> 标签用于搜集用户信息. 1 type属性 根据不同的 type 属性值,输入字段拥有很多种形式.可以 ...

  5. phpcms--使用添加php原生支持

    1,phpcms模板中有时候要添加一些php相关变量这个时候要使用原始php的东西,可以如下加入 {php $no_wq_id=$r[id] ;}其中$r[id]是通过{pc:get sql=&quo ...

  6. [ASP.NET MVC] 使用Bootstrap套件

    [ASP.NET MVC] 使用Bootstrap套件 前言 在开发Web项目的时候,除了一些天赋异禀的开发人员之外,大多数的开发人员应该都跟我一样,对于如何建构出「美观」的用户接口而感到困扰.这时除 ...

  7. angularJS中的$apply(),$digest(),$watch()

    $apply()和$digest()在AngularJS中是两个核心概念,但是有时候它们又让人困惑.而为了了解AngularJS的工作方式,首先需要了解$apply()和$digest()是如何工作的 ...

  8. IOS6学习笔记(三)

    1.ARC空声明变量 使用ARC的另一个优势是所有未初始化的变量默认都是“空值化”的.这意味着像下面这样的声明使用ARC编译后指向的是空值(nil): NSObject *myObject1,*myO ...

  9. android 事件

    package com.example.yanlei.my2; import android.app.Activity; import android.content.Context; import ...

  10. Oracle EBS在编码方式为AL32UTF8时的注意事项

    现如今的EBS系统中,为了推进国际化的进程,以及系统向全球化的扩展,在Oracle数据库的编码方式上渐渐从支持中国本土简体中文的ZHS16GBK转向了更趋于国际化的AL32UTF8编码方式.但随之而来 ...