您的位置:首页 > 其它

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

2016-08-04 20:34 453 查看
题目连接: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\n");
else printf("NO\n");
if (T) printf("\n");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: