The idea of splitting and smelting algorithm

Source: Internet
Author: User

1. The idea of split-smelting algorithm is to divide a complex computational problem into small scale, simple calculation, and then synthesize each small problem to get the answer of the final question.

2. The execution process of the split-metallurgical algorithm

    • For a problem with the magic N, if the problem can be easily resolved, then directly resolve, otherwise perform the following steps.
    • This is decomposed into M-small sub-problems, which are independent of each other and are the same as the original problem.
    • Recursive solutions to these problems,
    • Then, the elder sister of each sub-problem is merged to get the solution of the original problem.

3. Sorting algorithm Examples

Java implementations:

Package com.sjx.test1;
Import Java.util.Scanner;

public class Fenyea {
static final int maxnum = 30;

   //The function is to separate the true and false currency array from the middle, compare the weight of the first half and the last half, and see which one is heavier.
static int falsecoin (int coin[], int low, int high)
{
int I, SUM1, sum2, sum3;
int re = 0;
sum1 = sum2 = sum3 = 0;
if (Low+1==high)
{
if (Coin[low]<coin[high])
{
Re = low +1;
return re;
}
Else
{
re = high+1;
return re;
}
}
if ((high-low+1)%2==0)
{
for (I=low; i<=low+ (high-low)/2; i++);
SUM1 = Sum1+coin[i];
For (i=low+ (high-low)/2+1; i<=high; i++)
sum2 = Sum2+coin[i];
if (sum1>sum2)
{
Re = falsecoin (coin, low+ (high-low)/2+1, high);
return re;
}
else if (sum1<sum2)
{
Re = falsecoin (coin, Low, low+ (high-low)/2);
return re;
}
Else
{
}
}
Else
{
for (I=low; i<=low+ (high-low)/2-1; i++)
Sum1=sum1+coin[i];
For (i=low+ (high-low)/2+1; i<=high; i++)
sum2 = sum2 +coin[i];
SUM3 = coin[low+ (high-low)/2];
if (sum1>sum2)
{
Re = falsecoin (coin, low+ (high-low)/2+1, high);
return re;
}
else if (sum1<sum2)
{
Re = falsecoin (coin, Low, low+ (high-low)/2-1);
return re;
}
Else
{
}
if (SUM1+SUM3==SUM2+SUM3)
{
Re = low + (high-low)/2;
return re;
}
}
return re;
}
public static void Main (string[] args)
{
int[] coin = new Int[maxnum];
int i, n;
int Weizhi;
System.out.println ("Split-smelting algorithm to solve the problem of counterfeit coins!") ");
System.out.print ("Please enter the number of counterfeit money:");
Scanner input = new Scanner (system.in);
n = input.nextint ();
System.out.print ("Please enter the true and false of the silver coin:");//The weight of the real currency is the same and the weight is heavier, and the counterfeit money is relatively light
for (i=0; i<n; i++)
{
Coin[i] = Input.nextint ();
}
Weizhi = falsecoin (coin, 0, n-1);
System.out.println ("In the Above" +maxnum+ "silver coin, the first" +weizhi+ "silver coin is false! ");
Input.close ();
}
}

The idea of splitting and smelting algorithm

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.