最大公约数(辗转相除)

【Python】用辗转相除法求两个正整数的最大公约数 用python实现用辗转相除法求最大公约数 阅读详情

题目描述

输入两个正整数,求其最大公约数。

输入描述

测试数据有多组,每组输入两个正整数。

输出描述

对于每组输入,请输出其最大公约数。

输入

49 14

输出

7

代码

常规法

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int i,x,y;
    while(cin>>x>>y){
        int Min=min(x,y);
        for(int i=Min;i>=1;i--){
            if(x%i==0&&y%i==0){
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}

进阶版1:辗转相除法

#include<iostream>
using namespace std;
int GCD(int a,int b){
    return a%b?GCD(b,a%b):b;
}
int main(){
    int x,y;
    while(cin>>x>>y){
        cout<<GCD(x,y)<<endl;
    }
    return 0;
}

进阶版2:

#include<iostream>
using namespace std;
int gcd(int a,int b){
    if(a%b==0)
        return b;
    return gcd(b,a%b);
}
int main(){
    int x,y;
    cin>>x>>y;
    cout<<gcd(x,y)<<endl;
    return 0;
}
保姆级教程:在Windows上用VS2019和OpenCV4.6搞定海康MV-EB435i RGBD相机图像采集 本文提供了一份详细的教程,指导如何在Windows环境下使用VS2019和OpenCV4.6配置海康MV-EB435i RGBD相机进行图像采集。从开发环境准备、SDK部署到代码移植和优化,涵盖了深度图与RGB图同步采集的实战技巧,帮助开发者快速上手工业级RGBD相机的应用开发。 阅读详情

相关推荐

Coppelia Sim (v-REP)仿真 机器人3D相机手眼标定与实时视觉追踪 (一)

先把这连个都添加到场景中,然后把kinect的depth相机拖到vision sensor中,把kinect的script也拖到vision sensor中,同时要注意修改深度相机的坐标,及修改script,删掉/rgb部分。由于需要导入到仿真场景中PDF文件不能直接导入,所以需要生成图下文件,使用windows自带的画图工具,生成以厘米为单位的图像格式,大小和标定板的设置大小一直。可以生成需要用的标定板的文件,设置标定板的大小,棋盘格的大小,等相关的参数,之后可以保存PDF文件。设置plane的大小。

hong3731的专栏 1753

【慢慢学算法】:求最大公约数

题目描述: 输入两个正整数,求其最大公约数。 输入: 测试数据有多组,每组输入两个正整数。 输出: 对于每组输入,请输出其最大公约数。 样例输入: 49 14 样例输出: 7 代码: #include <iostream> using namespace std; int main() { int a,...

weixin_30402343的博客 413

安卓逆向实战:用Batch Apktool 3.8.0快速汉化APK的完整流程

本文详细解析了如何使用Batch Apktool 3.8.0进行安卓APK汉化的完整流程,包括环境配置、反编译、资源解析、深度汉化、重构建与签名优化等关键步骤。通过这款集成化逆向工具,开发者可以高效完成APK汉化,提升工作效率。

s1t2u3的博客 1101

第六周测验

1788:Pell数列 http://noi.openjudge.cn/ch0202/1788/ 描述 Pell数列a1, a2, a3, ...的定义是这样的,a1 = 1, a2 = 2, ... , an = 2 * an − 1 + an - 2 (n &gt; 2)。 给出一个正整数k,要求Pell数列的第k项模上32767是多少。 输入 第1行是测试数据的组数n,后面跟着n行输...

yanyanwenmeng的博客 1520

基于FME—三调篇—尖锐角及小缝隙处理

1 问题分析: 该功能实现在FME较为简单,相对ARCGIS模型构建器以及ARCGIS ENGINE,FME更适合无代码基础的人群。在三调内业数据处理要求中,尖锐角是不允许存在的。本文基于FME实现了对尖锐角的自动查询与消除以及自动处理在消除尖锐角过程中产生的缝隙,并进行了拓扑检查,实现自动化批量处理尖锐角的功能,提高工作效率。 2 总体思路: 2.1 实现功能分析: 1.找出小于指定角度的尖锐角...

qq_33315579的博客 9182

望获实时Linux与国产芯片协同:打造LinuxCNC数控机床的全栈国产化方案

本文探讨了如何通过望获实时Linux与飞腾、龙芯等国产芯片的深度协同,为LinuxCNC数控机床打造全栈国产化方案。望获实时Linux通过中断隔离等核心技术,提供亚微秒级确定性响应,解决了传统Linux系统实时性不足的痛点,使LinuxCNC得以稳定驱动高精度机床。该方案实现了从硬件、操作系统到应用软件的全栈自主可控,为工业控制领域提供了高性能、可定制的国产化选择。

weixin_29291069的博客 648

求两个数的最大公约数

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34252686的博客 196

【Tessy】集成测试小白入门指导

本文详细介绍了如何进行集成测试的小白入门指导,从新建工程到设计用例,实现集成测试的过程详细步骤

qq_37282365的博客 1194

题目47:最大公约数

题目描述 题目描述: 输入两个正整数,求其最大公约数。 输入: 测试数据有多组,每组输入两个正整数。 输出: 对于每组输入,请输出其最大公约数。 样例输入: 49 14 样例输出: 7 解题思路 #include<iostream> using namespace std; int main() { int m,n; cin>>m>>n; int te...

八哥快走开的博客 494

YOLOv11 损失函数解析

YOLOv11 在 YOLO 系列的持续演进中,通过 ​多模态损失协同优化、动态任务感知机制 和 ​超轻量化设计,实现了检测精度与推理效率的再突破。其损失函数设计融合了前沿的几何建模、分布学习与自监督技术,显著提升了复杂场景下的鲁棒性。

浩瀚之水的专栏 6702

FPGA信号处理系列文章——分数倍插值抽取滤波器(一)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 FPGA信号处理系列文章——分数倍插值抽取滤波器(一)前言信号流matlab模型频谱示意图分析总结 前言 变采样率滤波器不仅仅存在整数倍插值和整数倍抽取的情况,还会存在分数倍插值抽取的情况。那这种情况下要怎么处理呢 信号流 分数倍插值抽取滤波器的信号流是什么?信号先进行插值,再进行FIR滤波,最后进行抽取 matlab模型 %signal 定点化数据输入 coeff_val_fir = importdata('fir.coe'); si

gzy0506的博客 1万+

XYNU OJ 1073: 习题5-3-2 求最大公约数

题目描述 输入两个正整数,求其最大公约数。 输入 测试数据有多组,每组输入两个正整数,两个正整数之间以空格分隔。 输出 对于每组输入,输出其最大公约数。 每组对应一个输出,单独占一行。 样例输入 14 49 21 66 样例输出 7 3 #include int main() { int m,n,i; while(scanf("%d %d

Xing___wei的博客 1267

Claude Code 完全指南:从入门到精通

《AI编程助手ClaudeCode深度指南》摘要 ClaudeCode作为新一代AI编程助手,凭借200K tokens的超长上下文窗口和深度代码理解能力,正在重塑开发工作流程。本文系统介绍了从安装配置到高级应用的完整使用指南: 核心优势包括: 项目级代码理解能力 Agent自主规划复杂任务 原生终端集成支持 多文件协同编辑功能 实战应用场景: 快速理解陌生项目架构 诊断性能瓶颈并提供优化方案 自动生成符合规范的API代码 开发可复用技术组件 高级技巧: 通过CLAUDE.md定义项目规范 合理使用/cle

分享AI、区块链技术及应用,未来已来 604

求最大公约数(辗转相除法)

最大公约数(Greatest Common Divisor)指两个或多个整数共有约数中最大的一个。 也称最大公因数、最大公因子,a, b的最大公约数记为(a,b),同样的,a,b,c的最大公约数记为(a,b,c),多个 整数的最大公约数也有同样的记号。求最大公约数有多种 方法,常见的有质因数分解法、短除法、辗转相除法、更相减损法。与最大公约数相对应的概念是最小公倍数,a,b的 最小...

晴空๓ 11万+

深度学习工具包 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")

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

利用辗转相除法——求最大公约数(详解)

辗转相除法 求最大公约数: 给定两个数,求这两个数的最大公约数 欧几里得算法又称辗转相除法,是指用于计算两个非负整数a,b的最大公约数。应用领域有数学和计算机两个方面。计算公式gcd(a,b) = gcd(b,a mod b)。 例如:假如需要求 100 和18 两个正整数的最大公约数,用欧几里得算法,是这样进行的: 100 / 18 = 5 (余 10) 18 / 10= 1(余8) 10 / 8 = 1(余2) 8 / 2 = 4 (余0) 至此,最大公约数为2 以除数和余数反复做除法运算,当余数为 0

H48530的博客 8万+

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【算法】辗转相除法求最大公约数

辗转相除法,又称欧几里德算法(Euclidean Algorithm),是求两个数的最大公约数(greatestcommondivisor)的一种方法。用较大的数除以较小的数,再以除数和余数反复做除法运算,当余数为0时,取当前算式除数为最大公约数。

Qiuhan_909的博客 6万+

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

多项式辗转相除法求最大公约数_辗转相除法求最大公约数

辗转相除法求最大公约数约数如果数 a 能被数 b 整除,a 就叫做 b 的倍数,b 就叫做 a 的约数。最大公约数最大公约数就是两个数中,大家都能相约且最大的数。辗转相除法辗转相除法又名欧几里得算法(Euclidean algorithm),目的是求出两个正整数的最大公约数。它是已知最古老的算法,其可追溯至公元前300年前。这条算法基于一个定理:两个正整数 a 和 b(a 大于 b),它们的最大公...

weixin_42347660的博客 2896

详解“辗转相除法”(如何求最大公约数)

辗转相除法是用于求两个非负整数的最大公约数的高效方法这种方法可以不用去计算两个数谁大谁小,这样能够提高运算效率具体还是看我上面的手绘图加深一下理解。

C_Small_Cai的博客 1万+

求最大公约数之辗转相除法

文章目录一.前言二.辗转相除法原理三.用C语言求最大公约数 一.前言 最大公约数为两个及其以上的整数中约数最大的一个。 也称为最大公因子,最大公因数。a,b的最大公约数记为(a,b),同样的,a,b,c的最大公约数记为(a,b,c),多个整数的最大公约数也有同样的记号。求最大公约数有多种方法,常见的有质因数分解法、短除法、辗转相除法、更相减损法。与最大公约数相对应的概念是最小公倍数,a,b的最小公倍数记为[a,b]。 如果a能被b整除,那么a是b的倍数,b是a的约数。 二.辗转相除法原理 相信大家对辗转

weixin_57023347的博客 6277

辗转相除法求两数的最大公约数

1.最大公约数也叫最大公因数、最大公因子 2.记法:a,b两数最大公约数记为(a, b),对应两数最小公倍数记为[a, b]; 3.求最大公约数常见方法:质因数分解法、短除法、辗转相除法(又叫欧几里得算法)、更相减损法 这里重点介绍辗转相除法求两数的最大公约数,总结一句话就是: 用除数和余数反复做除法运算,当余数为 0 时,取当前算式除数为最大公约数。 例子:求1997 和 615 两个正...

谢少军的博客 1万+

辗转相除法求最大公约数

辗转相除法又称为欧几里得算法,是用来求两个整数的最大公约数的,其过程是先用大数除去小数得到余数,若余数为0则除数为最大公约数,余数不为0则将上一轮的除数作为被除数除余数,再判断余数,若为0 则除数为最大公约数,否则重复上述操作,直到余数为0,则最后一个除数就是最大公约数。代码如下: int a,b,m,n; printf("输入两个数字:\n"); scanf("%d %d",&a,...

weixin_43482231的博客 1万+

python用辗转相除法求最大公约数

辗转相除法:(1)比较两数,并使m>n(2)将m作被除数,n做除数,相除后余数为r(3)循环判断r,若r==0,则n为最大公约数,结束循环。若r !=0 ,执行m=n,n=r;将m作被除数,n做除数,相除后余数为r运行代码如下:num1 = int(input("请输入第一个数字:")) num2 = int(input("请输入第一个数字:")) m = max(num1, num2) n =

康雨城 5万+

辗转相除法为什么能够得到最大公约数

GCD(Greatest Common Divisor最大公约数) 数学原理 number = divisor * coefficient 假设a = 45, b = 27,求a与b的GCD a = 45 = 9 * 5 = divisor * m b = 27 = 9 * 2 = divisor * n reminder = a % b = 18 = 9 * 2 = divisor * k 由r...

爱吃萝卜的小青菜 2976

最大公约数 辗转相除的原理

最大公约数:指两个或多个整数共有约数中最大的一个。Go代码如下: package main import "fmt" func gcd(x, y int) int { for y != 0 { x, y = y, x%y } return x } func main() { fmt.Print(gcd(14, 8)) } 原理如下: 假设有两个数x和y,存在一个最大公约...

SanfordZhu的博客 1117

辗转相除法求最大公约数的原理

辗转相除法通过不断地将问题规模缩小(用较小的数代替较大的数),直到余数为零,从而高效地计算出两个数的最大公约数。这种方法不仅简单易懂,而且在计算机算法中非常实用,因为其时间复杂度相对较低,通常为Olog⁡min⁡abOlogminab)))。

hanqing1972的博客 1245

辗转相除法求解最大公约数和最小公倍数

题外话 今天在某C语言视频上看到一条很有意思的评论,说“写程序哪是在敲代码,是在搞数学啊!” 这话也确实有点道理,毕竟一个程序里面包含了很多很多的数学知识,包括学习算法、数据结构时,都需要数学理论的支撑,但是我们得清楚一点:对于程序员来说,学好数学的确很重要,但是探究数学原理、证明为什么不是我们该干的事,我们只需要运用原理,实现我们的程序就行了。 所以说,搞清楚重点,我们应该做的是什么,这样才能学好。 最大公约数 要求最大公约数有很多方法:穷举法、辗转相除法、递归、 ...

SAKURAjinx的博客 6733
上一篇: 数字之和
下一篇: 害死人不偿命的(3n+1)猜想/xxx定律
菜菜2022
博客等级 码龄9年 67粉丝 1033原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值