• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            MyMSDN

            MyMSDN記錄開(kāi)發(fā)新知道

            最大公約數(shù)問(wèn)題

            image

            image

            image

            image

            image 

            image

            image

            image

            image

            以上內(nèi)容摘自《編程之美》P150-154。

            為了方便使用,下面是可拷貝的代碼:

            Math.h

            #pragma once
            
            class Math
            {
            public:
                Math(void);
                ~Math(void);
            
            public :
                //編程之美P150-154
            
                //求最大公約數(shù),歐幾里德——輾轉(zhuǎn)相除法
                static int Gcd1(int x, int y);
            
                //求最大公約數(shù),歐幾里德——輾轉(zhuǎn)相除法(變相將除法變成了減法)
                static int Gcd2(int x, int y);
            
                static int Gcd3(int x, int y);
            
                inline static bool IsEven(int x);
            
                inline static int Absolute(int x);
            };
            

            Math.cpp

            #include "Math.h"
            
            Math::Math(void)
            {
            }
            
            Math::~Math(void)
            {
            }
            
            int Math::Gcd1(int x, int y)
            {
                //y, x%y順序不能錯(cuò);
                return y ? Gcd1(y, x % y) : x;
            }
            
            int Math::Gcd2(int x, int y)
            {
                //與Gcd1相同的方式,但由于x%y計(jì)算速度較x-y要慢,但效果相同,所以換用x - y
                // 但用減法和除法不同的是,比如和,%20=10,-20=70,也就是-4×=10
                // 也就是說(shuō)迭代次數(shù)較Gcd1而言通常是增加了。
                return y ? Gcd1(y, x - y) : x;
            }
            
            int Math::Gcd3(int x, int y)
            {
                if(x < y)
                    return Gcd3(y, x);
                if(y == 0)
                    return x;
                else
                {
                    if(IsEven(x))
                    {
                        if(IsEven(y))
                            return (Gcd3(x >> 1, y >> 1) << 1);
                        else
                            return Gcd3(x >> 1, y);
                    }
                    else
                    {
                        if(IsEven(y))
                            return Gcd3(x, y >> 1);
                        else
                            return Gcd3(y, x - y);
                    }
                }
            }
            
            bool Math::IsEven(int x)
            {
                return !(bool)x & 0x0001;
            }
            
            int Math::Absolute(int x)
            {
                return x < 0 ? -x : x;
            }

            Main.cpp

            #include <stdafx.h>
            #include <iostream>
            #include "Math.h"
            
            using namespace std;
            int _tmain(const int & arg)
            {
                cout<<"Math::Gcd1(42,30) = "<<Math::Gcd1(42,30)<<endl;
                cout<<"Math::Gcd1(30,42) = "<<Math::Gcd1(30,42)<<endl;
                cout<<"Math::Gcd1(50,50) = "<<Math::Gcd1(50,50)<<endl;
                cout<<"Math::Gcd1(0,0) = "<<Math::Gcd1(0,0)<<endl;
                cout<<"Math::Gcd1(-42,-30) = "<<Math::Gcd1(-42,-30)<<endl;
                cout<<"Math::Gcd1(-42,30) = "<<Math::Gcd1(-42,30)<<endl;
            
                cout<<"------------------------------"<<endl;
            
                cout<<"Math::Gcd2(42,30) = "<<Math::Gcd2(42,30)<<endl;
                cout<<"Math::Gcd2(30,42) = "<<Math::Gcd2(30,42)<<endl;
                cout<<"Math::Gcd2(50,50) = "<<Math::Gcd2(50,50)<<endl;
                cout<<"Math::Gcd2(0,0) = "<<Math::Gcd2(0,0)<<endl;
                cout<<"Math::Gcd2(-42,-30) = "<<Math::Gcd2(-42,-30)<<endl;
                cout<<"Math::Gcd2(-42,30) = "<<Math::Gcd2(-42,30)<<endl;
            
                cout<<"------------------------------"<<endl;
            
                cout<<"Math::Gcd3(42,30) = "<<Math::Gcd3(42,30)<<endl;
                cout<<"Math::Gcd3(30,42) = "<<Math::Gcd3(30,42)<<endl;
                cout<<"Math::Gcd3(50,50) = "<<Math::Gcd3(50,50)<<endl;
                cout<<"Math::Gcd3(0,0) = "<<Math::Gcd3(0,0)<<endl;
                cout<<"Math::Gcd3(-42,-30) = "<<Math::Gcd3(-42,-30)<<endl;
                cout<<"Math::Gcd3(-42,30) = "<<Math::Gcd3(-42,30)<<endl;
            
                return 0;
            }

            不過(guò)有一點(diǎn)值得一提,就是所謂性能最好效率最高的Gcd3不支持負(fù)數(shù),也就是最后兩行測(cè)試代碼無(wú)法通過(guò)。但是限于對(duì)負(fù)數(shù)的最大公約數(shù)并沒(méi)有定義,也就是說(shuō)即便上面的Gcd1和Gcd2好像算出了負(fù)數(shù),但它們的結(jié)果沒(méi)有意義。

            posted on 2009-03-04 23:52 volnet 閱讀(1029) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 知識(shí)庫(kù)(KnowledgeLibrary)

            特殊功能
             
            久久综合偷偷噜噜噜色| 久久综合88熟人妻| 青青久久精品国产免费看| 伊人色综合久久天天人守人婷| 亚洲日本久久久午夜精品| 日韩久久久久久中文人妻| 色婷婷综合久久久中文字幕| 久久久91精品国产一区二区三区| 精品国产青草久久久久福利| 无码精品久久久久久人妻中字 | 青青草国产成人久久91网| 久久伊人中文无码| 97久久超碰国产精品2021| 国内精品久久久久久久久电影网| 无码超乳爆乳中文字幕久久| 久久精品国产亚洲一区二区三区| 色综合久久无码中文字幕| 国产精品青草久久久久福利99| 热re99久久精品国99热| 日韩精品无码久久一区二区三| 亚洲伊人久久大香线蕉苏妲己| 中文字幕无码免费久久| 亚洲色欲久久久久综合网| 国产精品免费久久| 久久精品国产99国产精品澳门 | a高清免费毛片久久| 中文精品99久久国产 | 久久99热这里只有精品国产| 国内精品久久久久久久涩爱| 99久久精品无码一区二区毛片| AV无码久久久久不卡网站下载| 亚洲AV日韩精品久久久久久久| 色妞色综合久久夜夜| 亚洲国产精品成人久久蜜臀 | 精品多毛少妇人妻AV免费久久| 国产一级做a爰片久久毛片| 国内精品久久久久影院一蜜桃 | 久久精品国产亚洲AV忘忧草18| 亚洲一级Av无码毛片久久精品| 三级韩国一区久久二区综合| 久久影视国产亚洲|