C#两个数只能进行+1,-1,*2操作。求使得两个数相等的最小步骤

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 两个数字相等所需最小步骤
{
    public class PrintData
    {
        public int numA;
        public int numB;
        public string mOperator;
        public int result;
    }
    class Program
    {
        public static bool IsEven(int number)
        {
            return number % 2 == 0;
        }
        static void Main(string[] args)
        {
            int min, max, count = 1, c = 0;
            PrintData data;
            List<PrintData> ResultList = new List<PrintData>();
            Console.WriteLine("请输入两个整数数字:");
            bool aok = int.TryParse(Console.ReadLine(), out int a);
            bool bok = int.TryParse(Console.ReadLine(), out int b);
            if (!aok || !bok)
            {
                Console.WriteLine("请输入正确的数字!");
            }
            bool result = false;
            if (a == b)
            {//如果已经相等不需要操作
                Console.WriteLine("不需要操作两个数字已经相等!");
                return;
            }
            if (a < b)
            {//判断数字大小将最大数值保存在max,最小数值保存在min
                max = b;
                min = a;
            }
            else
            {
                max = a;
                min = b;
            }
            a = max;
            b = min;
            while (!result)
            {
                if (a / b > 2)
                {
                    if (IsEven(a))
                    {
                        data = new PrintData();
                        data.numA = a;
                        data.numB = 2;
                        data.result = a / 2;
                        data.mOperator = "/";
                        ResultList.Add(data);
                        a = a / 2;

                    }
                    else
                    {
                        data = new PrintData();
                        data.numA = b;
                        data.numB = 2;
                        data.result = b * 2;
                        data.mOperator = "*";
                        ResultList.Add(data);
                        b = b * 2;
                    }
                }
                else
                {
                    if (a > b * 2 && a % b >= min || a < b * 2 && (Math.Abs(a % b - b) >= min))
                    {
                        ResultList.Clear();

                        a = max;
                        b = min + count++;
                        for (int i = 0; i < count - 1; i++)
                        {
                            //str = "第" + (ResultList.Count + 1) + "步:" + (min + i) + "+1 =" + (min + i + 1);
                            //ResultList.Add(str);
                            data = new PrintData();
                            data.numA = min + i;
                            data.numB = 1;
                            data.result = min + i + 1;
                            data.mOperator = "+";
                            ResultList.Add(data);
                        }

                    }
                    else
                    {
                        if (IsEven(a))
                        {
                            data = new PrintData();
                            data.numA = a;
                            data.numB = 2;
                            data.result = a / 2;
                            data.mOperator = "/";
                            ResultList.Add(data);
                            a = a / 2;
                            c = a - b;
                        }
                        else
                        {
                            data = new PrintData();
                            data.numA = b;
                            data.numB = 2;
                            data.result = b * 2;
                            data.mOperator = "*";
                            ResultList.Add(data);
                            b = b * 2;
                            c = a - b;
                        }
                        result = true;
                    }
                }
                while (c > 0)
                {
                    //str = "第" + (ResultList.Count + 1) + "步:" + b + "+1 =" + (++b);
                    data = new PrintData();
                    data.numA = b;
                    data.numB = 1;
                    data.result = ++b;
                    data.mOperator = "+";
                    ResultList.Add(data);
                    c--;
                }
                while (c < 0)
                {
                    //str = "第" + (ResultList.Count + 1) + "步:" + b + "-1 =" + (--b);
                    data = new PrintData();
                    data.numA = b;
                    data.numB = 1;
                    data.result = --b;
                    data.mOperator = "-";
                    ResultList.Add(data);
                    c++;
                }
            }
            List<PrintData> TempData = new List<PrintData>();
            for (int i = 0; i < ResultList.Count; i++)
            {
                if (ResultList[i].mOperator == "/")
                {
                    TempData.Add(ResultList[i]);
                    ResultList.RemoveAt(i);
                    i--;
                }
            }
            for (int i = TempData.Count - 1; i >= 0; i--)
            {
                ResultList.Add(TempData[i]);
            }
            int index = 1;
            foreach (var item in ResultList)
            {
                if (item.mOperator == "/")
                {
                    Console.WriteLine("第{0}步:{1} {2} {3} = {4} ", index, item.result, "*", item.numB, item.numA);
                }
                else
                {
                    Console.WriteLine("第{0}步:{1} {2} {3} = {4} ", index, item.numA, item.mOperator, item.numB, item.result);
                }
                index++;
            }
            Console.ReadLine();
        }
    }
}

当前实现了两个数同号的算法操作。如果两个数异号的话,只需将绝对值较小的那个数执行操作先达到同号再进行如上操作即可。

posted @ 2019-03-15 15:21  xiaobao5161  阅读(475)  评论(0编辑  收藏  举报