﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-The Coder-文章分类-常用算法收集</title><link>http://www.cppblog.com/bch515/category/1877.html</link><description>I am a humble coder.</description><language>zh-cn</language><lastBuildDate>Wed, 21 May 2008 21:57:55 GMT</lastBuildDate><pubDate>Wed, 21 May 2008 21:57:55 GMT</pubDate><ttl>60</ttl><item><title>求最大公约数 欧几里德算法</title><link>http://www.cppblog.com/bch515/articles/7921.html</link><dc:creator>TH</dc:creator><author>TH</author><pubDate>Wed, 31 May 2006 03:33:00 GMT</pubDate><guid>http://www.cppblog.com/bch515/articles/7921.html</guid><wfw:comment>http://www.cppblog.com/bch515/comments/7921.html</wfw:comment><comments>http://www.cppblog.com/bch515/articles/7921.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/bch515/comments/commentRss/7921.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/bch515/services/trackbacks/7921.html</trackback:ping><description><![CDATA[
		<p>欧几里德算法 <br />欧几里德算法(辗转相除法)，用于计算两个整数a,b的最大公约数。<br />其计算原理依赖于下面的定理：</p>
		<p>定理：gcd(a,b) = gcd(b,a mod b) </p>
		<p>证明：a可以表示成a = kb + r，则r = a mod b <br />假设d是a,b的一个公约数，则有 d|a, d|b，<br />而r = a - kb，因此d|r <br />因此d是(b,a mod b)的公约数 </p>
		<p>假设d 是(b,a mod b)的公约数，则 <br />d | b , d |r ，但是a = kb + r <br />因此d也是(a,b)的公约数 </p>
		<p>因此(a,b)和(b,a mod b)的公约数是一样的，其最大公约数也必然相等，得证!</p>
		<p>（注 x|y：y可以被x整除，即 y mod x == 0 ）</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #000000">算法描述：<br />前提：a </span>
				<span style="COLOR: #000000">&gt;</span>
				<span style="COLOR: #000000"> b </span>
				<span style="COLOR: #000000">&gt;</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000"> .<br />返回：a,b的最大公约数。<br />程序：<br /></span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> gcd(</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> a, </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> b)<br />{<br />    </span>
				<span style="COLOR: #0000ff">for</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> r </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> a </span>
				<span style="COLOR: #000000">%</span>
				<span style="COLOR: #000000"> b; r </span>
				<span style="COLOR: #000000">!=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">; r </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> a </span>
				<span style="COLOR: #000000">%</span>
				<span style="COLOR: #000000"> b){<br />        a </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> b;<br />        b </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> r;        <br />    }<br />    </span>
				<span style="COLOR: #0000ff">return</span>
				<span style="COLOR: #000000"> b;<br />}</span>
		</div>
		<br />
		<br />本知识来源于网络.<img src ="http://www.cppblog.com/bch515/aggbug/7921.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/bch515/" target="_blank">TH</a> 2006-05-31 11:33 <a href="http://www.cppblog.com/bch515/articles/7921.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>