UVA839 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.

????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.

Input
The input begins with a single positive integer on a line by 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.
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

问题链接UVA839 Not so Mobile
问题简述
????给定一个杠杆两端的物体的质量和力臂,如果质量为零,则下面是一个杠杆,判断是否所有杠杆平衡。
问题分析
????本来是一个树的平衡问题,用递归来解决比较简单。这里的解题递归程序可以看作是在创建递归树,原理上与建树是相同的。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++程序如下:

/* UVA839 Not so Mobile */

#include <bits/stdc++.h>

using namespace std;

// 递归程序:输入并且判定平衡
int tree(bool& flag)
{
    int wl, dl, wr, dr;
    scanf("%d%d%d%d", &wl, &dl, &wr, &dr);
    if (!wl) wl = tree(flag);
    if (!wr) wr = tree(flag);
    flag = wl * dl == wr * dr ? flag : false;
    return wl + wr;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--) {
        bool flag = true;
        tree(flag);
        printf("%s\n", flag ? "YES" : "NO");
        if(t) printf("\n");
    }

    return 0;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10449650.html

时间: 2024-11-08 20:55:31

UVA839 Not so Mobile【递归树】的相关文章

【POJ 1195】 Mobile phones (树状数组)

[POJ 1195] Mobile phones (树状数组) Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16761   Accepted: 7713 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The ar

(转)递归树求递归算法的时间复杂度

本文转载:http://www.cnblogs.com/wu8685/archive/2010/12/21/1912347.html 递归算法时间复杂度的计算方程式一个递归方程: 在引入递归树之前可以考虑一个例子: T(n) = 2T(n/2) + n2 迭代2次可以得: T(n) = n2 + 2(2T(n/4) + (n/2) 2) 还可以继续迭代,将其完全展开可得: T(n) = n2 + 2((n/2) 2 + 2((n/22)2 + 2((n/23) 2 + 2((n/24) 2 +-

ORACLE 递归树型结构统计汇总

区域平台统计报表,省--市--区 汇总,还有各级医院,汇总与列表要在一个列表显示. 用到ORACLE 会话时临时表  GLOBAL TEMPORARY TABLE     ON COMMIT PRESERVE ROWS; 递归树: START WITH P.PARENTORG = 'ROOT'               CONNECT BY PRIOR P.ORGCODE = P.PARENTORG; WITH 连续嵌套 记录一下便于查阅. CREATE OR REPLACE PACKAGE

UVA839 Not so Mobile【二叉树】【递归】

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. \epsfbox{p839a.eps} The fig

swift_枚举 | 可为空类型 | 枚举关联值 | 枚举递归 | 树的概念

***************可为空的类型 var demo2 :we_demo = nil 上面这个代码串的语法是错的 为什么呢, 在Swift中,所有的类型定义出来的属性的默认值都不可以是nil 不管是普通简单值类型还是引用类型 那我就是要让这个属性默认值为空,为nil 怎么办呢,很简单,用语法,在定义这个属性的时,在类型后面声明一个? 这样就表示这个属性除了指定类型的默认值外还可以是一个可为空的类型 在Java中,最常见的错误类型就是NullPoinExecption, 为什么就是要有Nu

最全C 语言常用算法详解-排序-队列-堆栈-链表-递归-树

具体 源代码 案例查看github,持续更新中............ github地址:https://github.com/Master-fd/C-Algorithm 1. 二分法查找 2. 冒泡排序 3. 插入排序 4. 希尔排序 5. 选择排序 6. 快速排序 7. 单链表实现堆栈 8. 单链表实现队列 9. 普通单链表 10. 递归实现斐波拉契数列 11. 递归实现strlen 12. 循环链表 13. 求素数 14. 双向链表 15. 顺序表实现队列 16. 顺序表实现栈 17. 顺

在Silverlight中使用HierarchicalDataTemplate为TreeView实现递归树状结构

将实体绑定到TreeView控件,实现树状结构的显示,如下图所示.这个功能通过HierarchicalDataTemplate实现. ? 1. 业务实体 作为举例,我定义了一个大家都很熟悉的Folder类型,即文件夹.我们都知道,文件夹又可以包含子文件夹,而且可以多层嵌套.所以,这是一个递归的结构体. public class Folder { public string Name { get; set; } public ObservableCollection<Folder> Folder

【DRP】删除递归树的操作

正如图呈现的树结构.本文从任意节点删除树形结构.提供解决方案 图中,不包括其他结点的是叶子结点.包括其他结点的是父结点,即不是叶子结点. 一 本文的知识点: (1)递归调用: 由于待删除的结点的层次是不确定的,假设是叶子结点则能够直接获取id直接删除,如:北京中医医院.华北区.假设待删除的结点是父结点,则须要继续向下查询,依次遍历出其子结点,从下往上依次删除,如'华北区'.因此我们使用递归调用. (2)保证事务的原子性 如果待删除的结点是'华北区'.则相当于删除了3条信息(华北区.北京市.北京中

Oracle递归树

例子下载: http://yunpan.cn/cLY4v5hdQeMY6 (提取码:ae65) 语法: select ... from <TableName>        where <Conditional-1>  //过滤        start with <Conditional-2>  //递归起点        connect by <Conditional-3>;  //连接限定 注意: 1.prior放在子节点端,则表示扫描树是以start