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=;
const int M=;
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 = ;
return Wl+Wr;
} int main()
{
int T;
while (cin >> T)
while (T --) {
flag = ;
tree();
if (flag)
printf("YES\n");
else printf("NO\n");
if (T) printf("\n");
}
return ;
}
UVA 839 Not so Mobile (递归建立二叉树)的更多相关文章
- UVA.839 Not so Mobile ( 二叉树 DFS)
UVA.839 Not so Mobile ( 二叉树 DFS) 题意分析 给出一份天平,判断天平是否平衡. 一开始使用的是保存每个节点,节点存储着两边的质量和距离,但是一直是Runtime erro ...
- UVa 839 -- Not so Mobile(树的递归输入)
UVa 839 Not so Mobile(树的递归输入) 判断一个树状天平是否平衡,每个测试样例每行4个数 wl,dl,wr,dr,当wl*dl=wr*dr时,视为这个天平平衡,当wl或wr等于0是 ...
- UVa 839 Not so Mobile (递归思想处理树)
Before being an ubiquous communications gadget, a mobilewas just a structure made of strings and wir ...
- uva 839 not so mobile——yhx
Not so Mobile Before being an ubiquous communications gadget, a mobile was just a structure made of ...
- Uva 839 Not so Mobile
0.最后输出的yes no的大小写 1.注意 递归边界 一直到没有左右子树 即b1=b2=false的时候 才返回 是否 天平平衡. 2.注意重量是利用引用来传递的 #include <io ...
- UVa 699 The Falling Leaves(递归建树)
UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶 而且叶子只会垂直下落 每个节点保存的值为那个节点上的叶子数 求所有叶子全部下落后 地面从左到右每 ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- UVa 839 天平
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- BZOJ 2226 [Spoj 5971] LCMSum | 数论拆式子
题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2226 题解: 题目要求的是Σn*i/gcd(i,n) i∈[1,n] 把n提出来变成Σi/g ...
- BZOJ3529 [Sdoi2014]数表 【莫比乌斯反演】
3529: [Sdoi2014]数表 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2151 Solved: 1080 [Submit][Status ...
- 用JavaScript实现一个简单的树结构
数据源用数组混json结构,实现了基本的功能.效率一般,跟 dhtree 梅花雪树对比了下,都差不多. (ps感觉比dhtree快点,跟梅花雪树差不多,个人测试) 这个实现树的原理是根据json,不断 ...
- java程序在centos7里面开机自启动
1.我们先来个简单的start,status,stop程序: [root@localhost ~]# cat /home/tomcat/jarservice.sh #!/bin/bashCU_PID= ...
- idea使用(一)
基本上正式开发的常用工具基本都集成了,而且基本都在你非常容易触到的位置.说说我比较常用的: 1.ant 你懂的 2.maven你也懂的 3.SVN相比之下,IDEA的SVN的提交提供了更多的选项和功能 ...
- ppk和pem证书互转
首先你得去下载个putty pem:通用证书格式 ppk:为putty下面的专有格式 pem->ppk 直接通过putty下的puttygen.exe 选的Load private ke ...
- ActiveMQ(3) ActiveMQ创建(simpleAuthenticationPlugin)安全认证
控制端安全认证: ActiveMQ目录conf下jetty.xml: <bean id="securityLoginService" class="org.ecli ...
- Hadoop之计数器与自定义计数器及Combiner的使用
1,计数器: 显示的计数器中分为四个组,分别为:File Output Format Counters.FileSystemCounters.File Input Format Counters和Ma ...
- bzoj1574 [Usaco2009 Jan]地震损坏Damage
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1574 [题解] 贪心把report的点的旁边所有点破坏即可. # include <s ...
- unicode字符串解码显示
# encoding: utf-8 ''' unicode字符串解码显示 ''' import sys reload(sys) sys.setdefaultencoding('utf-8') a = ...