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

------------------------我是分割线----------------------------------------------------------------------------------------

这道题从题目就可以看出是递归关系定义的,所以使用递归进行输入;

并且可以在输入过程中进行判断;

使用引用传值而不用全局变量,极大简化代码,增加可读性。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<cstdlib>
  8. using namespace std;
  9. int read(){
  10. int x=,f=;char ch=getchar();
  11. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  12. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  13. return x*f;
  14. }
  15. bool build(int &w)
  16. {
  17. int w1,w2,d1,d2;
  18. bool b1=true,b2=true;
  19. w1=read();d1=read();w2=read();d2=read();
  20. if(!w1) b1=build(w1);
  21. if(!w2) b2=build(w2);
  22. w=w1+w2;
  23. return b1&&b2&&(w1*d1==w2*d2);
  24. }
  25. int main()
  26. {
  27. int t,w;
  28. t=read();
  29. while(t--)
  30. {
  31. if(build(w)) cout<<"YES"<<endl;
  32. else cout<<"NO"<<endl;
  33. if(t) cout<<"\n";
  34. }
  35. return ;
  36. }

uva 839 Not so Mobile-S.B.S.的更多相关文章

  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——yhx

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

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

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

  5. Uva 839 Not so Mobile

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

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

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

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

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

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

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

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

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

  10. Not so Mobile UVA - 839

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

随机推荐

  1. 我的HTML笔记

    HTML(Hypertext Marked Language)"超文本标记语言". 1.HTML的声明 <!DOCTYPE html> 2.HTML的基本结构 < ...

  2. [moka同学笔记]YII2中发送邮件示例(转载)

    原文:http://yiilib.com/topic/675/Yii2%E4%B8%AD%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E7%A4%BA%E4%BE%8B { ...

  3. phpcms—— 内容中的附件调用和添加远程地址的调用

    phpcms中几个地址调用的方法 1,CSS路径有{CSS_PATH}2,图片路径有{IMG_PATH}3,JS路径有{JS_PATH} 4,那么附件的路径如何调用,使用下面的方式可以得到附件的路径前 ...

  4. [小北De编程手记] : Lesson 06 - Selenium For C# 之 流程控制

    无论你是用哪一种自动化测试的驱动框架,当我们构建一个复杂应用程序的自动化测试的时候.都希望构建一个测试流程稳定,维护成本较低的自动化测试.但是,现实往往没有理想丰满.而这一篇,我会为大家讲解我们在使用 ...

  5. 【OpenCV】OpenCV中GPU模块使用

    CUDA基本使用方法 在介绍OpenCV中GPU模块使用之前,先回顾下CUDA的一般使用方法,其基本步骤如下: 1.主机代码执行:2.传输数据到GPU:3.确定grid,block大小: 4.调用内核 ...

  6. 自己写的表格插件autotable

    自己写的表格插件autotable 作者:田想兵,个人网址:http://www.lovewebgames.com 这个表格插件所完成的功能是:ajax请求数据,然后动态绑定到指定表格下,格式化,分页 ...

  7. js的基本数据类型有哪些?

    js的基本数据类型有哪些? ECMAScript中有5中简单数据类型(也称为基本数据类型): Undefined.Null.Boolean.Number和String.还有1中复杂的数据类型----O ...

  8. 通过源码理解UST(用户栈回溯)

    UST原理:如果gflags标志中包含了UST标志,堆管理器会为当前进程分配一块内存,这个内存区域就是UST数据库(user-mode stack trace database),并建立一个STACK ...

  9. 用js实现在加载完成一个页面后自动执行一个方法

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  10. Force.com微信开发系列(七)OAuth2.0网页授权

    OAuth是一个开放协议,允许用户让第三方应用以安全且标准的方式获取该用户在某一网站上存储的私密资源(如用户个人信息.照片.视频.联系人列表),而无须将用户名和密码提供给第三方应用.本文将详细介绍OA ...