• <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記錄開發(fā)新知道

            最大公約數(shù)問題

            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
                // 也就是說迭代次數(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;
            }

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

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

            特殊功能
             
            亚洲国产成人精品91久久久| 伊人色综合久久天天| 无码8090精品久久一区| 亚洲午夜无码AV毛片久久| 亚洲精品乱码久久久久久蜜桃图片 | 久久精品日日躁夜夜躁欧美| 久久亚洲私人国产精品vA | 久久精品国产WWW456C0M| 久久国产AVJUST麻豆| 国产美女久久精品香蕉69| 久久久久99精品成人片| 亚洲狠狠婷婷综合久久久久| 精品久久久久久无码免费| 国内精品久久久久久久久电影网 | 青青草原综合久久大伊人精品| 性做久久久久久免费观看| 精品国产乱码久久久久久1区2区 | 久久亚洲精品无码播放| 2022年国产精品久久久久| 热RE99久久精品国产66热| 国产精品久久精品| 久久亚洲精品成人av无码网站| 精品无码人妻久久久久久| 成人国内精品久久久久一区| 2019久久久高清456| 一日本道伊人久久综合影| 狠狠综合久久综合中文88 | 亚洲&#228;v永久无码精品天堂久久 | 久久婷婷综合中文字幕| 亚洲va久久久噜噜噜久久天堂| 色天使久久综合网天天| 久久精品国产只有精品66| 日本道色综合久久影院| 狠狠久久亚洲欧美专区| 国产成人精品白浆久久69| 国产精品一久久香蕉国产线看观看| 国产69精品久久久久观看软件| 成人综合久久精品色婷婷 | 久久久久久久综合日本亚洲| 国产精品久久久亚洲| 99久久99久久精品免费看蜜桃|