天平(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) 的相关文章

  • 在 Android 上从网络打印

    好吧 那么有谁知道如何在不使用第 3 方软件的情况下直接从网络打印 Android 设备 不是图片 的解决方案吗 因此 假设用户将通过 Android 设备使用基于 Web 的应用程序 一旦他们完成了问题 他们将需要打印该问题 我知道 An
  • JQuery Mobile 加载更多选项

    知道如何在 JQueryMobile 中实现加载更多选项 在我的应用程序中 我需要提取大量数据 并希望加载前 20 条数据 并让用户通过单击 Listview 最后一行上的 加载更多 数据选项来加载更多数据 这应该有效 loadmore l
  • 不良状态:平台不允许不安全的 HTTP:[关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 我遇到以下问题 E flutter 7144 错误 flutter lib ui ui dart state cc 17
  • 在移动网络浏览器上下拉刷新[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在为网络应用程序提供移动支持 我的应用程序有一个要求 下拉屏幕刷新页面以获取最新更新 我在 iPh
  • PHP - 检查页面是否在移动或桌面浏览器上运行[重复]

    这个问题在这里已经有答案了 在我的 PHP 页面中 我应该根据页面是在移动浏览器还是桌面浏览器下运行来显示两种不同的文本内容 有没有办法在 PHP 中执行此控制 这里有一个非常好的 PHP 库用于检测移动客户端 http mobiledet
  • 在移动 Safari 中检测 iOS5(首选 JavaScript)

    iOS5 中引入的新固定定位损坏了我的网络应用程序 我需要一种方法来检测 iOS5 用户 如何检测iOS5 浏览器代理字符串是什么 首选 JavaScript 谢谢 从SO问题来看 iOS 5 用户代理字符串是什么 https stacko
  • 如何在移动设备上使用Unity3d实现多点触控?

    I use OnMouseDown 可以处理按压 但不可能实现多点触控 该程序包括当您点击时增加然后减少的对象 如果轻轻一按 一切都会正常 但是 当您尝试同时单击多个对象时 它不起作用 我正在尝试解决该问题 但它不起作用 对象无法缩放并且多
  • 为什么 HTML 5 音频忽略移动设备音频设置(例如静音或音量)?

    我有一个针对移动 Android 用户的网站 我正在使用 HTML5 音频来播放背景音乐 即使设备处于静音状态也会播放音频 无论设备音量设置如何 音频都会以最高音量播放 有谁知道为什么会发生这些事情或如何获取设备当前的音频设置 我更喜欢客户
  • jquery-mobile 是“移动优先”吗?

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我读过 Luke WROBLEWSKI 所著的 移动优先 一书以及许多其他有关该主题的读物 我是一名网
  • 如何将现有的 AngularJS 2 Web 应用程序转换为 Cordova 应用程序?

    我有一个用 Angularjs 2 0 构建的 web 应用程序 我想将其转换为 android apk 并将其安装在 android 手机上并进行测试 我没有任何构建移动本机应用程序或将网络应用程序转换为本机应用程序的经验 我已经完成了如
  • Android 浏览器报告屏幕尺寸错误?

    我正在开发一个移动网站 我遇到了一个有趣的问题 我正在我的台式机以及我的 Motorola Droid Android 2 2 上进行测试 我设置了媒体查询来加载 3 个不同的样式表 320 像素宽 480 像素宽和 640 像素宽 我注意
  • CSS 中的重叠文本 - 如何更改它?

    我正在尝试更改 css 文件中的重叠元素 文本 一行文本 在常规浏览器中 在移动设备中显示为两行文本 重叠在一起 此更改适用于网站的移动版本 横向平板电脑的 media 部分 目前 标题 h2 文本在 iPad 平板电脑上重叠 来自 h2
  • 如何使用 j2me 将图像保存到照片库

    我正在使用 j2me 为诺基亚 三星 LG 等制作应用程序 我想将图像保存到手机的照片库中 但我不知道我用什么路径来保存图像 看这篇文章 FileConnection API 入门 http developers sun com mobil
  • Swiper 幻灯片 - 像 Airbnb Slider 一样显示上一张/下一张幻灯片的结束/开始?

    上面是滑块Airbnb 有没有办法获得类似的效果Swiper http idangero us swiper api 对于第一张幻灯片 左侧有一个空白区域并开始 下一张幻灯片的内容 对于中间幻灯片 有上一张和一张的开始和结束位置 下一张幻灯
  • 使用不正确的凭据登录时,Jquery Mobile Rails & Devise 加载页面时出错

    我正在掌握 Rails 3 的窍门 并制作了几个可用的应用程序 我是在 Rails 中使用 javascript 或 jquery 的新手 我有一个使用 Rails 3 2 devise 和 cancan 的工作应用程序 然后我将其转换为使
  • JQuery-UI 水平拖动和垂直滚动

    我有一个页面 其中充满了仅在水平方向 在 X 轴上 的可拖动 div 当我使用触摸设备时 由于滚动和拖动之间的冲突 我无法向下滚动页面 这是一个jsfiddle 示例 http jsfiddle net 742cX 10 在触摸设备中进行测
  • Phonegap从Java代码获取本地存储值?

    我已经使用phonegap在客户端保存了数据本地存储 http docs phonegap com en 1 2 0 phonegap storage storage md html现在我想用java代码访问保存的数据 这可能吗 我怎样才能
  • 如何制作具有移动外观的 emberjs 应用程序(如 jquery mobile 中的应用程序)?

    我有一个使用 Emberjs 的简单移动 Web 应用程序项目 对于外观和感觉 我想要类似于 JQuery Mobile 的东西 有没有办法混合使用 Emberjs 和 jquery mobile 如果是这样 怎么办 我查看了 Travis
  • 钛金 Android 导航组

    您好 我是钛合金新手 它允许开发人员创建跨平台应用程序 我需要创建一个适用于 Android 和 iOS 的导航组 有没有明确的解决方案 因为 Ti UI iPhone createNavigationGrou 仅适用于 iphone 谢谢
  • 根据外形尺寸更改入口点类别

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

随机推荐

  • 古老的密码(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