惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
A
About on SuperTechFans
量子位
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog
U
Unit 42
D
DataBreaches.Net
博客园 - Franky
D
Docker
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Hugging Face - Blog
Hugging Face - Blog

StudyingLover's Blog

Diffusion Policy笔记 rwkv笔记 act笔记 nanovllm-block_manager opencode多智能体 nanobot-pre-train nanobot-rl nanobot-sft nanobot-checkpoint_manager nanobot-gpt nanobot-mid-train Vision Mamba (Vim)笔记 BPE演示 最后一遍学习Transformer YOLOv5 目标检测笔记 下载根服务器解析记录 Dynaseal A Backend-Controlled LLM API Key Distribution Scheme with Constrained Invocation Parameters 判断链表有环 王道25数据结构勘误 关于perplexity的open-sourcing-r1-1776 AI为什么不像人类一样进行多轮对话 新博客改造日记和功能测试 linuxqq只显示登陆背景图 数字设计和计算机体系结构(机械工业出版社)勘误(自制) Dynaseal:面向未来端侧llm agent的llm api key分发机制 A Definitive Guide to Markdown Style This post is using MDX, Where you can embed JSX and Astro components RT-Patch学习 pydantic实现的LLM ReAct fastapi 和 uvicorn 设置监听 ipv6
花式求GCD
About the Author StudyingLover · 2023-08-03 · via StudyingLover's Blog

花式求GCD

今天学校实验室纳新群有同学提到了a^=b^=a^=b​ 交换两个数的操作,我突然想到之前在知乎看到通过异或实现gcd的方法,一番翻找后没啥结果,便去问了下认识的oi大佬有没有一行求gcd的算法。

大佬很快给出了一个函数int gcd(int a,int b){return y?gcd(y,x%y):x;} 真的就是一行,完整的代码就是下面这个

#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int main(){
	int a,b;
	a=10;
	b=20;
	a = gcd(a,b);
	cout<<a<<endl;
	return 0;
}

但是我一像不对啊,我的异或呢?我又问了一下,大佬给了我一个截图

就是这个神奇的写法

这段代码的实现方式是,使用异或运算符(^)和取模运算符(%)来交换变量a和b的值。具体来说,代码中的while循环会一直执行,直到b的值为0为止。在每次循环中,代码会先将a对b取模,然后将结果赋值给a,接着将b对a取模,然后将结果赋值给b,最后使用异或运算符交换a和b的值。这样,当循环结束时,a和b的值就被成功地交换了。(来自copilot chat)

#include <bits/stdc++.h>
using namespace std;
int main(){
	int a,b;
	a=10;
	b=20;
	while(b^=a^=b^=a%=b);
	cout<<a<<endl;
	return 0;
}