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. 笔记之Cyclone IV第一卷第三章器件中的存储器模块

    嵌入式存储器结构由一列列 M9K 存储器模块组成,通过对这些 M9K 存储器模块进行配置,可以实现各种存储器功能,例如:RAM.移位寄存器. ROM 以及FIFO 缓冲器. M9K 存储器模块支持以下 ...

  2. Socket编程模式

    Socket编程模式 本文主要分析了几种Socket编程的模式.主要包括基本的阻塞Socket.非阻塞Socket.I/O多路复用.其中,阻塞和非阻塞是相对于套接字来说的,而其他的模式本质上来说是基于 ...

  3. Visual Studio 2015编译安装配置QT5.5.1(含QTWEBKIT)

    尽管QT5.5.1和VisualStudio 2015都已经发布很久了,但是QT项目组视乎不会为QT5.5.1专门发布预编译的QT5.5.1 for windows(2015)版本的,也不会专门发布V ...

  4. Impossible WPF Part 2: Binding Expressions

    原文 http://www.11011.net/wpf-binding-expressions Back in April I posted an idea for building an expre ...

  5. ubuntu学习: apt-get命令

    1.apt-get update 更新软件源本地缓存文件 2.apt-cache search 查找软件包,找到想要安装的包,如 sudo apt-cache search mysql-server ...

  6. WebView.destroy() called while still attached 的解决的方法

    能够如今webView的父组件中删除该webview,然后再Destroy parent.removeView(webView); 然后 webView.removeAllViews(); webVi ...

  7. 再造 “手机QQ” 侧滑菜单(三)——视图联动

    代码示例:https://github.com/johnlui/SwiftSideslipLikeQQ 本 文中,我们将一起使用 UINavigationController 来管理主视图,并实现点击 ...

  8. Java --CountDownLatch简介

    CountDownLatch 1.类介绍 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.用给定的计数 初始化 CountDownLatch.由于调用了 coun ...

  9. Qt信号槽的一些事(第一次知道信号还有返回值,以及Qt::UniqueConnection)

    注:此文是站在Qt5的角度说的,对于Qt4部分是不适用的. 1.先说Qt信号槽的几种连接方式和执行方式. 1)Qt信号槽给出了五种连接方式: Qt::AutoConnection 0 自动连接:默认的 ...

  10. Bootstrap技术: 模式对话框的使用

    一.概述 说到模式对话框,大家肯定都会想到windows下GUI程序,在gui程序中,有大量的对话框. 在web程序中,随着页面交互式功能的增多,有很多场景下也会用到对话框.在html原生的支持下,有 ...