Balance (not so Mobile) __java

Source: Internet
Author: User
Tags static class

Not so Mobile

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

Submit Status

Description

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

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's lever with the fulcrum on the point where the string ties the wire. From the lever principle we know this to balance a simple mobile the product of the weight of the objects by their Distanc E to the fulcrum must is equal. That iswlxdl = Wrxdr Wheredl are the left distance, the Dr is the right distance, Wl are the left weight andwr are the right Weig Ht.


In a more complex mobile the "object may" replaced by a sub-mobile, as shown in the next figure. In this case it isn't so straightforward to check if the mobile are balanced so we need you to write a, given A description of a mobile as input, checks whether the mobile is in equilibrium or. Input the input begins with a single positive integer in a line by itself indicating the number of the cases following, each Of them as described below. This are followed by a blank line, and there are also a blank line between two consecutive.


The input is composed of several lines, each containing 4 integers separated by a. 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 are a sub-mobile hanging from and the following lines. In this case we compute the weight of the sub-mobile as the sum of the 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 the left Sub-mobiles:first the right one. Output for each test case, the output must follow the description below. The outputs of two consecutive cases is 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
Analysis

The recursive input method is used.

Write the program in C + + language, code as follows:

#include <iostream>
using namespace std;
Enter a sub balance, return the balance of the sub balance, parameter W modified to the total weight of the sub scale
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;
}

Write a program in the Java language with the following code:

(in the test process, the recursive method has been added to the output statement, in the submission process, forget to delete the statement, resulting in a program time overrun.) The answer is correct after you delete the statement.

Here are two ways to pass references in a recursive method of a program:

Method 1:

Import Java.io.BufferedInputStream;

Import Java.util.Scanner;
Note that there is no reference pass in Java, and when we pass the object, the object that the method receives is simply a reference to the same address value, and when we change the object's direction, it does not affect the original address value. When we just change the object itself, without changing the object's properties, the contents of the original object are unchanged. (This is why the following integer instead of int is not allowed to pass the object, the integer object is//is assigned in the method, nor does it affect the caller's integer object) 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);
 }
}

Method 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);
 }
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.