当前位置:网站首页>GCD maximum common divisor
GCD maximum common divisor
2022-06-28 03:56:00 【I am already furious!】
gcd function
10 Advance preparation gcd
Let's start with a simple gcd function ( Recursive ), It should be understood at a glance , I won't talk about it here .
int gcd(int a,int b){
return a%b==0?b:gcd(b,a%b);
}
Here is the optimization , It is similar to the above principle , But the complexity is a little lower .
int gcd( int a , int b )
{
return b == 0 ? a : gcd( b , a%b ) ;
}
Binary fetch gcd
Learned from big brother Wang Yu , If even Wang Yu doesn't know , Then you have learned in vain QAQ
First, introduce a concept ,“^” Symbol , It's a decimal number , Or by bit .
Take a chestnut ;
5 Binary for 00101
9 Binary for 01001
After XOR, it is 01100
by 12
The following is the binary exchange of two numbers , It belongs to fast forward and fast read , Because binary is faster than decimal .
void swap( int &a , int &b )
{
a=a^b;
b=a^b;
a=a^b;
}
And then use this to do the operation gcd function
int gcd(int a,int b){
while(b=^a=^b^=a%=b);
return a;
// First a and b The remainder of , And then use the binary exchange rule
// The algorithm is the same as the previous decimal system
// return a Because a%b==0 however a and b Exchanged , So back a Value of equals before return b Value
}
边栏推荐
猜你喜欢
随机推荐
局域网内共享打印机的几种方式
以自动化赋能转型,飞鹤乳业加速迈向数字化!
第九章 APP项目测试(3) 测试工具
iptables防火墙规则和firewalld防火墙规则详解
Arrangement of basic electrical knowledge (II)
Research and arrangement of electronic map coordinate system
数据库系列之MySQL和TiDB中慢日志分析
ambari SSLError: Failed to connect. Please check openssl library versions.
一千行 MySQL 学习笔记,值得收藏!
"Five layer" architecture of cloud applications and services
MySQL configuration of database Series F5 load balancing
Summary of the use of composition API in the project
Lost connection repair: make "hide and seek" nowhere to hide
双亲委派机制的理解学习
Li Kou daily question - day 29 -1491 Average wage after removing minimum wage and maximum wage
leetcode:494. All methods of adding and subtracting operators to the array to get the specified value
gcd最大公约数
第一章 Bash 入门
Learning - useful resources
Arrangement of basic electrical knowledge (I)








