题目地址:http://ac.jobdu.com/problem.php?pid=1056

题目描述:

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

输入:

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

输出:

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

样例输入:
49 14
样例输出:
7
来源:
2011年哈尔滨工业大学计算机研究生机试真题
#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 【辗转相除法】的更多相关文章

  1. 九度OJ 1056:最大公约数 (GCD)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6278 解决:4075 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. 输出: 对于每组输入,请 ...

  2. 【九度OJ】题目1056:最大公约数 解题报告

    [九度OJ]题目1056:最大公约数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1056 题目描述: 输入两个正整数,求 ...

  3. 【九度OJ】题目1439:Least Common Multiple 解题报告

    [九度OJ]题目1439:Least Common Multiple 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1439 ...

  4. 【九度OJ】题目1438:最小公倍数 解题报告

    [九度OJ]题目1438:最小公倍数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1438 题目描述: 给定两个正整数,计 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  7. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  8. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  9. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  10. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

随机推荐

  1. readonly disabled 区别

    readonly 提交表单时包含该属性的内容 控件 disabled 不包含该属性

  2. Android studio插件安装

    Android Studio安装插件的方式其实和Eclipse大同小异.废话不多说,直接上图: 区域1:你当前已经安装了的插件 区域2:在线安装 区域3:从硬盘安装,即针对你已经下载好了的插件,可通过 ...

  3. 文件控制列表命令setfacl和getfacl的使用

    一 需求 有以下需求,通过setfacl命令实现 一组用户可写可读可执行,一组用户可写可执行,另一组用户只可读 linux rwx oracle wx uplook r 二 解决 第一步 添加六个用户 ...

  4. DBHelper数据库操作类(二)

    不错文章:http://www.codefans.net/articles/562.shtml http://www.cnblogs.com/gaobing/p/3878342.html using ...

  5. Xcode repository host is unreachable

    遇到这个错误,首先不要急.按照如下方法即可(如果你的svn地址没有问题的话): url要使用域名,所以映射下 1. 修改host:在应用程序里面打开终端(terminal),输入 sudo vi /e ...

  6. windows8 认识及使用

    windows8的一次技术分享. 利用国庆宅家的几天,在跑不动XP的老笔记本上装了win8,嘿,跑的溜溜的,一高兴做个ppt给公司的同事们介绍介绍,随意之作,勿较真抬杠,呵呵. 文件地址:http:/ ...

  7. 小谈一下Java I/O

    java中的I/O操作宏观上来说就氛围两个种类,一个是字节流 ,一个是字符流,分别相应着一组类和接口 字节流: InputStream 输入流的基类 OutputStream 输出流的基类 对于字节流 ...

  8. cocos2d-x读取xml(适用于cocos2d-x 2.0以上版本号)

    为了能在cocos2d-x的文本标签中显示中文,一个是转换文件编码格式,还有一种就是读取utf-8格式的xml文件.我选择了后者,其原因大家可以去搜索一下cocos2d-x显示中文,希望可以你给答案. ...

  9. Shell脚本文件操作

    Linux Shell http://baike.baidu.com/link?url=2LxUhKzlh5xBUgQrS0JEc61xi761nvCS7SHJsa1U1SkVbw3CC869AoUC ...

  10. windows身份验证模式和SQL server身份验证模式 有什么不同

    两个验证方式是有明显不同的. 主要集中在信任连接和非信任连接. windows 身份验证相对于混合模式更加安全,使用本连接模式时候,sql不判断sa密码,而仅根据用户的windows权限来进行身份验证 ...