The original question is as follows: 12 (a bucket is full of 12 kg of oil) 6 kg of oil is taken out of the bucket has an additional 8 kg (B bucket) and 5 kg (c bucket) the two empty buckets let the program output the steps to fetch the 6 kg oil
The algorithm can be configured with parameters (several buckets are defined, the initial amount of oil is available, and the amount of oil to be obtained can be configured), and any one can be found (multiple lines or not found) line chart of oil recovery meeting the conditions:
Running effect:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
// Customize the initial information here
Oil. Initial Oil volume = "12, 0, 0 ";
Oil. bucket capacity = "12, 8, 5 ";
Oil. required fuel volume = "6 ";
Oil oil = new Oil ();
Status SuperStatus = oil. GetStartState ();
Oil. status. Add (SuperStatus );
Oil. Execute (SuperStatus );
If (oil. Endstatus. Count = 0)
{
Console. WriteLine ("the specified quality oil cannot be poured out. ");
}
Else
{
Console. WriteLine ("found" + oil. Endstatus. Count. ToString () + "oil line :");
}
For (int I = 0; I <oil. Endstatus. Count; I ++)
{
Console. WriteLine (oil. translation route (oil. Endstatus [I]);
}
}
}
Class Oil
{
Public static string bucket capacity;
Public static string initial oil volume;
Public static string required fuel volume;
// Automatically record the total number of buckets
Int count;
Public Oil ()
{
Count = capacity of the bucket. Split (,). Count ();
Capacity = new int [count];
For (int I = 0; I <count; I ++)
{
Capacity [I] = Convert. ToInt32 (bucket capacity. Split (,) [I]);
}
}
Public int [] capacity; // new int [] {12, 8, 5 };
Public List <Status> status =