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. PUT 还是 POST ?

    http://www.oschina.net/translate/put-or-post http://my.oschina.net/u/1263964/blog/268932 这两个方法咋一看都可以 ...

  2. 最小生成树Prim算法(邻接矩阵和邻接表)

    最小生成树,普利姆算法. 简述算法: 先初始化一棵只有一个顶点的树,以这一顶点开始,找到它的最小权值,将这条边上的令一个顶点添加到树中 再从这棵树中的所有顶点中找到一个最小权值(而且权值的另一顶点不属 ...

  3. MyEclipse8.6安装SVN 教程 与遇到的问题

    按网上的多种方式都不好用     最后这种好用 了! 写此文做记录. MyEclipse版本:8.6 SVN版本:1.6.9 MyEclipse版本要对应SVN版本.否则会出错. 教程: 1.下载最新 ...

  4. RabbitMQ与AMQP协议详解

    1. 消息队列的历史 了解一件事情的来龙去脉,将不会对它感到神秘.让我们来看看消息队列(Message Queue)这项技术的发展历史. Message Queue的需求由来已久,80年代最早在金融交 ...

  5. android onNewIntent

    在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是当您需要让后台运行的Activity回到前台并传递一些数据可能就 ...

  6. Android5.0新特性——Material Design简介

    Material Design Material Design简介 Material Design是谷歌新的设计语言,谷歌希望寄由此来统一各种平台上的用户体验,Material Design的特点是干 ...

  7. java开发-技能要求-分词频度统计

    描述: 一哥们离职找工作,最近聊了聊面试待遇要求一类的事情,有些感触. 在一个公司呆的时间长了,对市场上对开发的要求已经不那么敏感了,也不知道人家要求哪些技能.一个公司的业务是有限的,呆了2年,3年, ...

  8. 关于制作报奖PPT的小结

    从9月26日接到制作报奖材料的任务开始,6个人(不包括审查领导和美工人员)忙活了半个多月终于交稿了,翻一下时间日志,10月1日前大概投入了13个小时,10月13日交稿又讨论修改了67个小时,总共算起来 ...

  9. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q77-Q80)

    Question 77You have a SharePoint list named Announcements.You have an event receiver that contains t ...

  10. Swing(一):JFrame框架窗体

    Swing窗体是一个组件,也是可视化的窗体,可以将其他组件放在这里.Jfream框架是一个容器,是Swing程序中各个组件的载体,可以将它看做为 一个容器,在开发中可以通过java.swing.jfr ...