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 ...
随机推荐
- Chapter 14 观察者模式
观察者模式又叫做发布-订阅模式:定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己. 观察者模式所做的工作其 ...
- xcode生成的IOS安装文件的位置
通过xcode生成可以在IOS系统下运行的文件的具体设置: 1.首先,需要有相应的程序,并且在mac下的xcode编译后,能够在模拟器中完美运行. 2.单击xcode,打开Xcode > Pre ...
- linux下java窗口,正确显示中文
Tip1 1.在 JAVA_HOME/jre/lib/fonts/ 下建立个目录 fallback 2.在 fallback 里弄个中文字体最简单ln一下就好了 比如: ln -s /usr/shar ...
- 为Cocos2d-x的Android平台加入Protobuffer支持
为Cocos2d-x的Android平台加入Protobuffer支持 分类: 工作2013-11-27 18:00 386人阅读 评论(1) 收藏 举报 cocos2d-xandroid平台交叉编译 ...
- SimpleAdapter
1.视图 1)主视图 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...
- 算法之旅,直奔<algorithm>之十 count_if
count_if(vs2010) 引言 这是我学习总结<algorithm>的第十篇,这个重要的地方是设置条件.用的还是蛮多的.(今天下午挺恶心的,一下午就做一个面试题,调代码调傻了... ...
- 目录 of 2013-2014-1(内容已更新结束)
(内容已更新结束) UML部分: ---------------1.概述2.用例图3.类图4.顺序图 MVC部分: ----------------1.概述2.路由3.控制器4.视图5.模型6.安装部 ...
- java操作Excel处理数字类型的精度损失问题验证
java操作Excel处理数字类型的精度损失问题验证: 场景: CELL_TYPE_NUMERIC-->CELL_TYPE_STRING--->CELL_TYPE_NUMERIC POI版 ...
- 管理启示,不起毛的鹦鹉——leo锦书54
下面一个很长的故事后,我真的很期待明确: 一个人去买鹦鹉,看到一仅仅鹦鹉前标:此鹦鹉会两国语言,售价二百元.还有一仅仅鹦鹉前则标道:此鹦鹉会四门语言.售价四百元.该买那仅仅呢?两仅仅都毛色鲜亮, ...
- mp3播放器
1.视图 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...