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 ...
随机推荐
- Convert.ToInt16 与 Convert.ToInt32 区别
取值的范围不同: int16:-32768 到 32767 int32:-2,147,483,648 到 2,147,483,647
- [vijos P1512] SuperBrother打鼹鼠
这周好好码树状数组和线段树!!之前没写过二维树状数组,凭借一维的思路居然写了个比较像模像样的东西出来,原来我没那么脑残.唯一要注意的就是getsum四个矩形加减的边界条件,这里看了别人标程才意识到错误 ...
- Python应用与实践
http://www.cnblogs.com/skynet/archive/2013/05/06/3063245.html
- Xutils的使用 转载 带自己细细研究
单例模式static DbUtils db = null; public static DbUtils getDb(Context context) { if (context == null) { ...
- C语言中数组的几种输入
- [C/C++]数据类型与变量
1.类型: 1.1.简介 1.1.1.基本类型(base type)和复合类型(compound type): 复合类型是指基于其它类型定义的类型.([1].2.3 p45)那么基本类型就是复合类型基 ...
- 使用windows远程桌面连接Windows Azure中的Ubuntu虚拟机
1.创建ubuntu虚拟机,这里同样不再赘述,创建过程和创建Windows虚拟机基本一样,只是登录可以选择密钥注入或者用户名密码(为了方便我选择了用户名密码认证),创建完成后,查看虚拟机详情中的端口信 ...
- Android开发-开发前的配置
一.JDK (不用安装) JDK是 java 语言的软件开发工具,主要用于移动设备.嵌入式设备上的java应用程序. JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JA ...
- JavaScript常用检测脚本(正则表达式)
转自:http://www.cnblogs.com/skylaugh/archive/2006/09/25/514492.html 文件名称:check.js 说明:JavaScript脚本,用于检查 ...
- HTML的常用标签
<!--编写HTML常用的标签 文字版面的编辑 格式标签 文本标签 超链接和锚点 图像和图像地图 --> <!DOCTYPE html PUBLIC "-//W3C//DT ...