九度OJ 1056--最大公约数 1439--Least Common Multiple 【辗转相除法】
题目地址:http://ac.jobdu.com/problem.php?pid=1056
- 题目描述:
-
输入两个正整数,求其最大公约数。
- 输入:
-
测试数据有多组,每组输入两个正整数。
- 输出:
-
对于每组输入,请输出其最大公约数。
- 样例输入:
-
49 14
- 样例输出:
-
7
#include <stdio.h> int gcd1 (int a, int b){
if (b == 0)
return a;
else
return gcd1 (b, a % b);
} int gcd2 (int a, int b){
int tmp; while (b != 0){
tmp = a;
a = b;
b = tmp % b;
}
return a;
} int main (void){
int a, b; while (scanf ("%d%d", &a, &b) != EOF){
printf ("%d\n", gcd1 (a, b));
printf ("%d\n", gcd2 (a, b));
} return 0;
}
题目地址:http://ac.jobdu.com/problem.php?pid=1439
- 题目描述:
-
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105.
- 输入:
-
Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 ...
nm where m is the number of integers in the set and n1 ... nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.
- 输出:
-
For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.
- 样例输入:
-
2
3 5 7 15
6 4 10296 936 1287 792 1
- 样例输出:
-
105
10296
#include <stdio.h> int Gcd (int a, int b){
int tmp; while (b != 0){
tmp = a;
a = b;
b = tmp % b;
}
return a;
} int main(void){
int n;
int a;
int b;
int m; while (scanf ("%d", &n) != EOF){
while (n-- != 0){
scanf ("%d%d", &m, &a);
--m;
while (m != 0){
scanf ("%d", &b);
a = a / Gcd (a, b) * b;
--m;
}
printf ("%d\n", a);
}
} return 0;
}
参考资料:维基百科 -- 辗转相除法
九度OJ 1056--最大公约数 1439--Least Common Multiple 【辗转相除法】的更多相关文章
- 九度OJ 1056:最大公约数 (GCD)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6278 解决:4075 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. 输出: 对于每组输入,请 ...
- 【九度OJ】题目1056:最大公约数 解题报告
[九度OJ]题目1056:最大公约数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1056 题目描述: 输入两个正整数,求 ...
- 【九度OJ】题目1439:Least Common Multiple 解题报告
[九度OJ]题目1439:Least Common Multiple 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1439 ...
- 【九度OJ】题目1438:最小公倍数 解题报告
[九度OJ]题目1438:最小公倍数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1438 题目描述: 给定两个正整数,计 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
随机推荐
- 剑指OFFER之把数组排成最小的数(九度OJ1504)
题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 输入: 输 ...
- ABAP DEBUG
[Function] Command=/H Type=SystemCommand 将上面的文件推动到SAP 窗口 可以启动调试 ------------------------------------ ...
- btrace拓展工具-java应用性能诊断优化利器
Btrace是一个实时监控工具,可以无需修改应用代码(事实上它修改了字节码),来达到不可告人的秘密!这是性能调优和诊断的利器! 它可以获取应用程序代码的执行时间,他可以让你无需修改代码,帮你做时间的打 ...
- ios开发——笔记篇
:开关 BOOL isopen = !isopen; //View @property (nonatomic, assign) BOOL open;//模型属性 self.group.open = ! ...
- iOS开发——面试指导
iOS面试指导 一 经过本人最近的面试和对面试资料的一些汇总,准备记录这些面试题,以便ios开发工程师找工作复习之用,本人希望有面试经验的同学能和我同时完成这个模块,先出面试题,然后会放出答案. 1. ...
- android学习日记15--WebView(网络视图)
一.WebView 1.简述 WebView(网络视图)内置WebKit引擎,能加载显示网页,还支持JS,并且能够在Android平台使用AJAXWebView可以在布局中声明,也可以在Activit ...
- python--字符工厂函数dict()
字符工厂函数str() class str(object): """ str(object='') -> str str(bytes_or_buffer[, enc ...
- 深入理解Binder(一),从AIDL谈起
打算写一篇Binder介绍的文章,仔细想想一篇文章貌似很难厘清,于是我打算从AIDL入手,本篇先来简单介绍下AIDL的使用,然后在此基础上我们继续来研究Binder的工作原理.小伙伴们都知道,AIDL ...
- 对JavaScript对象数组按指定属性和排序方向进行排序
引子 在以数据为中心的信息系统中,以表格形式展示数据是在常见不过的方式了.对数据进行排序是必不可少的功能.排序可以分为按单个字段排序和按多个字段不同排序方向排序.单字段排序局限性较大,不能满足用户对数 ...
- ssh docker container
docker run -d -p 127.0.0.1:33301:22 centos6-ssh ssh登陆容器: ssh root@127.0.0.1 -p 33301