欧几里德算法计算最大公因数
算法通过连续计算余数知道余数是0为止没最后的非零余数就是最大公因数.
/***************************************************
code writer : EOF
code file : gcd.c
code date : 2014.09.18
e-mail : jasonleaster@gmail.com
description :
This is a implementation of GCD :)
If there is something wrong with my code,
please touch me by e-mail. Thank you.
****************************************************/
unsigned int gcd(unsigned int x,unsigned int y)
{
unsigned int rem = 0;
/*
** set the bigger one as "x"
** the smaller one is "y".
*/
if(x < y)
{
rem = x;
x = y;
y = rem;
}
/*
** The min non-zero reminder is GCD.
*/
for(rem = 0; y > 0;)
{
rem = x % y;
x = y;
y = rem;
}
return x;
}
测试程序:
#include <stdio.h>
#include "gcd.h"
int main()
{
printf("gcd(1989,1590) :%d\n",gcd(1989,1590));
return 0;
}
更新: 2015.04.02
看到了STL里面关于GCD的实现.
7
但丁的小舟 德拉克罗瓦 1822年 油画 189×246厘米 巴黎卢浮宫藏
德拉克罗瓦1822年创作的、取材于但丁名著《神曲》的油画《但丁小舟》(又译《但丁和维吉尔共渡冥河》),画面上,头戴月桂花环的诗人维吉尔正引导他的伙伴但丁乘小舟穿越地狱;船头,一围裹着长条蓝布的赤身男子为其摇橹。浪花翻卷的河水中,几个被罚入地狱者紧紧抓住小船不放;其中还有一个女子,水珠在她身上闪光。近看,那些飞溅的水花其实就是红绿互补色中黄色和白色的涂点。如此精练的手法,如此真实的效果,不能不令观者惊叹。艺术史家认为,《但丁小舟》的悲剧性感受是对米开朗琪罗和鲁本斯的缅怀,其所表现出来的画家的执拗个性则奠定了德拉克洛瓦在法国浪漫主义画派中的先行者地位。