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

题目连接:http://acm.hust.edu.cn/vjudge/problem/19486

给你一个杠杆两端的物体的质量和力臂,如果质量为零,则下面是一个杠杆,判断是否所有杠杆平衡。

分析:递归。直接递归求解即可。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=300;
const int M=15005;
int flag;  
int tree()  
{  
    int Wl,Dl,Wr,Dr;  
    scanf("%d%d%d%d",&Wl,&Dl,&Wr,&Dr);  
    if (!Wl) Wl = tree();  
    if (!Wr) Wr = tree();  
    if (Wl*Dl != Wr*Dr) flag = 0;  
    return Wl+Wr;  
}  
  
int main()  
{  
    int T;  
    while (cin >> T)  
    while (T --) {  
        flag = 1;  
        tree();  
        if (flag)  
            printf("YES
");  
        else printf("NO
");  
        if (T) printf("
");  
    }  
    return 0;  
}  
View Code
原文地址:https://www.cnblogs.com/jianrenfang/p/5738303.html