Not so Mobile 

Before being an ubiquous communications gadget, a mobile wasjust a structure made of strings and wires suspending colourfullthings. This kind of mobile is usually found hanging over cradlesof small babies.

The figure illustrates a simple mobile. It is just a wire,suspended by a string, with an object on each side. It can also beseen as a kind of lever with the fulcrum on the point where thestring ties the wire. From the lever principle we know that tobalance a simple mobile the product of the weight of the objectsby their distance to the fulcrum must be equal. That isWl×Dl = Wr×Drwhere Dl is the left distance, Dr is theright distance, Wl is the left weight and Wris the right weight.

In a more complex mobile the object may be replaced by asub-mobile, as shown in the next figure. In this case it is not sostraightforward to check if the mobile is balanced so we need youto 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 indicatingthe 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 betweentwo consecutive inputs.

The input is composed of several lines, each containing 4 integersseparated by a single space. The 4 integers represent thedistances of each object to the fulcrum and their weights, in theformat: Wl Dl Wr Dr

If Wl or Wr is zero then there is a sub-mobile hanging fromthat end and the following lines define the the sub-mobile. Inthis case we compute the weight of the sub-mobile as the sum ofweights of all its objects, disregarding the weight of the wiresand strings. If both Wl and Wr are zero then the followinglines 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.

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

题意:

输入数据,描绘了一个天平,要求天平要平衡,其中有嵌套进去的天平,也要求不能倾斜~

然后平衡的公式应该都知道吧:w1 * d1 = w2 * d2;

思路:

递归不解释,数据出现0的时候就是递归的入口~

AC代码:

#include<stdio.h>

int flag;

int getW(int w) {
int tw1, td1, tw2, td2;
int p1, p2;
if(w == 0) {
scanf("%d %d %d %d", &tw1, &td1, &tw2, &td2);
p1 = getW(tw1) * td1;
p2 = getW(tw2) * td2;
if(p1 == p2)
return (p1/td1 + p2/td2);
else {
flag = 0;
return -1;
}
}
else
return w;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
int w1, d1, w2, d2;
int p1, p2;
flag = 1;
scanf("%d %d %d %d", &w1, &d1, &w2, &d2);
p1 = getW(w1) * d1;
p2 = getW(w2) * d2;
if(p1 == p2 && flag == 1) {
printf("YES\n");
if(T)
printf("\n");
}
else {
printf("NO\n");
if(T)
printf("\n");
}
}
return 0;
}

UVA 839 (13.08.20)的更多相关文章

  1. UVA 10194 (13.08.05)

    :W Problem A: Football (aka Soccer)  The Problem Football the most popular sport in the world (ameri ...

  2. UVA 573 (13.08.06)

     The Snail  A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...

  3. UVA 253 (13.08.06)

     Cube painting  We have a machine for painting cubes. It is supplied withthree different colors: blu ...

  4. UVA 156 (13.08.04)

     Ananagrams  Most crossword puzzle fans are used to anagrams--groupsof words with the same letters i ...

  5. UVA 10499 (13.08.06)

    Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...

  6. UVA 10025 (13.08.06)

     The ? 1 ? 2 ? ... ? n = k problem  Theproblem Given the following formula, one can set operators '+ ...

  7. UVA 465 (13.08.02)

     Overflow  Write a program that reads an expression consisting of twonon-negative integer and an ope ...

  8. UVA 10494 (13.08.02)

    点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...

  9. UVA 424 (13.08.02)

     Integer Inquiry  One of the first users of BIT's new supercomputer was Chip Diller. Heextended his ...

随机推荐

  1. mvc导航配置(View页面)

    @using Seyoit.Website.Config@using Seyoit.Website.Config.Navigation@{ var action = ViewContext.Route ...

  2. php MySQLi部分函数(面向对象和过程)

    版本支持:PHP > 5.0  MySQL > 4.0 2. mysqli 连接数据库: $db = new mysqli(host,user,password,database);返回一 ...

  3. win32多线程程序设计笔记(第五章)

    前面章节介绍了线程创建等过程,现在的问题是:如何在某个线程内终止另外一个正在运行的线程? windows核心编程中提到终止运行线程的方法: 1)线程函数自己返回: 2)线程通过调用ExitThread ...

  4. HDU 2040:亲和数

    亲和数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  5. mysql 参数optimizer_switch

    mysql 5.1中开始引入optimizer_switch, 控制mysql优化器行为.他有一些结果集,通过on和off控制开启和关闭优化器行为.使用有效期全局和会话两个级别,在5.5中optimi ...

  6. CMake使用之一

    概述 CMake是一个比make更高级的编译配置工具,它可以根据不同平台.不同的编译器,生成相应的Makefile或者vcproj项目. 通过编写CMakeLists.txt,可以控制生成的Makef ...

  7. activity变成Dialog的样式设置

    1.属性解析 <item name="android:windowFrame">@null</item> :Dialog的windowFrame框为无< ...

  8. 第四届蓝桥杯 c/c++真题

    第四届蓝桥杯 c/c++真题 <1>高斯日记 问题 大数学家高斯有个好习惯:无论如何都要记日记. 他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210 后来人们 ...

  9. spring MVC 如何获取session并实现传值到前台

    后台获取session: @RequestMapping("/usrlogin") public ModelAndView usrlogin(@RequestParam Strin ...

  10. Ubuntu下安装和配置mysql

    一.检查 1.检查是否已经安装mysql whereis mysql 2.检查mysql服务是否已经启动 sudo netstat -tap | grep mysql 如果没有安装,下面就进行安装. ...