当前位置:网站首页>[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 .
边栏推荐
- 0-1 knapsack problem (I)
- 渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
- Opencv learning log 12 binarization of Otsu method
- Cost accounting [22]
- Printing quality inspection and verification system Industry Research Report - market status analysis and development prospect forecast
- 入门C语言基础问答
- TCP的三次握手与四次挥手
- CS zero foundation introductory learning record
- Learning record: USART serial communication
- Cost accounting [13]
猜你喜欢
X-forwarded-for details, how to get the client IP
Information security - threat detection - detailed design of NAT log access threat detection platform
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
Gartner: five suggestions on best practices for zero trust network access
C语言学习笔记
毕业才知道IT专业大学生毕业前必做的1010件事
Learning record: understand systick system timer and write delay function
Information security - threat detection engine - common rule engine base performance comparison
MATLAB综合练习:信号与系统中的应用
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
随机推荐
Report on the market trend, technological innovation and market forecast of printing and decorative paper in China
Matlab example: two expressions of step function
Cost accounting [24]
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
动态规划前路径问题优化方式
Nodejs+vue网上鲜花店销售信息系统express+mysql
Opencv learning log 33 Gaussian mean filtering
力扣刷题记录
Cost accounting [18]
Penetration test (7) -- vulnerability scanning tool Nessus
Gartner:关于零信任网络访问最佳实践的五个建议
最全编程语言在线 API 文档
Cost accounting [13]
差分(一维,二维,三维) 蓝桥杯三体攻击
Cost accounting [22]
STM32如何使用STLINK下载程序:点亮LED跑马灯(库版本)
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
China's PCB connector market trend report, technological innovation and market forecast
Cost accounting [19]