zoukankan      html  css  js  c++  java
  • 不使用运算符 /号实现两整数相除

    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;
    
            }
    
        }
    }
    

      

  • 相关阅读:
    Xcode6中如何添加pch文件
    iOS 在UILabel显示不同的字体和颜色
    IOS 获取手机ip地址
    获取设备基本信息
    [__NSCFConstantString size]: unrecognized selector sent to instance 错误
    控制器之间的跳转,多层的跳转
    Multiple build commands for output file
    Can't find keyplane that supports type 4 for keyboard iPhone-Portrait-NumberPad; using 3876877096_Portrait_iPhone-Simple-Pad_Default
    Activity的生命之路
    Spring自定义标签
  • 原文地址:https://www.cnblogs.com/ProDoctor/p/7243708.html
Copyright © 2011-2022 走看看