C # implement the oil dumping Algorithm

Source: Internet
Author: User

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 =
Related Article

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.