UVA839

来源:互联网 发布:知乎有趣的话题 编辑:程序博客网 时间:2024/06/15 05:42

Description&Input&Output

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=10&page=show_problem&problem=780

Explanation

输入一个树状天平,根据力矩想等原则判断其是否平衡,力矩相等就是WiDi = WjDj,其中Wi和Wj分别为两边砝码的重量,D为距离
天平平衡的条件首先其子天平平衡,然后天平左右力矩相等,所以要递归判断其子天平

Code

#include <iostream>using namespace std;//输入一个子天平,返回子天平是否平衡,参数W修改为子天平的总重量bool solve(int &W){    int W1, D1, W2, D2;    bool b1 = true, b2 = true;    cin >> W1 >> D1 >> W2 >> D2;    if(!W1) b1 = solve(W1);    if(!W2) b2 = solve(W2);    W = W1 + W2;    return b1 && b2 && (W1 * D1 == W2 * D2);}int main(){    int T,W;    cin >> T;    while(T--){        if(solve(W)) cout << "YES" << endl;        else cout << "NO" << endl;        if(T) cout << endl;        //每两个样例间有一个空行    }    return 0;}
原创粉丝点击