UVa 839 -- Not so Mobile(树的递归输入)

UVa 839 Not so Mobile(树的递归输入)

  判断一个树状天平是否平衡,每个测试样例每行4个数 wl,dl,wr,dr,当wl*dl=wr*dr时,视为这个天平平衡,当wl或wr等于0是,下一行将是一个子天平,如果子天平平衡,wl为子天平的wl+wr ,否则整个天平不平衡

 1 #include<iostream>
 2 using namespace std;
 3 bool solve(int &w)
 4 {
 5     int wl,dl,wr,dr;
 6     cin>>wl>>dl>>wr>>dr;
 7     bool b1 = true,b2=true;
 8     if(wl == 0) b1 = solve(wl);///进入左子天平
 9     if(wr == 0) b2 = solve(wr);///进入右子天平
10     w = wl+wr;
11     return b1 && b2 &&(wl*dl == wr*dr);
12 }
13 
14 int main()
15 {
16     int n;
17    cin>>n;
18         while(n--)
19         {
20             int weight;
21             bool s = solve(weight);
22             if(s) cout<<"YES"<<endl;
23             else cout<<"NO"<<endl;
24             if(n) cout<<endl;
25         }
26     return 0;
27 }

 

posted @ 2018-02-15 11:51  卉卉卉大爷  阅读(160)  评论(0编辑  收藏  举报