uva 839 not so mobile——yhx
2016-05-02 16:42
387 查看
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.
[align=center] [/align]
[align=center] [/align]
[align=center] [/align]
[align=center](picture copy failed,cou huo zhe kan ba.)[/align]
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.
[align=center] [/align]
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.
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.
极其精简的代码。算法没什么,具体实现见注释。
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.
[align=center] [/align]
[align=center] [/align]
[align=center] [/align]
[align=center](picture copy failed,cou huo zhe kan ba.)[/align]
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.
[align=center] [/align]
Input
The input begins with a single positive integer on a line byitself 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 descriptionbelow. 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=1,b2=1; 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 1; else return 0; } int main() { int i,n,x; scanf("%d",&n); for (i=1;i<=n;i++) { if (slv(x)) printf("YES\n"); else printf("NO\n"); if (i!=n) printf("\n"); } }
极其精简的代码。算法没什么,具体实现见注释。
相关文章推荐
- 循环 wxl
- uva 297 quadtrees——yhx
- uva 572 oil deposits——yhx
- uva 10129 play on words——yhx
- uva 10562 undraw the trees(烂题) ——yhx
- calc 多项式计算 (STL版和非STL版) -SilverN
- NOIP2012普及组 (四年后的)解题报告 -SilverN
- NOIP2007 T1奖学金 解题报告-S.B.S.
- 团队合作 3.0
- 1644 免费馅饼 题解(c++)(S.B.S.)
- 常用函数(1)
- vb6.0 Access数据库
- Hello world S.B.S.
- Helloworld -SilverN
- HDU5572(计算几何+精度)
- Helloworld -SilverN
- Hello world S.B.S.
- 常用函数(1)
- 1644 免费馅饼 题解(c++)(S.B.S.)
- 后缀数组 UVA 11107 Life Forms