当前位置:网站首页>【练习-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的话。
添加了之后每次会返回下面两个球的和也就可以求两个天平是否平衡了。以此类推,递归完成。
边栏推荐
- 信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
- 信息安全-安全编排自动化与响应 (SOAR) 技术解析
- 编程到底难在哪里?
- Research Report on market supply and demand and strategy of China's medical chair industry
- Opencv learning log 13 corrosion, expansion, opening and closing operations
- F - Birthday Cake(山东省赛)
- Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
- Market trend report, technical innovation and market forecast of geosynthetic clay liner in China
- Learning record: USART serial communication
- 0-1 knapsack problem (I)
猜你喜欢
Determine the Photo Position
ucorelab4
UCORE Lab 1 system software startup process
Stm32 dossiers d'apprentissage: saisie des applications
用C语言写网页游戏
Es6---es6 content details
ucore lab5
STM32 how to use stlink download program: light LED running light (Library version)
C语言是低级和高级的分水岭
洛谷P1102 A-B数对(二分,map,双指针)
随机推荐
学习记录:STM32F103 时钟系统概述工作原理
Path problem before dynamic planning
动态规划前路径问题
nodejs爬虫
Eslint--- error: newline required at end of file but not found (EOL last) solution
Opencv learning log 15 count the number of solder joints and output
China earth moving machinery market trend report, technical dynamic innovation and market forecast
Interesting drink
Opencv learning log 32 edge extraction
China potato slicer market trend report, technical dynamic innovation and market forecast
Research Report on market supply and demand and strategy of China's earth drilling industry
Ball Dropping
通俗地理解什么是编程语言
Learning records: serial communication and solutions to errors encountered
信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
JS --- JS function and scope (II)
Hospital privacy screen Industry Research Report - market status analysis and development prospect forecast
Opencv learning log 18 Canny operator
Market trend report, technical innovation and market forecast of lip care products in China and Indonesia
数据在内存中的存储&载入内存,让程序运行起来