UVA839——天平

2023-10-06 19:42
文章标签 uva839 天平

本文主要是介绍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 filefreopen("test.in", "r", stdin);freopen("test.out", "w", stdout);#endif // fileint T,W;cin>>T;while(T--){if(solve(W))cout<<"YES"<<endl;elsecout<<"NO"<<endl;if(T)cout<<endl;}return 0;
}


这篇关于UVA839——天平的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/153865

相关文章

背包问题(天平)——POJ 1837

对应POJ题目:点击打开链接 Balance Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit  Status  Practice  POJ 1837 Description Gigel has a strange "balance" and he wa

poj 1837 Balance(01背包 天平平衡)

题目大意: 有一个天平,天平左右两边各有若干个钩子,总共有C个钩子,有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数。 其中可以把天枰看做一个以x轴0点作为平衡点的横轴 输入: 2 4 //C 钩子数 与 G钩码数 -2 3 //负数:左边的钩子距离天平中央的距离;正数:右边的钩子距离天平中央的距离c[k] 3 4 5 8 //G个重物的质量w[i]

bootstrap实现天平效果

之前提到了,最近,孩子的幼儿园让家长体验“半日助教活动”,每个家长需要讲授15-20分钟的课程。作为一名程序员,实在没有能教的课程,只能做了一个小游戏,带着小朋友们熟悉数字。 在上一章博客中,笔者发布了九宫格中猫捉老鼠的小游戏源码,下面再把通过bootstrap实现天平效果的源码发布,供读者参考。 效果大致是这样的。通过前端代码生成一个简易的天平,天平两边分别随机生成一个数字,点击较大的数字天

3-2. 用天平找小球

三个球A、B、C,大小形状相同且其中有一个球与其他球重量不同。要求找出这个不一样的球。 输入格式: 输入在一行中给出3个正整数,顺序对应球A、B、C的重量。 输出格式: 在一行中输出唯一的那个不一样的球。 输入样例: 1 1 2 输出样例: C #include<stdio.h>int main(){int a,b,c;scanf("%d%d%d",&a,&b,&

例题6-9 天平(Not so Moblie,UVa 839)

原题链接:https://vjudge.net/problem/UVA-839 分类:树 备注:思维 前言:不得不说汝佳大大的代码十分巧妙,这次回顾还是没有写出那么好的代码。 代码如下: #include<cstdio>using namespace std;int T, balance;int dfs(){int wl, dl, wr, dr;scanf("%d%d%d%d", &w

【券商报告】21年1季度债券市场展望:摇摆的天平——附下载链接

来源 | 兴业证券 20年4季度:大类资产表现,从股商品强势债弱到资产普涨,永煤事件爆发是转折点,流动性悲观预期的改善是主要 推手。20年债市曲线:牛陡(1-4月)—熊平(5-11月中)—牛陡(11月中以来),流动性才是核心矛盾而非基本面。                 如需查看完整报告和报告下载或了解更多,公众号:参一江湖

在120枚外观相同的硬币中,有一枚是假币,并且已知假币与真币的重量不同,但不知道假币与真币相比较轻还是较重。可以通过一架天平来任意比较两组硬币,最坏情况下,能不能只比较5次就检测出这枚假币?

能 这道题目我想先通过另外一道题目引入我的方法: 在13枚外观相同的硬币中,有一枚是假币,并且已知假币与真币的重量不同,但不知道假币与真币相比较轻还是较重。可以通过一架天平来任意比较两组硬币,最坏情况下,能不能只比较3次就检测出这枚假币? 将13枚硬币分为三组 ABCD  EFGH  IJKLM 这里引入一个概念,每次天平倾斜方向称之为X方向、Y方向、和平衡 X方向不一定就是向左倾斜,

12硬币中有一个不知道轻重的假币,用天平将它找出来

问题1:假设有8个硬币,里面有一个硬币是假币,并且知道它是重了还是轻了(假设是轻了),现在给你一个天平,要求用最小次数将这个硬币找出来. 这时候可以用一种类似二分法的算法来找出这个假币.将左边4个和右边4个比较,因为知道硬币是轻了,所以很快就能确定那堆硬币里面有假币,这时候问题的规模由原来的8变成了4....然后对4个硬币也采用同样的办法...最终3次找出那个假硬币 问

zjut 1722 天平2

http://acm.zjut.edu.cn/ShowProblem.aspx?ShowID=1722 没想到在zjut Oj 上超内存一直把我判成是RE,无语……之后才发现,改过后就Ac了,不错的题目 #include <cstdio>#include <cstring>#include <iostream>#include <vector>#include <algorithm