UVA 839 (13.08.20)
| 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)的更多相关文章
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- 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 ...
- UVA 253 (13.08.06)
Cube painting We have a machine for painting cubes. It is supplied withthree different colors: blu ...
- UVA 156 (13.08.04)
Ananagrams Most crossword puzzle fans are used to anagrams--groupsof words with the same letters i ...
- UVA 10499 (13.08.06)
Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...
- UVA 10025 (13.08.06)
The ? 1 ? 2 ? ... ? n = k problem Theproblem Given the following formula, one can set operators '+ ...
- UVA 465 (13.08.02)
Overflow Write a program that reads an expression consisting of twonon-negative integer and an ope ...
- UVA 10494 (13.08.02)
点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...
- UVA 424 (13.08.02)
Integer Inquiry One of the first users of BIT's new supercomputer was Chip Diller. Heextended his ...
随机推荐
- ORACLE存储过程笔记3
ORACLE存储过程笔记3 流程控制 1.条件 if expression thenpl/sql or sqlend if; if expression thenpl/sql or sqlel ...
- u-boot分析——struct gd_t与struct bd_t
gd_t和bd_t是u-boot中两个重要的数据结构,在初始化操作很多都要靠这两个数据结构来保存或传递.分别定义在./include/asm/global_data.h和./include/asm/u ...
- 基于visual Studio2013解决C语言竞赛题之0521圆盘求和
题目
- java邮件小实例
新建一个包,名为mail 第一个类:MailSenderInfo.java ########################################### package com.util. ...
- Android PopupWindow显示位置和显示大小
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb3l1YW41MTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- Sublime 脚本 配置 (lua 和 JavaScript篇)
{ "cmd" :["C:/Lua/Lua.exe","$file"], "file_regex" :"^(? ...
- BZOJ 2073: [POI2004]PRZ( 状压dp )
早上这道题没调完就去玩NOI网络同步赛了.... 状压dp , dp( s ) 表示 s 状态下所用的最短时间 , 转移就直接暴力枚举子集 . 可以先预处理出每个状态下的重量和时间的信息 . 复杂度是 ...
- Qt实现不同Treewidget之间拖拽
拖拽是编程中经常要用到的,我这里主要是实习了Treewidget之间直接拖拽Item,按下Ctrl键的话是copy,不按Ctrl则是Move.以下是实现代码 class TreeItemMimeDat ...
- 通过jQuery的attr修改onclick
var js = "alert('B:' + this.id); return false;"; // creates a function from the "js&q ...
- docker学习笔记5:利用commit命令创建镜像 和 删除本地镜像
一.概述 创建镜像有两种方法,一是用commit命令,二是用dockerfile方法(这个更常用,在下面文章介绍).本章介绍commit方法. 在介绍commit命令前,我们先回顾下对代码的版本控制, ...