Uva 839 Not so Mobile

题意:这是一个类似于树的天平,这个天平的每一端都有可能由一个子天平构成,而每个天平都满足一个公式 WL * DL = WR * DR,其中WL,WR分别代表左边和右边物品的重量,DL,DR分别代表左边和右边物品里天平中心的距离。

输入分析:对于每个输入的四个数,如果WL或WR为0时,则代表接下来的输入代表子天平的数据,如果WL和WR同时为0,则输入数据先描述左子天平的状态,其次是右子天平。

要求:判断该数据是否可以构成一个平衡的天平。

思路:根据这个天平的描述,我很自然的想到了递归,有点类似于二叉树的后续遍历。对每一个子天平都需判断是否能够构成平衡,只要有一个不行,就需要输出“NO”;否则输出“YES”。

/*
    UvaOJ 839
    Emerald
    Thu 28 May 2015
*/
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

bool isEqual; // means the status is equal

int Moblie() {

    int leftWeight, leftDis, rightWeight, rightDis;
    scanf( "%d%d%d%d", &leftWeight, &leftDis, &rightWeight, &rightDis );
    leftWeight = leftWeight == 0 ? Moblie() : leftWeight;
    rightWeight = rightWeight == 0 ? Moblie() : rightWeight;
    if( leftWeight * leftDis != rightWeight * rightDis ) {
        isEqual = false;
    }
    return leftWeight + rightWeight;
} // return the totalWeight

int main() {
    int T;
    cin >> T;
    while( T -- ) {
        isEqual = true;
        Moblie();
        printf("%s\n", isEqual ? "YES" : "NO" );
        if( T != 0 ) {
            printf("\n");
        }
    }
    return 0;
}
时间: 2024-08-08 09:27:24

Uva 839 Not so Mobile的相关文章

[2016-02-09][UVA][839][Not so Mobile]

时间:2016-02-09 12:10:40 星期二 题目编号:UVA 839 题目大意:给定一个秤,已知秤两边的重量和到支点的距离,问秤能否平衡 分析:每个秤都是一个树的结构,只需要遍历一遍,看每个分支点能否平衡即可 方法:根绝输入,dfs下去,判断每个节点是否平衡即可 解题过程遇到问题: 刚开始,不平衡,返回值设置成0,没考虑为0的情况,不平衡的时候 改成返回-1就AC了. 刚开始忘记输出每组数据之间的空行 这里写法把整棵树都遍历了一遍,事实上,当出现不平衡的点,读取完数据之后,就可以直接返

uva 839 not so mobile——yhx

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. (picture copy failed,cou h

UVa 839 Not so Mobile(树的递归输入)

题意  判断一个树状天平是否平衡   每个测试样例每行4个数  wl,dl,wr,dr  当wl*dl=wr*dr时  视为这个天平平衡  当wl或wr等于0是  下一行将是一个子天平  如果子天平平衡  wl为子天平的wl+wr  否则整个天平不平衡 容易看出  输入是递归的  所以我们可以直接递归  边输入边判断 #include<cstdio> using namespace std; bool solve(int &w) { int wl, dl, wr, dr; bool m

UVA 839 Not so Mobile (递归建立二叉树)

题目连接:http://acm.hust.edu.cn/vjudge/problem/19486 给你一个杠杆两端的物体的质量和力臂,如果质量为零,则下面是一个杠杆,判断是否所有杠杆平衡. 分析:递归.直接递归求解即可. #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climit

UVA 839 Not so Mobile (递归建树 判断)

#include<cstdio> #include<iostream> #include<queue> #include<cstring> #include<string> #include<math.h> #include<stack> #include<cstdlib> #include<algorithm> #include<cctype> #include<sstream&

Not so Mobile UVA 839

说说: 还是简单的二叉树遍历的题目.这道题不过是将一棵树的左右子树作为杠杆.若存在左子树于相应距离的乘积和右子树相应距离的乘积不等,或者说不平衡,则输出NO.全部平衡,则整棵树平衡,输出YES.解法的话,递归判断即可. 源代码: #include <stdio.h> int mobile(int*); int main(){ int T,w; //freopen("data","r",stdin); scanf("%d",&T

UVa 839 (递归方式读取二叉树) Not so Mobile

题意: 递归的方式输入一个树状天平(一个天平下面挂的不一定是砝码还可能是一个子天平),判断这个天平是否能满足平衡条件,即W1 * D1 == W2 * D2. 递归的方式处理输入数据感觉很巧妙,我虽然能理解,但自己是写不出来的. 这里的参数是传引用,所以是在递归回来的时候才会赋值的. 1 //#define LOCAL 2 #include <iostream> 3 using namespace std; 4 5 bool solve(int& w) 6 { 7 int w1, d1

天平 (Not so Mobile UVA - 839)

题目描述: 题目思路: 1.DFS建树 2.只有每个树的左右子树都平衡整颗树才平衡 1 #include <iostream> 2 using namespace std; 3 4 bool solve(int& w) 5 { 6 int d1,w1,d2,w2; 7 cin >> w1 >> d1 >> w2 >> d2 ; 8 bool b1 = true ,b2 = true ; 9 if(!w1) b1 = solve(w1) ;

UVa 839 天平

原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=780 先建立二叉树,之后遍历. 1 #include<iostream> 2 using namespace std; 3 4 struct Node 5 { 6 int W1, D1, W2, D2; 7 Node *left; 8 Node *right; 9 };