Java programming implements code sharing for solving eight silver coins and eight silver coins

Source: Internet
Author: User

Java programming implements code sharing for solving eight silver coins and eight silver coins

1. Introduction

I have encountered such a question in an algorithm competition at the university. Now I want to share it with you: There are currently eight silver coins abcdefgh, one of which is known to be counterfeit coins, its weight is different from the real currency, but I do not know whether it is lighter or heavier. How to Use the balance to determine which counterfeit currency is used for the minimum number of comparisons, and learn that the counterfeit currency is lighter or heavier than the real currency.

2. Analysis

If this question is just a simple solution to the counterfeit currency, the problem is not very complicated. You only need to trace back and recursion to obtain the result. We need to take the least steps to solve the problem !!!

Compared with the previous data structure problems, there are recursion and backtracking. Today we may be exposed to a new concept called tree. As the name suggests, the number structure means that our analysis graph is similar to a tree, with various information such as branch nodes. The tree structure is a big chapter in the data structure. It is not discussed here. In this topic, we will introduce a small part of the tree, decision tree.

Let's first build a mathematical model for solving eight silver coins. In a simple situation, we name silver coins as abcdefg and so on. We compare a + B + c with d + e + f. If they are equal, then the counterfeit currency must be g or h. First we compare g or h which is heavier. If g is heavier, we compare it with a (a is real currency). If g is equal to a, g is real currency, h is a counterfeit currency. Because h is lighter than g and g is a real coin, h is lighter than real coin.

What if they are not equal? In what case, We will trace and compare the branches until we get the final answer!

3. Example

Based on the above analysis, we can have a complete decision tree graph:

4. Code

Public class Coins {private int [] coins; public Coins () {coins = new int [8]; for (int I = 0; I <8; I ++) coins [I] = 10;} public void setFake (int weight) {coins [(int) (Math. random () * 7)] = weight;} public void fake () {if (coins [0] + coins [1] + coins [2] = coins [3] + coins [4] + coins [5]) {if (coins [6]> coins [7]) compare (6, 7, 0); else compare (7, 6, 0 );} else if (coins [0] + coins [1] + coins [2]> coins [3] + coins [4] + coins [5]) {if (coins [0] + coins [3] = coins [1] + coins [4]) compare (2, 5, 0 ); else if (coins [0] + coins [3]> coins [1] + coins [4]) compare (0, 4, 1 ); if (coins [0] + coins [3] <coins [1] + coins [4]) compare (1, 3, 0 );} else if (coins [0] + coins [1] + coins [2] <coins [3] + coins [4] + coins [5]) {if (coins [0] + coins [3] = coins [1] + coins [4]) compare (5, 2, 0 ); else if (coins [0] + coins [3]> coins [1] + coins [4]) compare (3, 1, 0 ); if (coins [0] + coins [3] <coins [1] + coins [4]) compare (4, 0, 1) ;}} protected void compare (int I, int j, int k) {if (coins [I]> coins [k]) System. out. print ("\ n counterfeit currency" + (I + 1) + "heavier"); else System. out. print ("\ n counterfeit currency" + (j + 1) + "lighter");} public static void main (String [] args) {if (args. length = 0) {System. out. println ("input counterfeit currency weight (greater than 10 or smaller)"); System. out. println ("ex. java Coins 5 "); return;} Coins eightCoins = new Coins (); eightCoins. setFake (Integer. parseInt (args [0]); eightCoins. fake ();}}

Result:

Input counterfeit currency weight (larger or smaller than 10)
Ex. java Coins 5

Here is a general solution. You can think about the code carefully. The above analysis is sufficient for this Code. The rest of the analysis requires you to learn and understand it.

Summary

The above is all the content shared in this article about java programming implementation to solve the code of eight silver coins, I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.