例题6-9 天平(Not so Mobile, UVa 839)
来源:互联网 发布:沈阳软件开发公司 编辑:程序博客网 时间:2024/05/20 21:59
递归建树并判断
1.本题无需建树,对每组数据的读入进行处理即可,这方面我很薄弱。
2.需要的得到的结果:是否平衡及重量,重量可以使用传引用的方式来改变。
#include <iostream>#include <string>#include <vector>#include <stack>#include <queue>#include <deque>#include <set>#include <map>#include <algorithm>#include <functional>#include <sstream>#include <utility>#include <cstring>#include <cstdio>#include <cstdlib>#include <ctime>#include <cmath>#include <cctype>#define CLEAR(a, b) memset(a, b, sizeof(a))#define IN() freopen("in.txt", "r", stdin)#define OUT() freopen("out.txt", "w", stdout)#define LL long long#define maxn 1005#define maxm 100005#define mod 10007#define INF 1e16+5#define EPS 1e-7#define PI 3.1415926535898#define N 4294967296using namespace std;//-------------------------CHC------------------------------//struct Node {int wl, dl, wr, dr, G;Node *left, *right;Node(int w1 = 0, int d1 = 0, int wr = 0, int dr = 0) : wl(w1), dl(d1), wr(wr), dr(dr), left(NULL), right(NULL), G(0) { }};bool ok = true;Node * build() {int wl, dl, wr, dr;scanf("%d%d%d%d", &wl, &dl, &wr, &dr);Node *root = new Node(wl, dl, wr, dr);if (!root->wl) root->left = build();if (!root->wr) root->right = build();if (!root->left && !root->right) {if (root->dl*root->wl != root->dr*root->wr) ok = false;root->G = root->wl + root->wr;}else if (root->left && root->right) {if (root->left->G*root->dl != root->right->G*root->dr) ok = false;root->G = root->left->G + root->right->G;}else if (root->left) {if (root->left->G*root->dl != root->wr*root->dr) ok = false;root->G = root->left->G + root->wr;}else {if (root->right->G*root->dr != root->wl*root->dl) ok = false;root->G = root->right->G + root->wl;}return root;}bool post(Node *root) {if (root) {printf("%d %d %d %d\n", root->wl, root->dl, root->wr, root->dr);post(root->left);post(root->right);}return true;}int main() {//IN(); OUT();int T;scanf("%d", &T);while (T--) {ok = true;Node *root = build();puts(ok ? "YES" : "NO");if (T) puts("");}return 0;}
1.本题无需建树,对每组数据的读入进行处理即可,这方面我很薄弱。
2.需要的得到的结果:是否平衡及重量,重量可以使用传引用的方式来改变。
#include <iostream>#include <string>#include <vector>#include <stack>#include <queue>#include <deque>#include <set>#include <map>#include <algorithm>#include <functional>#include <sstream>#include <utility>#include <cstring>#include <cstdio>#include <cstdlib>#include <ctime>#include <cmath>#include <cctype>#define CLEAR(a, b) memset(a, b, sizeof(a))#define IN() freopen("in.txt", "r", stdin)#define OUT() freopen("out.txt", "w", stdout)#define LL long long#define maxn 1005#define maxm 100005#define mod 10007#define INF 1e16+5#define EPS 1e-7#define PI 3.1415926535898#define N 4294967296using namespace std;//-------------------------CHC------------------------------//bool solve(int &W) {int w1, d1, w2, d2;scanf("%d%d%d%d", &w1, &d1, &w2, &d2);bool ok1 = true, ok2 = true;if (!w1) ok1 = solve(w1);if (!w2) ok2 = solve(w2);W = w1 + w2;return ok1 && ok2 && (w1*d1 == w2*d2);}int main() {//IN(); OUT();int T, W;scanf("%d", &T);while (T--) {puts(solve(W) ? "YES" : "NO");if (T) puts("");}return 0;}
阅读全文
0 0
- 例题6-9 天平(Not so Mobile, UVa 839)
- UVa 839-Not so Mobile(天平)
- 天平 (Not so Mobile, UVa 839)
- uva 839 (Not so mobile)天平 ---递归输入
- 天平(Not so Mobile)
- 例题6-9 天平 UVa 839 递归
- 例题6-9 UVa839 Not so Mobile(树)
- Uva839 Not so Mobile(天平)
- UVa839-Not so Mobile (天平)
- Not so Mobile 天平 UVA839
- Uva839 Not so Mobile 【递归输入】【例题6-9】
- uva 839 - Not so Mobile
- uva 839 Not so Mobile
- uva 839 - Not so Mobile
- UVa 839 - Not so Mobile
- UVa 839 - Not so Mobile
- uva-839 Not so Mobile
- uva 839 - Not so Mobile
- LPC2138的时钟
- 633
- 关于Vue.nextTick()的正确使用
- 双边滤波JAVA代码实现
- XAMPP 环境下配置Mysql root 密码设置
- 例题6-9 天平(Not so Mobile, UVa 839)
- java-4个访问控制权限
- Android设置控件的透明度
- spring4 staticproxy
- HDU 6198 number number number(规律+矩阵快速幂)
- 行为型-模板方法模式(Template Method)
- csp-ccf之Markdown
- xml转xsd,xsd,转java类
- Scanner