天平(Not so Mobile)

2023-05-16

Not so Mobile
Time Limit:3000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu

Submit Status

Description

Download as PDF

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.

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 isWl×Dl = Wr×Dr whereDl is the left distance, Dr is the right distance, Wl is the left weight andWr 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.

Input 

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.


The input is composed of several lines, each containing 4 integers separated by a single space. The 4 integers represent the distances of each object to the fulcrum and their weights, in the format:Wl Dl Wr Dr

If Wl or Wr is zero then there is a sub-mobile hanging from that end and the following lines define the the sub-mobile. In this case we compute the weight of the sub-mobile as the sum of weights of all its objects, disregarding the weight of the wires and strings. If bothWl and Wr are zero then the following lines define two sub-mobiles: first the left then the right one.

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.


Write `YES' if the mobile is in equilibrium, write `NO' otherwise.

Sample Input 


1

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

Sample Output 


YES
  
【分析】

       采用递归方式输入。

用C++语言编写程序,代码如下:

#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;
}

用java语言编写程序,代码如下:

(在测试过程中,曾在递归方法中加入一句输出语句,在提交过程中,忘记对该语句的删除,导致程序时间超限。删除该语句之后就答案正确了)

以下有两个方法可以使得在程序的递归方法中可以传递引用:

方法1:

import java.io.BufferedInputStream;
import java.util.Scanner;

//要注意的是java中并没有所谓的引用传递,当我们传递对象时,方法接受到的对象只不过是指向相同地址值的引用摆了,当我们改变该对象的指向时,并不会影响原来的地址值。
//当我们只是改变对象本身,而没有改变对象的属性时,原对象的内容不变。(这也是为什么下面用Integer代替int改为传递对象不行的原因,Integer对象在方法中被
//重新赋值,也不会影响调用者的Integer对象)
public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(new BufferedInputStream(System.in));
		int[] W = new int[1];
		int T = input.nextInt();
		for(int i = 0; i < T; i++) {
			if(i != 0)
				System.out.println();
			
			if(solve(input, W))
				System.out.println("YES");
			else
				System.out.println("NO");
		}
	}
	
	public static boolean solve(Scanner input, int[] W) {
		int[] W1 = new int[1]; int[] W2 = new int[1];
		int D1,D2;
		boolean b1 = true, b2 = true;
		W1[0] = input.nextInt(); D1 = input.nextInt();
		W2[0] = input.nextInt(); D2 = input.nextInt();
		if(W1[0] == 0) b1 = solve(input, W1);
		if(W2[0] == 0) b2 = solve(input, W2);
		W[0] = W1[0] + W2[0];
		//System.out.println(W[0]);
		return b1 && b2 && (W1[0] * D1 == W2[0] * D2);
	}
}

方法2:

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(new BufferedInputStream(System.in));
		int T = input.nextInt();
		Weight w = new Weight();
		for(int i = 0; i < T; i++) {
			if(i != 0)
				System.out.println();
			
			if(solve(input, w))
				System.out.println("YES");
			else
				System.out.println("NO");
		}
	}
	
	static class Weight {
		int w;
		public Weight(int w) {
			this. w = w;
		}
		public Weight() {
			super();
		}
	}
	
	public static boolean solve(Scanner input, Weight W) {
		Weight W1 = new Weight();
		Weight W2 = new Weight();
		int D1, D2;
		boolean b1 = true, b2 = true;
		W1.w = input.nextInt(); D1 = input.nextInt();
		W2.w = input.nextInt(); D2 = input.nextInt();
		if(W1.w == 0) b1 = solve(input, W1);
		if(W2.w == 0) b2 = solve(input, W2);
		W.w = W1.w + W2.w;
		//System.out.println(W.w);
		return b1 && b2 && (W1.w * D1 == W2.w * D2);
	}
}





本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

