当前位置:网站首页>[exercise-5] (UVA 839) not so mobile (balance)
[exercise-5] (UVA 839) not so mobile (balance)
2022-07-06 15:56:00 【Flame car】
Translate :
Enter a tree scale , Judge whether it is balanced according to the principle of equal moment . The so-called equal moment , Namely W1D1 = W2D2, among W1 and W2 Are the weights of the left and right weights ,D For distance .
Recursion ( Preface ) Mode input : The format of each balance is W1 D1 W2 D2, When W1 or W2 by 0 when , It means that we should “ Weights ” It's actually a balance , Next, I will describe the scale . When W1=W2=0 when , I will first describe the left balance , Then there is the right balance .
recursive .
AC Code :
#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;
}
}
to x1 and x2 The assignment is 1 It's because on the last floor , He has no left-right balance , So the default is 1.
Because it is preordered recursion , So our input is first root, then left and then right . According to the introduction of the topic, you can also know , It must be a balanced binary tree .
At first, I thought I could not add w, It turned out ,w Must be added and participate in recursion , If not , Then only the bottom layer can complete the calculation 
Like in the picture above , Only 1 * 1 = 1 * 1 and 2 * 4 = 4 * 2 and 3* 2 = 6 * 1 It can be calculated if you don't add w Words .
After adding, the sum of the following two balls will be returned each time, so you can find out whether the two balances are balanced . And so on , Recursion completes .
边栏推荐
- Penetration test (1) -- necessary tools, navigation
- Learning records: serial communication and solutions to errors encountered
- Research Report on shell heater industry - market status analysis and development prospect forecast
- Research Report on market supply and demand and strategy of Chinese hospital cleaning chemicals industry
- Accounting regulations and professional ethics [2]
- 数据在内存中的存储&载入内存,让程序运行起来
- 0-1背包问题(一)
- D - Function(HDU - 6546)女生赛
- China exterior wall cladding (EWC) market trend report, technical dynamic innovation and market forecast
- 对iptables进行常规操作
猜你喜欢

Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs

Nodejs+vue网上鲜花店销售信息系统express+mysql

STM32 how to use stlink download program: light LED running light (Library version)

X-forwarded-for details, how to get the client IP

信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志

C语言学习笔记

C语言必背代码大全

【练习-7】Crossword Answers

渗透测试 ( 1 ) --- 必备 工具、导航

Matlab example: two expressions of step function
随机推荐
Research Report on market supply and demand and strategy of China's earth drilling industry
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’
Borg Maze (BFS+最小生成树)(解题报告)
Cost accounting [14]
【练习-10】 Unread Messages(未读消息)
Market trend report, technical innovation and market forecast of Chinese hospital respiratory humidification equipment
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
Learning record: Tim - Basic timer
Cost accounting [13]
Learning record: use STM32 external input interrupt
Research Report on shell heater industry - market status analysis and development prospect forecast
渗透测试 ( 1 ) --- 必备 工具、导航
Learning record: understand systick system timer and write delay function
Cost accounting [21]
Nodejs+vue网上鲜花店销售信息系统express+mysql
Opencv learning log 30 -- histogram equalization
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
Learning records: serial communication and solutions to errors encountered
程序员的你,有哪些炫技的代码写法?
Truck History