Problem F: 最大公约数、最小公倍数
Description
输入两个正整数m和n,输出m、n的最大公约数和最大公倍数。先计算最大公约数,m和n得乘积除以最大公约数,就得到了最小公倍数。其中最大公约数可以用穷举法求得,也可以用辗转相除法求得。
Input
两个正整数m和n,空格隔开
Output
m、n的最大公约数和最小公倍数。
提示:一般地说,求最小公倍数用两个数的积除以最大公约数即可,而求最大公约数有多种算法:
1. 穷举法1,从1开始直到较小的数,每个数进行判断,如果是公约数就保留此数到变量max中,最后留下的就是最大公约数
2.穷举法2,从较小数由大到小列举,直到找到公约数立即中断列举,得到的公约数便是最大公约数。
3.辗转相除法,又名欧几里德算法,是计算最大公约数和最小公倍数的重要方法,比穷举法简便得多。
主要过程是设两数为a,b,
1)a除以b得余数àc;
2)如果c不等于0则:
bàa,càb,回到1);
Sample Input
12 48
20 12
Sample Output
12 48
4 60
#include<stdio.h>
int fact(int a, int b)
{
if (b == )
{
return a;
}
else
{
return fact(b, a% b);
}
}
int main()
{
int m,n,max,min;
while(scanf("%d %d",&m,&n)!=EOF)
{
max=fact(m,n);
min=m*n/max;
printf("%d %d\n",max,min);
}
return ;
}
实现辗转相除法通常有两种思路,分别如下
1、使用循环实现
function gcd(number1, number2){
// 创建一个表示余数的变量
var remainder = ;
// 通过循环计算
do {
// 更新当前余数
remainder = number1 % number2;
// 更新数字1
number1 = number2;
// 更新数字1
number2 = remainder;
} while(remainder !== );
return number1;
}
2、使用函数递归
function gcd(number1, number2) {
if (number2 == ) {
return number1;
} else {
return gcd(number2, number1 % number2);
}
}
Problem F: 最大公约数、最小公倍数的更多相关文章
- 实验12:Problem F: 求平均年龄
Home Web Board ProblemSet Standing Status Statistics Problem F: 求平均年龄 Problem F: 求平均年龄 Time Limit: ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem F
Problem F Funny Car Racing There is a funny car racing in a city with n junctions and m directed roa ...
- Codeforces Gym 100500F Problem F. Door Lock 二分
Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...
- Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...
- Codeforces Gym 100286F Problem F. Fibonacci System 数位DP
Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...
- Problem F: Exponentiation
Problem F: ExponentiationTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 4 Solved: 2[Submit][Status][W ...
- Problem F: 合唱比赛开始了!
Problem F: 合唱比赛开始了! Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 440 Solved: 201[Submit][Status][ ...
- 几何入门合集 gym101968 problem F. Mirror + gym102082 Problem F Fair Chocolate-Cutting + gym101915 problem B. Ali and Wi-Fi
abstract: V const & a 加速 F. Mirror 题意 链接 问题: 有n个人在y=0的平面上(及xoz平面).z=0平面上有一面镜子(边平行于坐标轴).z=a平面上有q个 ...
- Problem F Plug It In!
题目链接:https://cn.vjudge.net/contest/245468#problem/F 大意:给你插座和电器的对应关系,有多个电器对应一个插座的情况,但是一个插座只能供一个电器使用,现 ...
随机推荐
- HDU 1148 Rock-Paper-Scissors Tournament (模拟)
题目链接 Problem Description Rock-Paper-Scissors is game for two players, A and B, who each choose, inde ...
- hdu 1498 50 years, 50 colors(二分匹配_匈牙利算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1498 50 years, 50 colors Time Limit: 2000/1000 MS (Ja ...
- jQuery实现简单前端搜索功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Neuroph studio 入门教程
PERCEPTRON Perceptron is a simple two layer neural network with several neurons in input layer, and ...
- 关于select联动的两种做法
第一种方法: function dong(){ var getSheng = document.getElementById("sheng"); var get ...
- 21.Merge Two Sorted Lists---《剑指offer》面试17
题目链接:https://leetcode.com/problems/merge-two-sorted-lists/description/ 题目大意: 给出两个升序链表,将它们归并成一个链表,若有重 ...
- 头像截图上传三种方式之一(一个简单易用的flash插件)(asp.net版本)
flash中有版权声明,不适合商业开发.这是官网地址:http://www.hdfu.net/ 本文参考了http://blog.csdn.net/yafei450225664/article/det ...
- 如何删除git远程分支(转)
1,在开发过程中,大家在远程创建了许多分支,有些是无用的,该如何删除呢,可以参考下面的方法. 如果不再需要某个远程分支了,比如搞定了某个特性并把它合并进了远程的 master 分支(或任何其他存放 稳 ...
- PSQueue队列操作
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈(FILO,First In Last Out,先进后出)属于线性表一样,队 ...
- POJ 2431 Expedition (贪心 + 优先队列)
题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...