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");
}
}

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

最新文章

  1. 如何解决例如i++的线程不安全性
  2. hdfs client access the hdfs cluster not in one domain
  3. Android BLE 蓝牙编程(三)
  4. 使用行为树(Behavior Tree)实现游戏AI
  5. Apache Spark源码走读之10 -- 在YARN上运行SparkPi
  6. hdoj 2098 分拆素数和
  7. PHP数组foreach后使用current取值的问题
  8. c# 菜单无限极分类-利用递归
  9. 继续推荐几款VisualStudio的插件
  10. Docker学习笔记 - Docker的远程访问
  11. 约定Service构建方式
  12. Linux安装配置Redis,CentOS7下安装Redis教程
  13. Android Handler面试解析
  14. gdb调试问题汇总
  15. idea创建web聚合工程(2)
  16. loopback 代码解析
  17. MySQL存储引擎与数据类型
  18. 【问题记录】uwsgi部署并启动俩个几乎一样的python flask web app,发现有一个app响应时间非常长
  19. 本地为Windows,使用Xshell登录Linux云主机
  20. openerp学习笔记 按客户电话、名称模糊查找选择客户(name_search)及客户名称自定义显示(name_get)

热门文章

  1. 【JS复习笔记】02 对象与函数
  2. 【JVM】2、关于jdk7的MethodHandle类
  3. svn的管理与维护要点—纯手工编写
  4. HTML &#183; 图片热点,网页划区,拼接,表单
  5. [程序人生]前途无&quot;亮‘’的大学
  6. [js开源组件开发]图片放大镜
  7. js验证真实姓名与身份证号,手机号
  8. 导入myeclipse项目出现的问题及解决方案
  9. This application is currently offline. To enable the application, remove the app_offline.htm file from the application root directory.
  10. view.performClick()触发点击事件