This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog Algorithm - Classic interesting topics - looking for fake silver coins - Joshua 317's blog
1, Question
Looking for fake silver coins is a very interesting intellectual topic. The main idea of looking for fake silver coins is as follows: now there are 8 silver coins, one of which is fake. However, in terms of appearance and workmanship, we can't tell which is real and which is fake. We only know that the weight of fake money is slightly lighter than real money. It is required to use only one balance. How to find fake silver coins with the least steps?
2, Analysis
Let's analyze the problem of looking for fake silver coins. In fact, it is not difficult to find fake silver coins. One of the most basic methods is to number the coins (1 ~ 8), and then compare them by balance. The operation steps are as follows:
(1) First, compare the weight of the first silver coin and the second silver coin. If both sides of the balance are balanced, proceed to the next step. Otherwise, the coin on the lighter side is counterfeit;
(2) Then compare the weight of the third silver coin and the fourth silver coin. If both sides of the balance are balanced, proceed to the next step, otherwise the coin on the lighter side is counterfeit;
......
Repeat the above steps until all 8 silver coins are compared, and you can find the fake silver coins. This pairwise comparison method is simple, but it is not efficient and requires more steps. Here you need to find the least steps.
The idea of recursive divide and conquer can be used to solve this problem. The operation steps are as follows:
(1) First, number each silver coin, and then divide all silver coins into two parts and put them on both sides of the balance.
(2) Because the counterfeit silver coins are light, the lighter side of the balance must contain the counterfeit silver coins.
(3) Then divide the hard silver coins in the lighter side into two parts and repeat the above practice.
(4) Until there are two hard silver coins left, you can find the fake silver coins directly with a balance.
This method shows its advantages when there are a large number of silver coins. According to this idea, the corresponding algorithm for finding fake silver coins can be written.
3, Programming
package com.joshua317; import java.util.Scanner; public class Main { public static void main(String[] args) { int n; int position; System.out.println("Divide and conquer algorithm for counterfeit money problem"); System.out.println("Please enter the total number of silver coins"); Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); int[] coin = new int[n]; System.out.println("Please enter the authenticity of silver coins. There can only be one counterfeit coin,,2 For true, 1 for false"); boolean flag = false; for (int i = 0; i < n; i++) { coin[i] = scanner.nextInt();//Accept the authenticity of silver coins if (coin[i] == 1) { flag = true; } } if (!flag) { System.out.println("There must be a counterfeit"); return; } position = FindFakeCoin.getFakeCoin(coin, 0, n-1); System.out.println("Above" + n + "Of the silver coins, the" + position + "A silver coin is fake!"); } } class FindFakeCoin { public static int getFakeCoin(int coin[], int low, int high) { int i, sum1, sum2, sum3; int re = 0; sum1 = sum2 = sum3 = 0; //The last two if (low + 1 == high) { if (coin[low] < coin[high]) { re = low + 1; return re; } else if (coin[low] > coin[high]) { re = high+1; return re; } else { return re; } } //Coins are even if ((high - low + 1) % 2 == 0) { //Sum of the first half broken for (i = low; i <= low + (high - low) / 2; i++) { sum1 += coin[i]; } //The sum of the latter half for (i = low + (high - low) / 2 + 1; i <= high; i++) { sum2 += coin[i]; } if (sum1 < sum2) { re = getFakeCoin(coin, low, low + (high - low) / 2); return re; } else if (sum1 > sum2) { re = getFakeCoin(coin, low + (high - low) / 2 + 1, high); return re; } else { } } else { //Sum of the first half broken for (i = low; i <= low + (high - low) / 2 - 1; i++) { sum1 += coin[i]; } //The sum of the latter half for (i = low + (high - low) / 2 + 1; i <= high; i++) { sum2 += coin[i]; } sum3 = coin[low+(high-low)/2]; if (sum1 < sum2) { re = getFakeCoin(coin, low, low + (high - low) / 2 - 1); return re; } else if (sum1 > sum2) { re = getFakeCoin(coin, low + (high - low) / 2 + 1, high); return re; } else { } if (sum1 + sum3 == sum2 + sum3) { re = low + (high - low) / 2 + 1; return re; } } return re; } }
This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog Algorithm - Classic interesting topics - looking for fake silver coins - Joshua 317's blog