天平(Not so Mobile)__java

來源:互聯網
上載者:User

Not so Mobile

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

Submit Status

Description

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 

10 2 0 40 3 0 11 1 1 12 4 4 21 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;elsecout << "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");elseSystem.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");elseSystem.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);}}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.