求最大公约数(C语言)

【C语言】实现求两个数的最大公约数【四种算法】 题目 给定两个数,求这两个数的最大公约数 例如: 输入:20 40 输出:20 解题思路 最大公约数:即两个数据中公共约数的最大者 求解的方式比较多,暴力穷举、辗转相除法、更相减损法、Stein算法算法 方法一:辗转相除法(推荐此方法) 思路: 例子:18和24的最大公约数 第一次:a = 18 b = 24 c = a%b = 18%24 = 18 循环中:a = 24 b=18 第二次:a = 24 b = 18 c = a%b = 24%18 = 6 循环中:a = 18 b 阅读详情

一、求最大公约数

求最大公约数是数学中的一种基本问题,它有着广泛的应用场景,如计算机科学、物理学、工程学等领域。本文将介绍三种常用的求最大公约数方法,它们分别是辗转相除法、更相减损法和质因数分解法。

二、辗转相除法

(1)基本原理

辗转相除法,又称欧几里得算法,是求解最大公约数(gcd)或最小公倍数(lcm)的一种简单而古老的算法。
以下是辗转相除法求解最大公约数的基本步骤:

1、如果a能被b整除,则gcd(a,b) = b;
2、如果a不能被b整除,则gcd(a,b) = gcd(b,a%b),令c=a%b,带入第一步,得gcd(a,b) = gcd(b,c);
3、迭代执行以上操作,直到a%b等于0,则b即为最大公约数。

(2)C语言实现

这里用递归来实现(嵌套函数)。具体实现可参考下面的C语言代码。

int gcd(int a, int b) {
   
     //定义函数名为gcd
    if(b == 0) {
   
     // 当 b 为0时,a 即为最大公约数
        return a;
    }
    return gcd(b
在 C 语言中,求最大公约数(GCD,Greatest Common Divisor)的常见的四种方法(附带源代码) 在 C 语言中,求最大公约数(GCD,Greatest Common Divisor)的常见方法有三种:辗转相除法、更相减损法和辗转相减法。以下是这四种方法的简单实现:这些方法都可以求得两个整数的最大公约数。选择哪种方法取决于具体的情况和性能要求。通常情况下,辗转相除法是最常用的方法,因为它在性能上相对较好。 阅读详情

相关推荐

C语言求最大公约数的三种方法

最大公约数是两个数可以同时整除的数中最大的那个 这里用三种方法来解决 穷举法求最大公约数 判断x和y的最大公约数,x和y其中一定有一个相对较小的数,然后从这个较小数开始遍历,不断地用x和y去除这个数,直到可以同时整除,那么就得出了最大公约数。 #include <stdio.h> int MyGCD(int _x, int _y) //定义一个函数求两个数的最大公约数 { if (_x > _y) //进行交换,保证_x是两个数中较小的一个方便后面遍历 { int te

LCW0102的博客 8301

ADRV9009的学习历程(一)

** AD9361的同胞ADRV9009 ** 终于记录一下了 距离第一次想写博客记录自己学习的点点滴滴,大概已经过去有一年有余。一直都是停留在心有余而力不足的境界。 基本每次做项目遇到问题都会来CSDN寻求帮助,这里虽离庙堂较远,但高手云集。每每读来,思绪万千,次次膜拜。 趁这次又学习了一样新物件,就记录一下,望大神看见能赐教一下。 ADRV9009 最近有项目要用到ADRV9009这个芯片。就来学习一下。做过AD9361的都知道,ADI官方会给出这个芯片的参考手册、数据手册和驱动程序。当然我只是见过AD

weixin_42868560的博客 1万+

硬件-汽车电子-TBOX/TCAM核心器件选型与系统集成设计指南

本文深入解析汽车电子TBOX/TCAM硬件架构与系统集成设计,涵盖核心器件选型、电源管理、信号完整性优化等关键点。重点分享5G模组选型避坑经验、MCU隐性指标及热设计验证方案,为车载通信系统开发提供实用指南。

weixin_30352645的博客 525

RV1126上YOLOv8部署实战:从模型转换到板端推理的完整避坑指南

本文提供了在瑞芯微RV1126芯片上部署YOLOv8目标识别算法的完整实战指南。内容涵盖从模型转换、环境搭建到板端推理代码编写的全流程,重点解析了RKNN-Toolkit使用、量化配置、内存优化及后处理实现等关键环节的常见问题与解决方案,旨在帮助开发者高效避坑,实现边缘AI应用的稳定落地。

pz89012345的博客 559

SAP采购含税价转换的底层逻辑解析:ABAP例程如何实现价税分离计算

本文深入解析SAP系统中采购含税价与不含税价的转换机制,重点介绍VOFM例程和ABAP代码实现价税分离计算的方法。通过详细讲解数学原理、税率体系及SAP定价引擎架构,为开发人员提供完整的解决方案,帮助提升采购业务中的价格处理效率和准确性。

weixin_29237635的博客 443

TSMC 40nm LP工艺下,新手画版图最容易踩的5个坑(附Cadence避坑指南)

本文详细解析了TSMC 40nm LP工艺下版图设计的5个常见陷阱,包括MOS管尺寸转换、DNW隔离、Guard Ring设计、金属布线和DRC检查。通过Cadence实战技巧和设计规则优化,帮助新手工程师规避典型错误,提升版图设计效率和质量。

weixin_26800111的博客 452

Python与PyCharm安装配置教程:从零搭建开发环境

编程入门的第一步是搭建开发环境,而Python解释器与IDE工具的选择常让新手困惑。Python作为解释型语言,负责将代码翻译为机器指令,而PyCharm作为集成开发环境,提供代码编辑、调试与项目管理功能,两者协同工作构成高效开发基础。环境变量PATH的配置是安装过程中的关键环节,正确处理可避免“python不是内部或外部命令”的常见错误。理解pip包管理机制,则能轻松安装第三方库并管理项目依赖。从下载官方安装包到运行第一个Hello World程序,再到利用虚拟环境隔离应用依赖,这套流程适用于Window

benben 676

欺诈文本分类微调(一):基座模型选型

通义千问Qwen2具有0.5B、1.5B、7B、72B等一系列参数大小不等的模型,我们需要做的是从大到小依次测试每个模型的能力,找到满足自己需要的最小参数模型。依1.5B和1.5B-Instruct的主要区别:按照官方文档,前者是预训练模型,后者是经过指令微调的模型。

golfxiao的专栏 2652

Ollama本地大模型-个性化AI:训练自己的风格模型和数据投喂打造专属智能体验

之前我们使用Ollama下载了deepseek模型,现在我想将其训练成自己风格的模型,应该怎么做?

瑶山的博客 1775

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

上一篇: 二分查找(C语言)
下一篇: 汉诺塔递归算法(C语言)
小全.
博客等级 码龄4年 352粉丝 31原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小全.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值