zoukankan
html css js c++ java
辗转相除
节选自:(
http://zhidao.baidu.com/question/5920943.html
)
若 a
=
bq
+
r,则a和b的最大公因子等于b和r的最大公因子绝对值(都为整数)
比如求4864和3458的最大公因子:
4864
=
1
*
3458
+
1406
3458
=
2
*
1406
+
646
1406
=
2
*
646
+
114
646
=
5
*
114
+
76
114
=
1
*
76
+
38
76
=
2
*
38
+
0
所以4864和3458的最大公因子为38
自己跟据这个,写了个用辗转相除求两个数最大公因子的方法。
int
getIn(
int
a,
int
b)
{
if
(b
==
0
)
return
a;
return
getIn(b, a
%
b);
}
百度搜索了一下,果然还是别人的方法简单。
辗转相除递归算法:
//
求最大公约数,公式if(a=b*q+r)then(gcd(a,b)=gcd(b,r))
int
gcd(
int
a,
int
b)
{
return
(a
%
b)
?
gcd(b,a
%
b):b;
}
非递归算法:
//
非递归辗转相除
int
gcd(
int
a,
int
b)
{
int
r
=
0
;
r
=
a
%
b;
while
(r)
{
a
=
b;
b
=
r;
r
=
a
%
b;
}
return
b;
}
张旋(zxsoft)
如对本文有什么疑问,请在下面写下留言,谢谢!
查看全文
相关阅读:
HTTP请求下载文件格式
MT7621 加 openWRT 用HTTP和远程服务器通信
MT7621加 OPENWRT 移植MQTT(paho.mqtt.c) 进行数据的收发
MT7621安装的openwrt出现无法删除文件的问题
GAI_LIB = -lanl
error: expected declaration specifiers or '...' before numeric constant void free(void *);
environment variable 'STAGING_DIR' not defined
ubuntu安装 make4.2
gcc在root权限下查不到版本
【原创】大叔经验分享(113)markdown语法
原文地址:https://www.cnblogs.com/zxsoft/p/940155.html
最新文章
git 撤销&回滚命令
docker save load push 命令
Nginx 设置未绑定域名禁止访问
123456123456----updateV#%#3%#%---pinLv###3%%%----com.zzj.ChildMath688---前show后广--儿童数学-333333
iOS开发 判定某个时间是否属于这个时间段
123456123456----updateV#%#2%#%---pinLv###6%%%----com.zzj.ChildEnglis698---前show后广--儿童英语-111111111
ShenZhenXiaoLengHuanYou Technology Co.,Ltd 技术支持网站
ios评分功能实现
iOS App转让、转移、迁移(App transfer) -- 仅需四步
Pan wutong团队技术支持
热门文章
pan wutong团队隐私协议
RunTime总结:
123456123456----updateV#%#6%#%---pinLv###1%%%----com.zzj.CarCleanGame567---前show后广--儿童洗车-222222
尝试使用Nestjs搭建GraphQL服务
【问题集】Static 静态变量 不能直接使用 @autowired标签的问题
MYSQL SQL查询近一个月的数据
本地搭建SVN服务器(Windows环境)
IDEA中编译报错:常量字符串过长
IDEA中导出GitLab项目的方法
TMDSEVM6678 开发板配套的XDS560V2仿真器单独使用记录
Copyright © 2011-2022 走看看