UVa 839 天平
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=780
先建立二叉树,之后遍历。
#include<iostream>
using namespace std; struct Node
{
int W1, D1, W2, D2;
Node *left;
Node *right;
}; bool flag; Node* creatTree()
{
Node* root = new Node;
cin >> root->W1 >> root->D1 >> root->W2 >> root->D2;
if (root->W1 == ) root->left = creatTree();
else root->left = NULL;
if (root->W2 == ) root->right = creatTree();
else root->right = NULL;
return root;
} int balance(Node* root)
{
if (flag == false) return ;
if (root->W1 == ) root->W1 = balance(root->left);
if (root->W2 == ) root->W2 = balance(root->right);
if (root->W1*root->D1 != root->W2*root->D2) flag = false;
return(root->W1 + root->W2);
} void remove_tree(Node* u)
{
if (u == NULL) return;
remove_tree(u->left);
remove_tree(u->right);
delete u;
} int main()
{
int t;
cin >> t;
while (t--)
{
Node* root = NULL;
flag = true;
root=creatTree();
flag = true;
balance(root);
if (flag == true) cout << "YES" << endl;
else cout << "NO" << endl;
remove_tree(root);
if (t != ) cout << endl;
}
}
刘汝佳的书上给出了一个引用传值的简单的方法
#include<iostream>
using namespace std; bool solve(int &W)
{
int W1, D1, W2, D2;
bool b1 = true, b2 = true;
cin >> W1 >> D1 >> W2 >> D2;
if (!W1) b1 = solve(W1);
if (!W2) b2 = solve(W2);
W = W1 + W2;
return b1&&b2 && (W1*D1 == W2*D2);
} int main()
{
int T, W;
cin >> T;
while (T--)
{
if (solve(W)) cout << "YES" << endl;
else cout << "NO" << endl;
if (T) cout << endl;
}
return ;
}
UVa 839 天平的更多相关文章
- Uva 839天平(二叉树dfs, 递归建树)
题意: 给定一个天平长度 输入格式为 wl dl wr dr 分别代表天平左边长度,左边重量, 右边长度, 右边重量. 如果重量为0, 说明下面还有一个天平, 递归给出. 样例输入:10 2 0 40 ...
- 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是 ...
- 天平 (Not so Mobile UVA - 839)
题目描述: 题目思路: 1.DFS建树 2.只有每个树的左右子树都平衡整颗树才平衡 #include <iostream> using namespace std; bool solve( ...
- UVa 1354 天平难题
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Uva 839 Not so Mobile
0.最后输出的yes no的大小写 1.注意 递归边界 一直到没有左右子树 即b1=b2=false的时候 才返回 是否 天平平衡. 2.注意重量是利用引用来传递的 #include <io ...
- UVa 839 (递归方式读取二叉树) Not so Mobile
题意: 递归的方式输入一个树状天平(一个天平下面挂的不一定是砝码还可能是一个子天平),判断这个天平是否能满足平衡条件,即W1 * D1 == W2 * D2. 递归的方式处理输入数据感觉很巧妙,我虽然 ...
- UVA 839 (13.08.20)
Not so Mobile Before being an ubiquous communications gadget, a mobile wasjust a structure made of ...
- 【紫书】【重要】Not so Mobile UVA - 839 递归得漂亮
题意:判断某个天平是否平衡,输入以递归方式给出. 题解:递归着输入,顺便将当前质量作为 &参数 维护一下,顺便再把是否平衡作为返回值传回去. 坑:最后一行不能多回车 附:天秀代码 #defin ...
随机推荐
- HDU 4627 E(Contest #3)
Description There are many unsolvable problem in the world.It could be about one or about zero.But t ...
- [Java]Java简介
Java版本历史 1995年5月23日,Java语言诞生 1996年1月,第一个JDK1.0诞生 1996年4月,10个最主要的操作系统供应商申明将在其产品中嵌入JAVA技术 1996年9月,约8.3 ...
- python——周边
Pythonic的禅意 import this python是用c语言写的.传说python不止有C语言实现,还有java实现,还有python实现的python,甚至还有js实现的python. p ...
- 收藏的博客--PHP
32位Win7下安装与配置PHP环境(一至三) http://blog.csdn.net/yousuosi/article/details/9448903
- php大力力 [033节] 随便看看:PHP程序员学习C++
php大力力 [033节] 随便看看:PHP程序员学习C++ 2014 兄弟连高洛峰 PHP教程14.1.7 在PHP脚本中操作MySQL数据库4 观看 - 56.com http://www.med ...
- Android如何分析和研究Monkey Log文件
Log 在android中的地位非常重要,要是作为一个android程序员不能过分析log这关,算是android没有入门吧 . 下面我们就来说说如何处理log文件 . 什么时候会有Log文件的产生 ...
- ubuntu下的wps不能使用中文.
首先如果wps不能用中文的话应该是 excell ppt word 都不能用 . 我的办法需要改三个文件 . 先后打开这三个文件 . xpower@xpower-CW65S:~$ sudo vim / ...
- Python的魔法方法 .
基本行为和属性 __init__(self[,....])构造函数 . 在实例化对象的时候会自动运行 __del__(self)析构函数 . 在对象被回收机制回收的时候会被调用 __str__(sel ...
- String.Format 全汇总
C#格式化数值结果表 字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0 ...
- nginx的启动,停止命令
停止操作停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文 章)来进行的步骤1:查询nginx主进程号ps -ef | grep nginx在进程列表里 面找master进程,它的编 ...