UVA839——天平

描述:

Before being an ubiquous communications gadget, a mobile was just a structure made of strings and wires suspending colourfull things. This kind of mobile is usually found hanging over cradles of small babies.

\epsfbox{p839a.eps}

The figure illustrates a simple mobile. It is just a wire, suspended by a string, with an object on each side. It can also be seen as a kind of lever with the fulcrum on the point where the string ties the wire. From the lever principle we know that to balance a simple mobile the product of the weight of the objects by their distance to the fulcrum must be equal. That is Wl×Dl = Wr×Dr where Dl is the left distance, Dr is the right distance, Wl is the left weight and Wr is the right weight.


In a more complex mobile the object may be replaced by a sub-mobile, as shown in the next figure. In this case it is not so straightforward to check if the mobile is balanced so we need you to write a program that, given a description of a mobile as input, checks whether the mobile is in equilibrium or not.

\epsfbox{p839b.eps}
分析:
简而言之,输入一个天平系统,从上到下输入,如果该位置有子天平,则按照由左到右的顺序输入,判断该系统是否平衡(即左重量*左力臂=右重量*右力臂)
该题输入采用了递归定义的方式,因此题解用递归写也很方便,此处使用传值引用,代码可以写的很简洁。
P.S该题提交的时候遇到了点小问题,到现在没搞明白,疑点已经写在注释里了,求高人赐教。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>

//#define file
#define PI 3.1415926
#define MAX(A,B) ((A)>(B)?(A):(B))
#define ll long long
#define fordo(A,B,C) for(int (A)=(B);(A)<=(C);(A)++)
using namespace std;

bool solve(int& W)
{
    int W1,D1,W2,D2;
    bool b1=true;
    bool b2=true;
    cin>>W1>>D1>>W2>>D2;
    if(!W1)//这里很奇怪,当我用W1==0时VJ就会报WA,只有!W1这种表示法可以过,貌似这两者没区别吧……
        b1=solve(W1);//当存在子天平,求解。
    if(!W2)
        b2=solve(W2);
    W=W1+W2;//由子天平重量计算该天平重量
    return b1&&b2&&(W1*D1==W2*D2);
    //递归求解:当该托盘的子天平都平衡,并且该天平平衡,则返回该天平平衡。
}

int main()
{
    #ifdef file
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
    #endif // file
    int T,W;
    cin>>T;
    while(T--)
    {
        if(solve(W))
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
        if(T)
            cout<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值