立志用最少的代码做最高效的表达


Before being an ubiquous communications gadget, a mobile was just a structure made of strings and wires suspending colourfull things. This kind of mobile is usually found hanging over cradles of small babies.
The figure illustrates a simple mobile. It is just a wire, suspended by a string, with an object on each side. It can also be seen as a kind of lever with the fulcrum on the point where the string ties the wire. From the lever principle we know that to balance a simple mobile the product of the weight of the objects by their distance to the fulcrum must be equal. That is Wl × Dl = Wr × Dr where Dl
is the left distance,
Dr is the right distance, Wl is the left weight and Wr is the right weight.
In a more complex mobile the object may be replaced by a sub-mobile, as shown in the next figure.
In this case it is not so straightforward to check if the mobile is balanced so we need you to write a program that, given a description of a mobile as input, checks whether the mobile is in equilibrium or not.

Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The input is composed of several lines, each containing 4 integers separated by a single space.
The 4 integers represent the distances of each object to the fulcrum and their weights, in the format:
Wl Dl Wr Dr
If Wl or Wr is zero then there is a sub-mobile hanging from that end and the following lines define the the sub-mobile. In this case we compute the weight of the sub-mobile as the sum of weights of all its objects, disregarding the weight of the wires and strings. If both Wl and Wr are zero then the following lines define two sub-mobiles: first the left then the right one.

Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
Write ‘YES’ if the mobile is in equilibrium, write ‘NO’ otherwise.

Sample Input
1
0 2 0 4
0 3 0 1
1 1 1 1
2 4 4 2
1 6 3 2
Sample Output
YES


思路分析

经过观察很容易发现,本题的输入类似二叉树的先序遍历(即根左右)

以下三种解法为循序渐进的优化过程。

解法一:根据先序遍历建树, 然后后序遍历判断天平是否平衡

解法二:在建树的同时判断天平是否平衡

解法三:隐式建树,通过递归的过程动态判断天平是否平衡。


解法一:先序建树、后序判断

#include<bits/stdc++.h>
using namespace std;
struct Node {int wl, wr, dl, dr;        //value Node *l = NULL, *r = NULL;    //left subtree、right subtree
};string s;
int T;
bool readInput(int& wl, int& dl, int &wr, int& dr) { //读取输入 if(getline(cin, s) && !s.empty()) {stringstream input(s);       //stringstream用法 input >> wl >> dl >> wr >> dr;return true;     //成功 }else return false;    //失败
}Node* createTree() {   //递归建树(先序) Node* root = NULL;int wl, dl, wr, dr;if(readInput(wl, dl, wr, d2)) {  //读入成功 root = new Node;root->wl = wl; root->wr = wr;root->dl = dl; root->dr = dr;if(wl == 0) root->l = createTree();if(wr == 0) root->r = createTree(); }return root;
}int dfs(Node* root) {  //后序遍历并判断是否平衡if(root == NULL) return -1;  //空树if(root->l != NULL) root->wl = dfs(root->l); //左子树if(root->r != NULL) root->wr = dfs(root->r);    //右子树if(root->wl == -1 || root->wr == -1) return -1;  //非法,剪枝if(root->wl * root->dl == root->wr*root->dr) return root->wl+root->wr;   //平衡,返回左右重量和else return -1;  //非法标志 }int main() {scanf("%d  ", &T);        //两个空格可吸收换行for(int i = 0; i < T; i++) {Node* bt = createTree();printf("%s%s\n", dfs(bt)!=-1 ? "YES" : "NO", i!=T-1?"\n":"");if(i != T-1) getchar(); //空行吸收 } return 0;
}

解法二:建树的同时判断

#include<bits/stdc++.h>
using namespace std;
struct Node{int Wl, Dl, Wr, Dr;     //数据域Node* left = nullptr, *right = nullptr;      //左右孩子指针
};int DFS(Node*&root, bool&f) {root = new Node();scanf("%d%d%d%d", &root->Wl, &root->Dl, &root->Wr, &root->Dr);if(root->Wl == 0)        //Wl是0root->Wl = DFS(root->left, f); //递归建立左子天平if(root->Wr == 0) root->Wr = DFS(root->right, f);if(root->Dl*root->Wl != root->Dr*root->Wr) //力矩不等f = false;return root->Wl + root->Wr;
}int main() {int n; scanf("%d", &n);for(int i = 0; i < n; i++) {Node *root = nullptr;bool f = true;DFS(root, f);printf("%s%s\n", i>0?"\n":"", f?"YES":"NO");}return 0;
}

解法三:隐式建树

#include<iostream>
using namespace std;bool solve(int &W) {        //此处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--) {cout << solve(W) ? "YES\n" : "NO\n";if(T) putchar('\n');}return 0;
}

