• 题解【UVA839】天平 Not so Mobile


    Description

    Input

    Output

    Examples

    Input

    1
    0 2 0 4
    0 3 0 1
    1 1 1 1
    2 4 4 2
    1 6 3 2
    

    Output

    YES
    

    Translation

    输入一个树状天平,根据力矩相等原则判断是否平衡。

    采用递归(先序)方式输入:每个天平格式为(W_l,D_l,W_r,D_r),当(W_l)(W_r)(0)时,表示该“砝码”实际是一个子天平,接下来会描述这个子天平。当(W_l=W_r=0)时,会先描述左子天平,然后是右子天平。

    其正确输出为(YES),否则输出(NO)

    Solution

    这道题目的输入就采取了递归方式定义,所以编写一个递归函数进行输入比较自然。实际上,在输入的过程中就可以完成判断。由于使用了引用传值,因此,代码很精简。

    本题极为重要,是递归的好题。

    Code

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <cctype>
    
    using namespace std;
    
    inline int gi()
    {
        int f = 1, x = 0; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
        while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
        return f * x;
    }
    
    int t, y;//t为询问总数,y为子天平的总重量
    
    bool pd(int &x)//输入一个子天平,返回子天平是否平衡,参数x为当前子天平的总重量
    {
        int a, b, c, d;//输入顺序同题目翻译
        bool f = true, fl = true;//判断有没有子天平
        a = gi(), b = gi(), c = gi(), d = gi();//输入
        if (!a) f = pd(a);//左边有子天平
        if (!c) fl = pd(c);//右边有子天平
        x = a + c;//修改总重量
        return (f && fl && (a * b == c * d));//进行判断
    }
    
    int main()//进入主函数
    {
        t = gi();//输入数据组数
        while (t--)
        {
            if (pd(y)) puts("YES");//如果合法就输出YES
            else puts("NO");//否则输出NO
            if (t) puts("");//每组数据用空行隔开,最后一组数据后不要输出空行
        }
        return 0;//结束主函数
    }
    
  • 相关阅读:
    融资担保公司
    典当公司
    保险代理、经纪公司互联网保险
    财产、人身、养老保险公司
    105家基金子公司
    LogStash Download
    cmd使用管理员权限运行,启动路径不是当前目录
    Module controller in JMeter
    Elasticsearch-->Get Started--> Exploring Your Data
    What are the differences between Flyweight and Object Pool patterns?
  • 原文地址:https://www.cnblogs.com/xsl19/p/11104958.html
Copyright © 2020-2023  润新知