zoukankan      html  css  js  c++  java
  • 一个分油的逻辑问題C#实现

    第一次用word 2007POST

    /// <summary>

    /// 两个小孩去打油,一人带了一个一斤的空瓶,另一个带了一个七两和一个三两的空瓶。

    /// 原计划各打一斤油,可是由于所带的钱不够,只好合打了一斤油,

    /// 在回家的路上,二人想平分这一斤油,可是又没有其它工具。

    /// 现只用这三个瓶子(一斤、七两、三两)精确地分出两个半斤油来。

    /// </summary>

    /// <remarks>author petter liu 2008726</remarks>

    [TestFixture]

    public class Testnkap

    {

    private int a10 = 10;

    private int a7 = 0;

    private int a3 = 0;

    private int count = 0;

     

    /// <summary>

    /// fenyou

    /// <remarks>

    /// 5=3+25=7-2;关键是要多出一个2L的油,2L的空间。

    /// </remarks>

    /// </summary>

    [Test]

    public void fenyou()

    {

    Console.WriteLine("10->7");

    swap(ref a10, ref a7, 7);

    Console.WriteLine("7->3");

    swap(ref a7, ref a3, 3);

    Console.WriteLine("3->10");

    swap(ref a3, ref a10, 3);

    Console.WriteLine("7->3");

    swap(ref a7, ref a3, 3);

    Console.WriteLine("3->10");

    swap(ref a3, ref a10, 3);

    Console.WriteLine("7->3");

    swap(ref a7, ref a3, 1);

    Console.WriteLine("10->7");

    swap(ref a10, ref a7, 7);

    Console.WriteLine("7->3");

    swap(ref a7, ref a3, 2);

    Console.WriteLine("3->10");

    swap(ref a3, ref a10, 3);

    Console.ReadLine();

    }

     

    /// <summary>

    /// 交换Function

    /// </summary>

    /// <param name="x">source</param>

    /// <param name="y">target</param>

    /// <param name="num">num</param>

    private void swap(ref int x, ref int y, int num)

    {

    x = x - num;

    y = y + num;

    count++;

    Console.WriteLine("{0}", count);

    Console.WriteLine("{0} {1} {2}", a10, a7, a3);

    Console.WriteLine("--------------------------");

    }

    }

    输出结果:

    10->7

    第次

    3 7 0

    --------------------------

    7->3

    第次

    3 4 3

    --------------------------

    3->10

    第次

    6 4 0

    --------------------------

    7->3

    第次

    6 1 3

    --------------------------

    3->10

    第次

    9 1 0

    --------------------------

    7->3

    第次

    9 0 1

    --------------------------

    10->7

    第次

    2 7 1

    --------------------------

    7->3

    第次

    2 5 3

    --------------------------

    3->10

    第次

    5 5 0

    --------------------------

  • 相关阅读:
    腾讯2017暑期实习编程题3
    腾讯2017暑期实习编程题2
    腾讯2017暑期实习编程题1
    力扣算法题—098验证二叉搜索树
    题目1451:不容易系列之一
    题目1362:左旋转字符串(Move!Move!!Move!!!)
    HDU 2564 词组缩写
    HDU 2561 二小整数
    HDU 2034 人见人爱A-B
    HDU 1875 畅通工程再续
  • 原文地址:https://www.cnblogs.com/wintersun/p/1251992.html
Copyright © 2011-2022 走看看