题目链接
题意
输入一个树状天平,每一个天平输入“左质量”、“左力臂”、“右质量”、“右力臂”
当力臂为0的时候说明这边是一个子天平,询问是否能够平衡
解决
- 递归建树
#include<bits/stdc++.h>
using namespace std;
bool ok;
int tree()
{
int Wl,Dl,Wr,Dr;
scanf("%d%d%d%d",&Wl,&Dl,&Wr,&Dr);
if(!Wl) Wl=tree(); //重量为0的时候输入左子天平,返回左子天平的总重量
if(!Wr) Wr=tree(); //同上
if(Wl*Dl!=Wr*Dr) ok=false;
return Wl+Wr;
}
int main()
{
int cases;
scanf("%d",&cases);
while(cases--)
{
ok=true;
tree();
if(ok) puts("YES");
else puts("NO");
if(cases) puts("");
}
}