当前位置:网站首页>【练习-5】(Uva 839)Not so Mobile(天平)
【练习-5】(Uva 839)Not so Mobile(天平)
2022-07-06 09:26:00 【火焰车】
翻译一下:
输入一个树状天平,根据力矩相等原则判断是否平衡。所谓力矩相等,就是W1D1 = W2D2,其中W1和W2分别为左右两边砝码的重量,D为距离。
采用递归(先序)方式输入:每个天平的格式为W1 D1 W2 D2,当W1 或 W2 为0时,表示该“砝码”实际是一个天平,接下来会描述这个天平。当W1=W2=0时,会先描述左子天平,然后是右子天平。
递归。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const ll mod = 1e9+7;
int solve(int &w)
{
int w1,d1,w2,d2;
int x1=1,x2=1;
cin>>w1>>d1>>w2>>d2;
if(!w1) x1 = solve(w1);
if(!w2) x2 = solve(w2);
w = w1+w2;
return x1 && x2 && (w1 * d1 == w2 * d2);
}
int main()
{
int n,w;
cin>>n;
while(n--)
{
if(solve(w))
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
cout<<endl;
}
}
给x1 和 x2赋值为1是因为到了最后一层,他是没有左右子天平的,所以默认是1。
由于是先序递归,所以我们的输入也是先根再左再右。根据题目的介绍也可以知道,必然是个平衡二叉树。
最一开始我以为可以不加w,后来发现,w是必须要加的而且参与递归,如果不加,那么只有最下面的一层可以完成计算
比如在上面的图中,只有1 * 1 = 1 * 1和 2 * 4 = 4 * 2 和 3* 2 = 6 * 1 可以被计算出来如果不加w的话。
添加了之后每次会返回下面两个球的和也就可以求两个天平是否平衡了。以此类推,递归完成。
边栏推荐
- Research Report on market supply and demand and strategy of China's land incineration plant industry
- Opencv learning log 18 Canny operator
- LeetCode#36. Effective Sudoku
- mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
- Research Report of peripheral venous catheter (pivc) industry - market status analysis and development prospect prediction
- Record of brushing questions with force deduction -- complete knapsack problem (I)
- 对iptables进行常规操作
- 信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
- 0-1背包问题(一)
- 力扣刷题记录
猜你喜欢

Nodejs+vue网上鲜花店销售信息系统express+mysql
![mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’](/img/e6/f4a696179282fe1f4193410c5a493a.png)
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’

Gartner:关于零信任网络访问最佳实践的五个建议

Optimization method of path problem before dynamic planning

学习记录:使用STM32F1看门狗

Matlab comprehensive exercise: application in signal and system

基于web的照片数码冲印网站

STM32學習記錄:輸入捕獲應用

毕业才知道IT专业大学生毕业前必做的1010件事

Ball Dropping
随机推荐
Cost accounting [14]
Record of force deduction and question brushing
HDU - 6024 Building Shops(女生赛)
X-Forwarded-For详解、如何获取到客户端IP
JS --- JS function and scope (II)
China exterior wall cladding (EWC) market trend report, technical dynamic innovation and market forecast
Learning record: STM32F103 clock system overview working principle
ucore lab 6
Cost accounting [18]
想应聘程序员,您的简历就该这样写【精华总结】
FSM and I2C experiment report
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
Learning records: serial communication and solutions to errors encountered
Research Report on market supply and demand and strategy of Chinese graphic screen printing equipment industry
VS2019初步使用
Report on the market trend, technological innovation and market forecast of printing and decorative paper in China
Research Report on printed circuit board (PCB) connector industry - market status analysis and development prospect forecast
JS --- detailed explanation of JS DOM (IV)
Learning record: use STM32 external input interrupt
Cost accounting [21]