UVa839 Not so Mobile
我的解法: 建树,递归判断
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std; struct Node {
Node() {
wl=wr=dl=dr=0;
l=r=0;
}
int wl;
int dl;
int wr;
int dr;
Node* l;
Node* r;
}; Node* build()
{
int wl, wr, dl, dr;
scanf("%d%d%d%d", &wl, &dl, &wr, &dr);
Node* root=new Node;
if(wl==0)
{
Node* left=build();
wl=left->wl + left->wr;
root->l=left;
}
if(wr==0)
{
Node* right=build();
wr=right->wl + right->wr;
root->r=right;
} root->wl=wl;
root->wr=wr;
root->dl=dl;
root->dr=dr;
return root;
} bool equilibrium(Node* root)
{
if(!root)
return true;
bool el=equilibrium(root->l);
bool er=equilibrium(root->r);
if(el&&er)
{
return (root->wl * root->dl == root->wr * root->dr);
}
else
{
return false;
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva839.in", "r", stdin);
#endif
int T;
scanf("%d", &T);
while(T--) {
Node* root=build();
if(equilibrium(root))
printf("YES\n");
else
printf("NO\n"); if(T!=0)
printf("\n");
} return 0;
}
解答解法:
// UVa839 Not so Mobile
// Rujia Liu
// 题意:输入一个树状天平,根据力矩相等原则判断是否平衡。采用递归方式输入,0表示中间结点
// 算法:在“建树”时直接读入并判断,并且无须把树保存下来
#include<iostream>
using namespace std; // 输入一个子天平,返回子天平是否平衡,参数W修改为子天平的总重量
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\n"; else cout << "NO\n";
if(T) cout << "\n";
}
return 0;
}
UVa839 Not so Mobile的更多相关文章
- 递归输入与引用传值(UVa839 Not so Mobile)
题目的大意是一个树形天平,输入给出样例的个数,然后空一行,每行4个数W1,D1,W2,D2,分别代表天平左侧的重量.力臂和天平右侧的重量.力臂.如果W1或者W2为0,则代表该节点有左子树或右子树,如果 ...
- 【数据结构】Not so Mobile (6-9)
[UVA839]Not so Mobile 算法入门经典第6章6-9(P157) 题目大意:输入一个树状天平,根据力矩相等原则判断是否平衡. 试题分析:貌似没有什么难点…… #include<i ...
- 题解【UVA839】天平 Not so Mobile
Description Input Output Examples Input 1 0 2 0 4 0 3 0 1 1 1 1 1 2 4 4 2 1 6 3 2 Output YES Transla ...
- UWP开发之Mvvmlight实践七:如何查找设备(Mobile模拟器、实体手机、PC)中应用的Log等文件
在开发中或者后期测试乃至最后交付使用的时候,如果应用出问题了我们一般的做法就是查看Log文件.上章也提到了查看Log文件,这章重点讲解下如何查看Log文件?如何找到我们需要的Packages安装包目录 ...
- ipad&mobile通用webapp框架前哨战
响应式设计的意义 随着移动设备的发展,移动设备以迅猛的势头分刮着PC的占有率,ipad或者android pad的市场占有率稳步提升,所以我们的程序需要在ipad上很好的运行,对于公司来说有以下负担: ...
- jQuery Mobile入门
转:http://www.cnblogs.com/linjiqin/archive/2011/07/17/2108896.html 简介:jQuery Mobile框架可以轻松的帮助我们实现非常好看的 ...
- 解决Jquery mobile点击较长文本body的时候Header和footer会渐入渐出的问题
在做一个Phonegap+Jqm工程的时候,出现了如题的问题,相信很多人都遇到过Jquerymobile点击body时候header和footer会闪烁的显示和隐藏问题,fixed却并不能真 ...
- jquery mobile 问问多多
jquery mobile 问题多多,兼容性太差.android4.1下完全崩溃.以后再也不用jquery mobile了
- front end about mobile web techs
WEB OF DEVICES http://www.w3.org/standards/webofdevices/ MOBILE WEB http://www.w3.org/standards/webd ...
随机推荐
- group by的使用
1.概述 2.原始表 3.简单Group By 4.Group By 和 Order By 5.Group By中Select指定的字段限制 6.Group By All 7.Group By与聚合函 ...
- Solr与Mysql简单集成
Solr与Mysql数据库的集成,实现全量索引.增量索引的创建. 基本原理很简单:在Solr项目中注册solr的DataImportHandler并配置Mysql数据源以及数据查询sql语句.当我们通 ...
- [Everyday Mathematics]20150113
设 $f\in C^2(0,+\infty)$ 适合 $$\bex \lim_{x\to 0^+}f'(x)=-\infty,\quad \lim_{x\to 0^+}f''(x)=+\infty. ...
- Oracle归档方式设置
一 设置为归档方式Sql代码 sql> archive log list; #查看是不是归档方式 sql> alter system set log_archive_start=true ...
- 《Python 学习手册4th》 第十四章 迭代器和解析
''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...
- 使用jQuery Mobile实现通讯录
jQuery Mobile 通讯录 拨打电话作者:方倍工作室 地址: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional/ ...
- bzoj 3143 [Hnoi2013]游走(贪心,高斯消元,期望方程)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3143 [题意] 给定一个无向图,从1走到n,走过一条边得到的分数为边的标号,问一个边的 ...
- Codeforces Round #364
http://codeforces.com/contest/701 A - Cards 水 // #pragma comment(linker, "/STACK:102c000000,102 ...
- 在Toast里面显示图片
关于怎么在Toast里面显示图片,首先自定义一个toast,在自定义一个布局,这个布局你想让toast显示什么样的布局就定义什么样的,然后在自定的布局中放一个ImageView,在把自己自定义 ...
- Linux中MySQL5.5解压版普通用户安装
#查看本机mysql 安装路径 [hadoop@SY-0134 toolkit]$ rpm -qa|grep -i mysql [hadoop@SY-0134 toolkit]$ whereis my ...