择苦而安,择做而乐,虚拟终究不比真实精彩之万一

【三种解法】Not so Mobile UVA - 839_19行代码AC相关推荐

  1. [简单题]自定义取余(三种解法)C++实现

    题目链接: 点击打开原题链接 题目意思,就是标题意思. 第一种解法:(加法迭代)用加法来模拟这个(17行代码) int mod256WithoutMod(int number) {if (number ...

  2. jsp判断字符串相等_最长回文字符串三种解法

    先解释一下什么是回文字符串,比如说字符串"aba",无论是从先往后读取还是从后往前读取,结果都是一样的.当给定很长的字符串时,如何快速获取到最长的回文字符串,这也是大厂比较常见的算 ...

  3. 【三种解法】剑指 Offer 06. 从尾到头打印链表【附完整可运行代码】

    立志用最少的代码做最高效的表达 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回). 示例 1: 输入:head = [1,3,2] 输出:[2,3,1] 限制: 0 <= 链表 ...

  4. 【LeetCode】1. 盛最多水的容器:C#三种解法

    题目:https://leetcode-cn.com/problems/container-with-most-water/ 盛最多水的容器 难度:中等 给你 n 个非负整数 a1,a2,...,an ...

  5. 一只青蛙跳向三个台阶_青蛙跳台阶问题的三种解法

    题目:一只青蛙一次可以跳 1 级台阶,也可以跳 2 级.求该青蛙跳上一个 n 级的台阶总共有多少种跳法. 这道题还被 ITEye 放在了博文视点杯有奖答题活动里面. 我提供三种解法. 1.递归求解: ...

  6. 背包问题knapsack的三种解法(Python 和 C)

    最近研究了一下0-1背包问题,题目就不复述了,大家应该都知道的. 确切的说不是三种解法而是四种解法,下面我就一一写来. 0.枚举法 这种是最简单的一种做法,当然也是时间空间复杂度最大的方法,得到的肯定 ...

  7. 寻找两个有序数组的中位数(附上三种解法)

    目录 •写在前面 •题目 •解法一 •解法二 •解法三 •结束 •写在前面 这道题比较经典,我当时在做的时候,想出了两种解决方案,不过总感觉算法不够优美,所以找到了另一种我觉得非常精妙的解法,利用了K ...

  8. 两个有序数组求中位数的三种解法

    寻找两个有序数组的中位数(附上三种解法)_BoCong-Deng的博客-CSDN博客_两个有序数组求中位数

  9. Python实现阶乘的三种解法

    Python实现阶乘的三种解法 问题描述 输入一个正整数n,输出n!的值. 其中n!=123*-*n. 算法描述 n!可能很大,而计算机能表示的整数范围有限,需要使用高精度计算的方法.使用一个数组A来 ...

最新文章

  1. 专访阿里 iDST 语音组总监鄢志杰:智能语音交互从技术到产品,有哪些坑和细节要注意?
  2. python查看图像通道数(通过PIL)
  3. 大数据_MapperReduce_Hbase配置参数说明_以及部分源码说明---Hbase工作笔记0031
  4. 脑电数据预处理和后续处理(EEGLAB)
  5. 6.ring3-ImportREC重建输入表
  6. 打开JMeter报错:Could not reserve enough space for 1048576KB object heap
  7. Unity 制造moba英雄联盟战争迷雾2
  8. 【Qt开发】编译时报“undefined reference to“问题的解决方案
  9. 2021免费领取微软onedrive云盘1T空间
  10. NetBpm 示例:请假审批(6)
  11. python snmp
  12. 已知一个字符串,将字符串中的大写英文字母转变/转化成小写字母,小写字母转变为大写字母,并将转变后的字符串输出
  13. 有一种感觉叫清风细雨
  14. 关于Raphael开发过程中的一些总结
  15. 爱我所爱,行我所行,听从我心,无问西东
  16. lecture12-玻尔兹曼机和受限玻尔兹曼机
  17. 复旦大学高等代数考试命题的若干经验
  18. Android八门神器(一):OkHttp框架源码解析 1
  19. java tika pdf_TIKA - 提取PDF
  20. 利用外部程序对存储BIOS设置参数的CMOS RAM进行读取操作的可行性分析

热门文章

  1. 详解分布式一致性机制
  2. 比Redis快5倍的中间件,为啥这么快?
  3. MySQL主备复制原理、实现及异常处理
  4. Linux(CentOS)中常用软件安装,使用及异常——Zookeeper, Kafka
  5. 网络编程套接字(二)
  6. 【今晚七点半】:白板与开源
  7. LiveVideoStackCon 2022 上海站 专题抢先看
  8. 滴滴李先刚:语音识别在复杂场景的性能将显著提升
  9. PMP之项目整合管理之变更管理计划
  10. debian9 没有ipv4