1. 程式人生 > >不使用運算符 /號實現兩整數相除

不使用運算符 /號實現兩整數相除

reading tar math ati har pro cond return lin

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(div(1888888888,5));
            Console.Read();
        }

        /// <summary>
        /// 整數相除
        /// </summary>
        /// <param name="a">除數</param>
        /// <param name="b">被除數</param>
        /// <returns></returns>
        public static int div (int a,int b){
            List<int> history = new List<int>();

            double count = 0;
            while (true)
            {
                if (a - b < 0) break;
                history.Add(b);
                count++;
                a = a - b;
                b += b;

            }
            count = (Math.Pow(2, count)) - 1;
            for (int i =history.Count-1; i >-1; i--)
            {
                if (a < 1) break;
                if (a - history[i] < 0) continue;
                count += Math.Pow(2, i);
                 a -= history[i];
                 
            }
            Console.WriteLine("余數為"+a);
            return (int) count;

        }
    }
}

  這裏沒有考慮被除數為0的情況

技術分享

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < 1000000; i++)
            {
             int s=   div(1888888888, 5);
            }
            watch.Stop();
            Console.WriteLine("土方法耗時"+watch.ElapsedMilliseconds);
            watch.Reset();
            watch.Start();

            for (int i = 0; i < 1000000; i++)
            {
              int s=  (1888888888/5);
            }
            watch.Stop();
            Console.WriteLine("系統方法耗時" + watch.ElapsedMilliseconds);

            Console.Read();
        }

        /// <summary>
        /// 整數相除
        /// </summary>
        /// <param name="a">除數</param>
        /// <param name="b">被除數</param>
        /// <returns></returns>
        public static int div (int a,int b){
            List<int> history = new List<int>();

            double count = 0;
            while (true)
            {
                if (a - b < 0) break;
                history.Add(b);
                count++;
                a = a - b;
                b += b;

            }
            count = (Math.Pow(2, count)) - 1;
            for (int i =history.Count-1; i >-1; i--)
            {
                if (a < 1) break;
                if (a - history[i] < 0) continue;
                count += Math.Pow(2, i);
                 a -= history[i];
                 
            }
            //Console.WriteLine("余數為"+a);
            return (int) count;

        }

    }
}

  技術分享

不使用運算符 /號實現兩整數相除