Not so Mobile(二叉树递归输入同时建树){天平}

题意:

给出一个大天平,大天平中还有许多小天平,求出所有的天平是否平衡;平衡条件为wldl = wrdr;

题目

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
方案一:思维:可以构造一个二叉树,递归求出每个子树是否平衡,传递判断天平是否平衡1,0;
错误出现:1,若在调用函数bfs中了.l,r定义为全局变量(且在调用中对r,l赋值),则错误。
因为赋值时会覆盖掉上一次获取的r,l的值。
解决方案1,将其定义为局部变量,每一次调用,r,l不为同一值,利用递归,对r,l的值进行传递。
2,定义全局变量,但在主函数中赋初值,用递归传递值
(2) 参数传递的方式是引用传递
对形参的任何操作都能改变相应的数据

#include<iostream>
#include<string.h>
using namespace std;
int t;
int bfs(int &p)//p 相当于此分支目前的重量
{/*递归传递判断天平是否平衡1,0*/int a,u,b,v;cin>>a>>u>>b>>v;///输入数据的时候正好可以构建树,当输入数完成后就构建成了一颗完整的树int l=1,r=1;/**care 可能l和r最后也没能赋值,为了到达叶节点可以比较,故赋初值*/if(a==0)l=bfs(a);/**判断左子树上,叶节点是否平衡*/if(b==0)r=bfs(b);/**判断右子树上,叶节点是否平衡*/p=a+b;///当输入结束,回溯过程时,左右子树重量相加,获得判断节点的重量和if(l&&r&&(a*u==b*v)) return 1;///左边,右边子树重量相等,再比较此时它的重量else return 0;
}
int main()
{cin>>t;while(t--){int x;int flag=bfs(x);///x 相当于此分支目前的重量if(flag) cout<<"YES"<<endl;else cout<<"NO"<<endl;if(t)cout<<endl;}return 0;
}
/**
在C++中,参数传递的方式是“实虚结合”。
A按值传递(pass by value)  int x
B地址传递(pass by pointer)  int *x
C引用传递(pass by reference)  int &x
A调用函数本身不对实参进行操作,也就是说,即使形参的值在函数中发生了变化,实参的值也完全不会受
到影响,仍为调用前的值。
B地址传递与按值传递的不同在于,它把实参的存储地址传送给对应的形参,从而使得形参指针和实参指针
指向同一个地址。因此,被调用函数中对形参指针所指向的地址中内容的任何改变都会影响到实参。
C如果以引用为参数,可以使得对形参的任何操作都能改变相应的数据,引用传递方式是在函数定义时在形参前面加上引用运算符“&”。
*/

方案二
可以构造一个二叉树,递归求出每个子树是否平衡,并返回wl+wr作为父亲节点的w;

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int t,flag;
int dfs()
{int a=0,u,b=0,v;scanf("%d%d%d%d",&a,&u,&b,&v);if(a==0)a=dfs();if(b==0)b=dfs();if(a*u!=b*v)flag=1;return a+b;
}
int main()
{scanf("%d",&t);while(t--){flag=0;dfs();if(!flag)printf("YES\n");elseprintf("NO\n");if(t)printf("\n");}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/310085.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++类型转换基本语法

static_cast 用于内置的数据类型 还有具有继承关系的指针或者引用 代码如下: #include <iostream> using namespace std;class Building {}; class Animal{}; class Cat:public Animal{};int main() {int a 97;char c static_cast<char>(a);cout << c &l…

java arraylist 源代码_java中ArrayList的源代码是什么

展开全部package java.util;public class ArrayList extends AbstractListimplements List, RandomAccess, Cloneable, java.io.Serializable{private static final long serialVersionUID 8683452581122892189L;/*** The array buffer into which the elements of the ArrayL…

在阿里云函数计算上部署.NET Core 3.1

使用阿里云ECS或者其他常见的VPS服务部署应用的时候&#xff0c;需要手动配置环境&#xff0c;并且监测ECS的行为&#xff0c;做补丁之类的&#xff0c;搞得有点复杂。好在很多云厂商&#xff08;阿里云、Azure等&#xff09;提供了Serverless服务&#xff0c;借助于Serverless…

[PAT乙级]1023 组个最小数

给定数字 0-9 各若干个。你可以以任意顺序排列这些数字&#xff0c;但必须全部使用。目标是使得最后得到的数尽可能小&#xff08;注意 0 不能做首位&#xff09;。例如&#xff1a;给定两个 0&#xff0c;两个 1&#xff0c;三个 5&#xff0c;一个 8&#xff0c;我们得到的最…

C++中函数调用时的三种参数传递方式(x,*x,x)

先看三种方式实现函数中参数传递的实例程序 输入描述&#xff1a;两个待交换的整数程序输出&#xff1a;交换后得按值传递两个整数&#xff08;一&#xff09;按值传递&#xff1a;按值传递的过程为&#xff1a;首先计算出实参表达式的值&#xff0c;接着给对应的形参变量分配…

java 持续交付_【Java架构:持续交付】一篇文章搞掂:Jenkins

1.1、使用yum安装JDKa、检查系统是否有安装open-jdkrpm -qa |grep javarpm -qa |grep jdkrpm -qa |grep gcj如果没有输入信息表示没有安装。如果安装可以使用rpm -qa | grep java | xargs rpm -e --nodeps 批量卸载所有带有Java的文件 这句命令的关键字是javab、检索yum中包含…

抱歉,请不要把 “业务逻辑层” 理解为 “业务中台”

这是头哥侃码的第197篇原创在IAS2019中台架构峰会上&#xff0c;我曾与一位年轻帅气的技术小伙来了一番有趣的对话。因为和朋友有约&#xff0c;所以我在现场互动结束之后&#xff0c;就急匆匆地跟其他嘉宾打了声招呼&#xff0c;抱着笔记本冲出了会场。但没想到刚到电梯口&…

C++异常处理分析

C异常处理基本语法: 代码如下: #include <iostream> using namespace std;int divide(int x, int y) {if (y 0) throw y;return x / y; }void test01() {//试着去捕获异常try{divide(10, 0);}/*catch (int){cout << "除数为0!" << endl;} */catc…

java文件损坏_java – 损坏的文件处理

我想知道如果任何人有任何建议处理损坏的文件与Apache POI我试图打开一个文件&#xff0c;并收到此消息&#xff1a;Exception in thread "main" org.apache.poi.hssf.record.RecordInputStream$LeftoverDataException: Initialisation of record 0x1C left 2 bytes …

Harmonic Number (II) LightOJ - 1245(找规律?大数f(n)=n/1+n/2+n/3+......+n/n)

题意&#xff1a;让我们求f&#xff08;n&#xff09;n/1n/2n/3......n/n&#xff1b;同时注意n/i取整&#xff1b; 思路&#xff1a;首先我们先看数据的范围&#xff0c;n (1 ≤ n < 2 31)&#xff0c;数据范围太大&#xff0c;如果我们按 照题目中的代码直接暴力肯定超时…

陌陌的 Service Mesh 探索与实践

Service Mesh Virtual Meetup 是 ServiceMesher 社区和 CNCF 联合主办的线上系列直播。本期为 Service Mesh Virtual Meetup#1 &#xff0c;邀请了四位来自不同公司的嘉宾&#xff0c;从不同角度展开了 Service Mesh 的应用实践分享&#xff0c;分享涵盖来自陌陌和百度的 Servi…

C标准输入流

标准输入流对象cin&#xff0c;重点掌握的函数: cin.get()//一次只能读取一个字符 cin.get(一次参数)//读一个字符 cin.get(两个字符)//可以读字符串 cin.getline() cin.ignore() cin.peek() cin.putback() 标准输入流cin.get() 代码如下: #include <iostream> using n…

Harmonic Number(欧拉公式或技巧打表)LightOJ - 1234(求调和级数的和)

题意&#xff1a;求f(n)1/11/21/31/4…1/n (1 ≤ n ≤ 108).&#xff0c;精确到10-8 (原题在文末&#xff09; 知识点&#xff1a;调和级数(即f(n))至今没有一个完全正确的公式&#xff0c;但欧拉给出过一个近似公式&#xff1a;(n很大时) f(n)≈ln(n)C1/2*n 欧拉常数值&…

教你配置windows上的windbg,linux上的lldb,打入clr内部这一篇就够了

一&#xff1a;背景1. 讲故事前几天公众号里有位兄弟看了几篇文章之后&#xff0c;也准备用windbg试试看&#xff0c;结果这一配就花了好几天&#xff0c;(づ╥﹏╥)づ&#xff0c;我想也有很多跃跃欲试的朋友在配置的时候肯定会遇到这样和那样的问题&#xff0c;所以我觉得有必…

vue动态跟新layui的select_vue+layui实现select动态加载后台数据的例子

vuelayui实现select动态加载后台数据的例子发布时间&#xff1a;2020-09-18 22:51:36来源&#xff1a;脚本之家阅读&#xff1a;85作者&#xff1a;qq_26814945刚开始由于layui form渲染与vue渲染有时间差 有时会导致 select里面是空白的后来就想办法 等vue数据渲染完 再渲染la…

Rochambeau POJ - 2912 (枚举和加权并查集+路径压缩)找唯一裁判

题意&#xff1a;有n个人玩石头剪刀布&#xff0c;有且只有一个裁判。除了裁判每个人的出拳形式都是一样的。 a<b表示b打败a&#xff0c;ab表示a和b出拳一样&#xff0c;平手。a>b表示a打败b。 给出m个回合的游戏结果&#xff0c;问能否判断出谁是裁判&#xff1f;如果能…

C标准输出流

标准输入流对象cin&#xff0c;重点掌握的函数: cout.flush()//刷新缓冲区 cout.put()//向缓冲区写字符 cout.write()//二进制流的输出 cout.width()//输出格式控制 cout.fill() cout.set(标记) cout.flush() 代码如下: #include <iostream> using namespace std;void…

Autofac在.NET Core 中的使用

前言Autofac 是一款.NET IoC 容器 . 它管理类之间的依赖关系, 从而使应用在规模及复杂性增长的情况下依然可以轻易地修改 。.NET CORE 中也内置了依赖注入&#xff0c;但是有些情况下需要用到Autofac去进行依赖注入&#xff0c;Autofac支持的所有注入方式以外&#xff0c;还支持…

Aladdin and the Flying Carpet (素数打表+正整数的唯一分解定理,找因数对)

题目大意&#xff1a;给两个数a&#xff0c;b&#xff0c;求满足c*da且c>b且d>b的c,d二元组对数&#xff0c;(c,d)和(d,c)属于同一种情况 题目分析&#xff1a;根据唯一分解定理先将a唯一分解&#xff0c;则a的所有正约数的个数为ans (1 a1) * (1 a2) *...(1 an) 因为…

C++文本文件操作和二进制文件读写

文本文件操作: 代码如下: #include <iostream> #include <fstream> using namespace std;void test01() {const char *fileName "C:\\Users\\Tom\\Desktop\\hhh.txt";//ifstream ism(fileName, ios::in);//只读方式打开文件ifstream ism;ism.open(file…