当前位置:网站首页>[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 .
边栏推荐
- 【练习-2】(Uva 712) S-Trees (S树)
- 渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
- Record of brushing questions with force deduction -- complete knapsack problem (I)
- Opencv learning log 32 edge extraction
- STM32 how to use stlink download program: light LED running light (Library version)
- China's peripheral catheter market trend report, technological innovation and market forecast
- Research Report of cylindrical grinder industry - market status analysis and development prospect forecast
- Web based photo digital printing website
- Gartner:关于零信任网络访问最佳实践的五个建议
- 数据在内存中的存储&载入内存,让程序运行起来
猜你喜欢
TCP的三次握手与四次挥手
[analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
Information security - Analysis of security orchestration automation and response (soar) technology
Learning record: understand systick system timer and write delay function
力扣刷题记录
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
基于web的照片数码冲印网站
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Determine the Photo Position
动态规划前路径问题优化方式
随机推荐
Penetration test (8) -- official document of burp Suite Pro
Path problem before dynamic planning
0-1背包问题(一)
程序员的你,有哪些炫技的代码写法?
X-Forwarded-For详解、如何获取到客户端IP
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Opencv learning log 33 Gaussian mean filtering
Cost accounting [16]
Cost accounting [24]
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
Market trend report, technical innovation and market forecast of Chinese hospital respiratory humidification equipment
China potato slicer market trend report, technical dynamic innovation and market forecast
Accounting regulations and professional ethics [4]
Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
1010 things that college students majoring in it must do before graduation
Learning record: Tim - Basic timer
滲透測試 ( 1 ) --- 必備 工具、導航
Cost accounting [13]
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
Hospital privacy screen Industry Research Report - market status analysis and development prospect forecast