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<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
bool build(int &w)
{
int w1,w2,d1,d2;
bool b1=true,b2=true;
w1=read();d1=read();w2=read();d2=read();
if(!w1) b1=build(w1);
if(!w2) b2=build(w2);
w=w1+w2;
return b1&&b2&&(w1*d1==w2*d2);
}
int main()
{
int t,w;
t=read();
while(t--)
{
if(build(w)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
if(t) cout<<"\n";
}
return ;
}

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. .NET中Debug模式与Release模式

    Visual Studio 项目对程序的发布和调试版本分别有单独的配置.顾名思义,生成调试版本的目的是用于调试,而生成发布版本的目的是用于版本的最终分发. 如果在 Visual Studio 中创建程 ...

  2. (旧)子数涵数·PS——换脸

    一.打开Photoshop,打开下载好的两张素材图(均在百度下载的). 二.选中人物脸的部分,不需要很精确(哪种抠图方式都行,我用的是矩形选框工具),再使用移动工具(快捷键为V,或者按着Ctrl),将 ...

  3. Linux FTP配置文件说明

    一.vsftpd说明: LINUX下实现FTP服务的软件很多,最常见的有vsftpd,Wu-ftpd和Proftp等.Red Hat Enterprise Linux中默认安装的是vsftpd. 访问 ...

  4. dubbo序列化的一点注意

    最近工作中遇见了一个小问题,在此记录一下,大致是这样的,有一父类,有一个属性traceId,主要是记录日志号,这样可以把所有日志串起来,利于排查问题,所有的pojo对象继承于此,但是其中一同事在子类p ...

  5. Wechat4j之Hello world——使用wechat4j快速开发java版微信公众号

    Wechat4j是一个开源的java微信开发框架,是目前最简单易用的java微信开发框架. 项目地址:https://github.com/sword-org/wechat4j Wechat4j.ja ...

  6. css 负边距 小记

    水平格式化 当我们在元素上设置width的时候,影响的是内容区的宽度  但是当我们又为元素指定指定了内边距 边框 外边距 还是会增加宽度值  (IE传统盒模型 内边距 边框 会在元素的宽度内扩展 ma ...

  7. <SharePoint 2013 用户界面设计与品牌化>学习系列之---基础

    什么是SharePoint界面与品牌化设计 这一章主要介绍了: 为什么要品牌化SharePoint 介绍一些内部和互联网的SharePoint网站 简单 中等 复杂的三种品牌化方式 简单难度: 普通用 ...

  8. Convert part to feature command

    Search 库主页 Related Links ArcObjects SDK for ArcGIS 10Microsoft Help System Documentation Convert par ...

  9. 转:C# 使用NLog记录日志

    原文:http://www.cnblogs.com/felixnet/p/5498759.html NLog是一个记录日志组件,和log4net一样被广泛使用,它可以将日志保存到文本文件.CSV.控制 ...

  10. 响应式SharePoint模版页

    一张好的皮肤显然的会给你的项目加分不少.特别是大部分的项目,UI甚至可以决定成败. SharePoint在这方面一直都做得不好,曾经我有好多项目都是坐在美工旁边来一起修改样式.痛苦的经历. 不久以前, ...