原题链接: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 天平的更多相关文章

  1. Uva 839天平(二叉树dfs, 递归建树)

    题意: 给定一个天平长度 输入格式为 wl dl wr dr 分别代表天平左边长度,左边重量, 右边长度, 右边重量. 如果重量为0, 说明下面还有一个天平, 递归给出. 样例输入:10 2 0 40 ...

  2. UVA.839 Not so Mobile ( 二叉树 DFS)

    UVA.839 Not so Mobile ( 二叉树 DFS) 题意分析 给出一份天平,判断天平是否平衡. 一开始使用的是保存每个节点,节点存储着两边的质量和距离,但是一直是Runtime erro ...

  3. UVa 839 -- Not so Mobile(树的递归输入)

    UVa 839 Not so Mobile(树的递归输入) 判断一个树状天平是否平衡,每个测试样例每行4个数 wl,dl,wr,dr,当wl*dl=wr*dr时,视为这个天平平衡,当wl或wr等于0是 ...

  4. 天平 (Not so Mobile UVA - 839)

    题目描述: 题目思路: 1.DFS建树 2.只有每个树的左右子树都平衡整颗树才平衡 #include <iostream> using namespace std; bool solve( ...

  5. UVa 1354 天平难题

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. Uva 839 Not so Mobile

    0.最后输出的yes no的大小写 1.注意 递归边界   一直到没有左右子树 即b1=b2=false的时候 才返回 是否 天平平衡. 2.注意重量是利用引用来传递的 #include <io ...

  7. UVa 839 (递归方式读取二叉树) Not so Mobile

    题意: 递归的方式输入一个树状天平(一个天平下面挂的不一定是砝码还可能是一个子天平),判断这个天平是否能满足平衡条件,即W1 * D1 == W2 * D2. 递归的方式处理输入数据感觉很巧妙,我虽然 ...

  8. UVA 839 (13.08.20)

     Not so Mobile  Before being an ubiquous communications gadget, a mobile wasjust a structure made of ...

  9. 【紫书】【重要】Not so Mobile UVA - 839 递归得漂亮

    题意:判断某个天平是否平衡,输入以递归方式给出. 题解:递归着输入,顺便将当前质量作为 &参数 维护一下,顺便再把是否平衡作为返回值传回去. 坑:最后一行不能多回车 附:天秀代码 #defin ...

随机推荐

  1. oracle安装后登录

    运行 sqlplus回车 longon as sysdba回车 回车 这样就可以登录了. SQL>create user username identified by password; SQL ...

  2. linux命令每日一练习-pwd,cd

    pwd显示当前路径. pwd -P没能明白什么意思, cd 进入目录 cd ..返回上级目录

  3. 百度地图开发 android App 数字签名(SHA1)获取办法

    简述: a.输入keytool -list -v -keystore debug.keystore,会得到三种指纹证书,选取SHA1类型的证书(密钥口令是android),这个获取到的SHA1的值和e ...

  4. sql 语句中使用条件判断case then else end

    sql 语句中使用条件判断case then else end范例: SELECT les.[nLessonNo] FROM BS_Lesson AS les WHERE les.[sClassCod ...

  5. 欢迎参加MVP主讲的Windows 10开发线上课程

    博客地址:http://blog.csdn.net/FoxDave Windows 10 Developer Readiness - Powered by MVPs - 由微软最有价值专家(MVP)主 ...

  6. Python学习路程day3

    set集合 ​set是一个无序且不重复的元素集合,访问速度快,天生解决重复问题 s1 = set() s1.add('luo')​ s2 = set (['luo','wu','wei','ling' ...

  7. BZOJ 1060 时态同步

    贪心. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  8. Windows系统下安装Beautiful Soup4的步骤和方法

    1.到http://www.crummy.com/software/BeautifulSoup/网站上下载,最新版本是4.3.2. 2.下载完成之后需要解压缩,假设放到D:\Python27下. 3. ...

  9. session 和 cookie 的区别和联系

    二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录下来.当下次你再光临同一个网站,WEB 服务器会先看看有没 ...

  10. 【转】Fast Entity Component System

    http://entity-systems.wikidot.com/fast-entity-component-system Summary Create a generic System class ...