天平(Not so Mobile) 的相关文章

  • 由于屏幕方向变化而调整大小后,如何获取元素的新尺寸?

    我正在开发一个移动网络应用程序 在我的页面中我有一个div元素的宽度设置为 100 我需要设置这个的高度div以便高度对于设定的纵横比是正确的 例如 如果屏幕尺寸为 300 像素宽且比例为 3 2 我的脚本应该获取屏幕的宽度div 此时应为
  • iPhone 4 移动网络应用程序像素缩放问题

    我无法让我的移动 Web 应用程序在 iPhone 4 上正确呈现 根据 Wikipedia iPhone 4 的像素为 960 宽 x 680 高度 而其他 iPhone 的像素为 480 宽 x 340 像素 在我当前的构建中 图像和
  • 通过移动地址栏时,视差背景图像在移动设备上改变大小

    一周以来 我的视差元素一直在碰壁 寻求帮助对我来说是最后的手段 我已经在各种论坛上寻找这些问题的解决方案 但我尝试过的都没有效果 only在移动设备上 chrome 如果您按住触摸屏并继续向下滚动 则在初始屏幕图像之后 会出现一个白条 屏幕
  • 根据外形尺寸更改入口点类别

    如果用户从移动 Web 浏览器或桌面 Web 浏览器访问 我希望在我的 GWT 应用程序中加载不同的用户界面 我想知道如何编辑我的 Application gwt xml 文件 更改根据外形规格加载哪个入口点类 我认为这可能是这样的 但我只
  • 更改语言 Flutter 的按钮

    我正在 Flutter 中构建一个应用程序 到目前为止 我正在使用 JSON 国际化 其中应用程序的语言基于用户手机中默认的语言 它工作得很好 但我想给用户有机会在不更改手机系统语言设置的情况下更改语言 只需单击按钮 然后应用程序即可更改语
  • jQuery:单击外部元素以“关闭”使用toggleClass 出现的菜单

    我已经构建了一些导航 针对移动网络 它使用 jQuery 中的toggleClass 方法来隐藏和显示菜单 单击 MENU 图标 按钮可在菜单 div 上打开和关闭类 active 显示 隐藏 我一直在拼命寻找一种通过单击菜单外部 页面上的
  • jQuery Mobile 数据过滤器,以防没有结果

    我目前正在探索 jQuery Mobile 以开发带有订单跟踪信息的移动版仪表板 计划是使用一个包含所有订单的简单无序列表 人们可以单击他们想了解更多信息的链接 由于此列表可能会变得相当大 因此拥有过滤功能非常好 使用 jQuery Mob
  • iPhone Mobile Safari:强制键盘打开

    这是一个 HTML CSS JS jQuery iPad 应用程序 我有一个按钮 可以向下滑动输入表单 我想让用户将注意力集中在输入上 然后启动键盘 这是我正在处理的 但不起作用 myFormField focus 这确实集中了输入 但无法
  • 有没有办法在 couchdb 视图中发出附件数据

    我发现在网站上显示图像数据时使用 CouchDB 附件非常有用 然而 当我将数据库复制到移动环境时 运行视图然后必须循环浏览文档才能访问其附件的效率非常低 在 iOS Android 平台上 将数据存储为常规 BLOBS 似乎更有效 并且只
  • 在移动浏览器或 PhoneGap 应用程序之间进行检测

    是否可以使用 JavaScript 检测用户是否通过浏览器或应用程序进行访问 我正在通过网页和 PhoneGap 应用程序开发适用于多个移动操作系统的混合应用程序 目标是 独立于部署目标使用相同的代码 仅当用户代理是应用程序时添加 Phon
  • HTML5 网站在手机屏幕关闭时运行?

    基本上 我的问题与这个话题 https stackoverflow com questions 15465242 html5 mobile app running while phone screen is off但现在已经是 2018 年
  • ASP.NET MVC4 中自定义移动显示模式和桌面模式之间的切换

    我想创建切换到完整站点链接并切换到移动链接 我不想通过使用会话变量强制它转到它们中的任何一个 我想知道是否可以使用 ViewSwitcher Controller 自动执行此操作 这是我用于自定义移动显示模式的内容 public class
  • Windows 10:如何防止切换或关闭应用程序

    我正在为 Windows 10 设备开发一个应用程序 该设备将在公共场所暴露给用户 并且其硬件按钮将无法访问 如何防止用户使用触摸屏手势关闭我的应用程序 使用分配的访问权限设置您的应用程序 http www winbeta org news
  • IO 和 Android 之间发送数据? (字节数组)

    我正在 Android 中开发一个网络应用程序 它应该能够与 IO 应用程序进行通信 我正在使用 Appwarps 多人游戏后端 并且有一个发送和接收数据的功能 该函数接受一个字节数组 所以最初我认为我可以将一个 消息 对象序列化为一个字节
  • 通过 C# 的 Symbian

    是否可以使用 C 为 Symbian 构建程序 答案是肯定的 如果您使用 Net60 应用程序https www red Fivelabs com default aspx https www redfivelabs com default
  • cocos2D 或 IwGame

    我开始为 Android 开发游戏应用程序 但随着我的进步 我决定切换到跨平台环境 性能非常重要 因为后台有一些复杂的音频处理 经过几天研究该主题后 我得出的结论是 最成功的选择是 Marmalade SDK 现在我需要决定是使用IwGam
  • 如何检测 Facebook 应用内浏览器?

    您有过 Facebook 应用内浏览器检测的经验吗 用户代理的核心区别是什么 我不想知道它是否是唯一的移动 ios chrome 我需要知道用户代理是否特定于Facebook 应用内浏览器 您可以在用户代理中检查 FBAN FBAV 检查此
  • 从当前位置出发的移动网络行车路线

    我正在构建一个网站的移动版本 试图通过一键式链接来启动 Google 地图 并提供从用户当前位置到企业的行车路线 我让它在 iPhone 上运行良好 但在 Android 上测试时 它会查看 Current 20Location 并尝试查找
  • 位置特征检测:固定

    我正在尝试找到一个脚本来检测设备是否放置position fixed元素相对于视口而不是整个文档 目前 标准桌面浏览器和 Mobile Safari 适用于 iOS 5 都是这样做的 而 Android 设备则相对于整个文档放置固定元素 我
  • React Native 的最佳广告中介(AdMob 除外)[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在将广告放入我的 React Native 应用程序中 并试图找到最好的广告中介平台来使用 我正在

随机推荐

  • 古老的密码(Ancient Cipher)

    Ancient Cipher Time limit 3 000 seconds Ancient Roman empire had a strong governmentsystem with various departments incl
  • Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    原文转载自 sunny2038 的CSDN博客文章 原文地址 最近在看Java xff0c 在编译写书上一个例子时 xff0c 由于书上的代码只有一部分 xff0c 于是就自己补了一个内部类 结果编译时出现 xff1a No enclosi
  • 瑞星微开发工具下载镜像的配置方法?

    如何根据 parameter txt 建立配置表 xff1f 首先我们来看下 parameter txt 的内容 xff1a CMDLINE mtdparts 61 rk29xxnand 0x00002000 64 0x00004000 u
  • 特别困的学生(Extraordinarily Tired Students)

    Extraordinarily Tired Students Time limit 3 000 seconds When a student is too tired he can 39 t help sleeping in class e
  • 骰子涂色(Cube painting)

    Cube painting We have a machine for painting cubes It is supplied with three different colors blue red and green Each fa
  • 盒子(Box)

    Box Time limit 3 000 seconds Ivan works at a factory that produces heavy machinery He hasa simple job he knocks up woode
  • 循环小数(Repeating Decimals)

    Repeating Decimals Time limit 3 000 seconds Repeating Decimals The decimal expansion of the fraction 1 33 is wherethe is
  • 反片语(Ananagrams)

    反片语 Ananagrams 输入一些单词 xff0c 找出所有满足如下条件的单词 xff1a 该单词不能通过字母重排 xff0c 得到输入文本中的另外一个单词 在判断是否满足条件时 xff0c 字母不分大小写 xff0c 但在输出时应保留
  • 集合栈计算机(The SetStack Computer)

    The SetStack Computer Time limit 3 000 seconds 题目是这样的 xff1a 有一个专门为了集合运算而设计的 集合栈 计算机 该机器有一个初始为空的栈 xff0c 并且支持以下操作 xff1a PU
  • 代码对齐(Alignment of Code)

    Alignment of Code Time Limit 4000 2000 MS Java Others Memory Limit 32768 32768 K Java Others Total Submission s 958 Acce
  • Ducci序列(Ducci Sequence)

    Ducci Sequence Time limit 3 000 seconds A Ducci sequence is a sequence of n tuples of integers Given an n tuple of integ
  • 卡片游戏(Throwing cards away I)

    Problem B Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at
  • 交换学生(Foreign Exchange)

    Problem E Foreign Exchange Input standard input Output standard output Time Limit 1 second Your non profit organization
  • CAN通信物理容错测试checklist

    CAN通信物理容错测试checklist 工作项目中 xff0c 我们有时有些产品CAN总线功能 xff0c 一般情况下我们必须要满足以下几种状况的测试项才算符合要求 一 CAN H与CAN L短路 判定标准 xff1a 短接故障发生后 x
  • 对称轴(Symmetry)

    Symmetry Time limit 3 000 seconds The figure shown on the left is left right symmetric as it is possible to fold the she
  • 打印队列(Printer Queue)

    Printer Queue Time limit 3 000 seconds 分析 首先记录所求时间它在队列中的位置 xff0c 用一个队列存储这些任务的优先级 xff0c 同时也创建一个队列存储对应任务一开始的位置 xff0c 那么当我们
  • 更新字典(Updating a Dictionary)

    Updating a Dictionary Time Limit 1000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description In th
  • 铁轨(Rails)

    Rails Time limit 3 000 seconds Rails There is a famous railway station in PopPush City Country there is incredibly hilly
  • 矩阵链乘(Matrix Chain Multiplication)

    Matrix Chain Multiplication Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description
  • 天平(Not so Mobile)

    Not so Mobile Time Limit 3000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description Before being