zoukankan      html  css  js  c++  java
  • UVa839 Not so Mobile

     

    我的解法: 建树,递归判断

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    
    struct Node {
            Node() {
                    wl=wr=dl=dr=0;
                    l=r=0;
            }
            int wl;
            int dl;
            int wr;
            int dr;
            Node* l;
            Node* r;
    };
    
    Node* build()
    {
            int wl, wr, dl, dr;
            scanf("%d%d%d%d", &wl, &dl, &wr, &dr);
            Node* root=new Node;
            if(wl==0)
            {
                    Node* left=build();
                    wl=left->wl + left->wr;
                    root->l=left;
            }
            if(wr==0)
            {
                    Node* right=build();
                    wr=right->wl + right->wr;
                    root->r=right;
            }
    
            root->wl=wl;
            root->wr=wr;
            root->dl=dl;
            root->dr=dr;
            return root;
    }
    
    bool equilibrium(Node* root)
    {
            if(!root)
                    return true;
            bool el=equilibrium(root->l);
            bool er=equilibrium(root->r);
            if(el&&er)
            {
                    return (root->wl * root->dl == root->wr * root->dr);
            }
            else
            {
                    return false;
            }
    }
    
    
    
    int main()
    {
    #ifndef ONLINE_JUDGE
            freopen("./uva839.in", "r", stdin);
    #endif
            int T;
            scanf("%d", &T);
            while(T--) {
                    Node* root=build();
                    if(equilibrium(root))
                            printf("YES
    ");
                    else
                            printf("NO
    ");
    
                    if(T!=0)
                            printf("
    ");
            }
    
    
        return 0;
    }

    解答解法:

    // UVa839 Not so Mobile
    // Rujia Liu
    // 题意:输入一个树状天平,根据力矩相等原则判断是否平衡。采用递归方式输入,0表示中间结点
    // 算法:在“建树”时直接读入并判断,并且无须把树保存下来
    #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
    "; else cout << "NO
    ";
        if(T) cout << "
    ";
      }
      return 0;
    }
  • 相关阅读:
    js正则表达式中的问号使用技巧总结
    380. Insert Delete GetRandom O(1)
    34. Find First and Last Position of Element in Sorted Array
    162. Find Peak Element
    220. Contains Duplicate III
    269. Alien Dictionary
    18. 4Sum
    15. 3Sum
    224. Basic Calculator
    227. Basic Calculator II
  • 原文地址:https://www.cnblogs.com/cute/p/3642548.html
Copyright © 2011-2022 走看看