例10        最大公约数

问题描述

有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b。若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c。

输入数据

第一行输入一个n,表示有n组测试数据,接下来的n行,每行输入两个正整数a,b。

输出格式

输出对应的c,每组测试数据占一行。

输入样例

2

6 2

12 4

输出样例

4

8

(1)编程思路。

利用转辗相除法求两个整数的最大公约数。例如,求整数m=48,n=18两个数的最大公约数的方法如左图所示。

具体做法是:,若m%n==0,则n是最大公约数,否则,计算 r=m%n,置m=n,n=r,重复这个过程,直到m%n==0。

将求整数m和n的最大公约数定义为函数

int gcd(int m,intn); 。

在本题中,由于b是a、c的最大公约数,且c!=b,所以对c=2*b、3*b…进行穷举判断即可直到最小的满足条件的c。

(2)源程序。

#include <stdio.h>

int gcd(int m, int n)

{

int r;

while(m%n!=0)

{

r=m%n;

m = n;

n = r;

}

return n;

}

int main()

{

int t,a,b,c;

scanf("%d",&t);

while(t--)

{

scanf("%d%d",&a,&b);

c=2*b;

while(gcd(a,c)!=b)

c+=b;

printf("%d\n",c);

}

return 0;

}

习题10

10-1  Uniform Generator

本题选自杭州电子科技大学OJ题库 (http://acm.hdu.edu.cn/showproblem.php?pid=1014)

Problem Description

Computer simulations often require random numbers. One way to generate pseudo-random numbers is via a function of the form

seed(x+1) = [seed(x) + STEP] % MOD

where '%' is the modulus operator.

Such a function will generate pseudo-random numbers (seed) between 0 and MOD-1. One problem with functions of this form is that they will always generate the same pattern over and over. In order to minimize this effect, selecting the STEP and MOD values carefully can result in a uniform distribution of all values between (and including) 0 and MOD-1.

For example, if STEP = 3 and MOD = 5, the function will generate the series of pseudo-random numbers 0, 3, 1, 4, 2 in a repeating cycle. In this example, all of the numbers between and including 0 and MOD-1 will be generated every MOD iterations of the function. Note that by the nature of the function to generate the same seed(x+1) every time seed(x) occurs means that if a function will generate all the numbers between 0 and MOD-1, it will generate pseudo-random numbers uniformly with every MOD iterations.

If STEP = 15 and MOD = 20, the function generates the series 0, 15, 10, 5 (or any other repeating series if the initial seed is other than 0). This is a poor selection of STEP and MOD because no initial seed will generate all of the numbers from 0 and MOD-1.

Your program will determine if choices of STEP and MOD will generate a uniform distribution of pseudo-random numbers.

Input

Each line of input will contain a pair of integers for STEP and MOD in that order (1 <= STEP, MOD <= 100000).

Output

For each line of input, your program should print the STEP value right- justified in columns 1 through 10, the MOD value right-justified in columns 11 through 20 and either "Good Choice" or "Bad Choice" left-justified starting in column 25. The "Good Choice" message should be printed when the selection of STEP and MOD will generate all the numbers between and including 0 and MOD-1 when MOD numbers are generated. Otherwise, your program should print the message "Bad Choice". After each output test set, your program should print exactly one blank line.

Sample Input

3 5

15 20

63923 99999

Sample Output

3         5    Good Choice

15        20    Bad Choice

63923     99999    Good Choice

(1)编程思路。

题目的意思是:输入两个整数x和y,若x与y互质,则输出“Good Choice”;否则输出“Bad Choice”。

若两个整数x和y的最大公约数为1,则x与y互质。

(2)源程序。

#include <stdio.h>

int gcd(int a, int b)

{

int r;

while(a%b!=0)

{

r=a%b;

a = b;

b = r;

}

return b;

}

int main()

{

int x, y;

while(scanf("%d %d", &x, &y) != EOF)

{

if(gcd(x, y) == 1)

printf("%10d%10d    Good Choice\n\n", x, y);

else

printf("%10d%10d    Bad Choice\n\n",x, y);

}

return 0;

}

10-2  Party

本题选自北大POJ题库 (http://poj.org/problem?id=3970)

Description

The CEO of ACM (Association of Cryptographic Mavericks) organization has invited all of his teams to the annual all-hands meeting, being a very disciplined person, the CEO decided to give a money award to the first team that shows up to the meeting.

The CEO knows the number of employees in each of his teams and wants to determine X the least amount of money he should bring so that he awards the first team to show up such that all team members receive the same amount of money. You must write a program to help the CEO achieve this task.

Input

The input consists of multiple test cases, each test case is described on a line by itself, Each line starts with an integer N (1 <= N <= 20) the number of teams in the organization followed by N space separated positive integers representing the number of employees in each of the N teams. You may assume that X will always fit in a 32 bit signed integer. The last line of input starts with 0 and shouldn't be processed.

Output

For each test case in the input print "The CEO must bring X pounds.", where X is as described above or "Too much money to pay!" if X is 1000000 or more.

Sample Input

1 3000000

2 12 4

0

Sample Output

Too much money to pay!

The CEO must bring 12 pounds.

(1)编程思路。

题意是求输入的n个正整数的最小公倍数。设正整数x与y的最大公约数为gcd(x,y),则x与y的最小公倍数为x*y/gcd(x,y)。

(2)源程序。

#include <stdio.h>

int lcm(int x,int y)

{

int r,a,b;

a=x; b=y;

while (a%b!=0)

{

r=a%b;

a=b;

b=r;

}

return x*y/b;

}

int main()

{

int n,i,x0,x1;

while(scanf("%d",&n) && n!=0)

{

scanf("%d",&x0);

for (i=2;i<=n;i++)

{

scanf("%d",&x1);

x0=lcm(x0,x1);

}

if (x0>=1000000)

printf("Too much money to pay!\n");

else

printf("The CEO must bring %d pounds.\n",x0);

}

return 0;

}

10-3  相遇周期

题目描述

已知两颗卫星的运行周期,求它们的相遇周期。

输入

输入数据的第一行为一个正整数T,表示测试数据的组数,然后是T组测试数据,每组测试数据包含两组正整数,用空格隔开。每组包含两个正整数,表示转n圈需要的天数(26501/6335,表示转26501圈要6335天),用'/'隔开。

输出

对于每组测试数据, 输出它们的相遇周期,如果相遇周期是整数则用整数表示,否则用最简分数表示。

输入样例

2

26501/6335  18468/42

29359/11479  15725/19170

输出样例

81570078/7

5431415

(1)编程思路。

输入的每个分数就是一个卫星的周期。求两个卫星的相遇周期,即求二者周期的最小公倍数。可以先把两个分数通分,找出通分后两个分子的最小公倍数,再除以通分后的分母,即得相遇周期(最小公倍数)。

(2)源程序。

#include <stdio.h>

long long gcd(long long m, long long n)

{

long long r;

while(m%n!=0)

{

r=m%n;

m = n;

n = r;

}

return n;

}

int main()

{

int t;

long long a,b,c,d,e,f,n,m,k;

scanf("%d",&t);

while(t--)

{

scanf("%lld/%lld%lld/%lld",&a,&b,&c,&d);

e=b*c;

f=a*d;

m=b*d;          //通分

n=gcd(f,e);

n=f/n*e;

if(n%m==0)     //能除整

printf("%lld\n",n/m);

else           //不能整除,要化简后,输出分数

{

k=gcd(m,n);  //求分子分母最大公约数

printf("%lld/%lld\n",n/k,m/k);

}

}

return 0;

}

C语言程序设计100例之(10):最大公约数的更多相关文章

  1. 黑马程序员——经典C语言程序设计100例

    1.数字排列 2.奖金分配问题 3.已知条件求解整数 4.输入日期判断第几天 5.输入整数进行排序 6.用*号显示字母C的图案 7.显示特殊图案 8.打印九九口诀 9.输出国际象棋棋盘 10.打印楼梯 ...

  2. C语言程序设计100例之(4):水仙花数

    例4    水仙花数 题目描述 一个三位整数(100-999),若各位数的立方和等于该数自身,则称其为“水仙花数”(如:153=13+53+33),找出所有的这种数. 输入格式 没有输入 输出格式 若 ...

  3. C语言程序设计100例之(23):数列求和

    例23  数列求和 问题描述 已知某数列前两项为2和3,其后继项根据前面最后两项的乘积,按下列规则生成: ① 若乘积为一位数,则该乘积即为数列的后继项: ② 若乘积为二位数,则该乘积的十位上的数字和个 ...

  4. C语言程序设计100例之(18):火柴棒等式

    例18   火柴棒等式 用n根火柴棍,可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棒拼出的整数(若该数非零,则最高位不能是0).用火柴棒拼数字0~9的拼法如图1所示. 图1  用 ...

  5. C语言程序设计100例之(16):巧解算式

    例16  巧解算式 问题描述 在1.2.3.4.5.6.7.8.9.10个数中间加上加号或减号,使得到的表达式的值为自然数N,如果中间没有符号,则认为前后为一个数,如1 2 3认为是一百二十三(123 ...

  6. C语言程序设计100例之(26):二进制数中1的个数

    例26   二进制数中1的个数 问题描述 如果一个正整数m表示成二进制,它的位数为n(不包含前导0),称它为一个n位二进制数.所有的n位二进制数中,1的总个数是多少呢? 例如,3位二进制数总共有4个, ...

  7. C语言程序设计100例之(22):插入排序

    例22  插入排序 问题描述 排序是计算机程序设计中的一种重要操作,它的功能是将一个数据元素或记录的任意序列,重新排列成一个以关键字递增(或递减)排列的有序序列. 排序的方法有很多,简单插入排序就是一 ...

  8. C语言程序设计100例之(9):生理周期

    例9    生理周期 问题描述 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为 23 天.28 天和33 天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如 ...

  9. C语言程序设计100例之(6):数字反转

    例6    数字反转 题目描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例2). 输入格式 ...

随机推荐

  1. 基于 Web 端 3D 地铁站可视化系统

    前言 工业互联网,物联网,可视化等名词在我们现在信息化的大背景下已经是耳熟能详,日常生活的交通,出行,吃穿等可能都可以用信息化的方式来为我们表达,在传统的可视化监控领域,一般都是基于 Web SCAD ...

  2. 关于CSS Grid Layout的代码解释

    .wrapper { display: grid; /*生成grid类型块级网格*/ grid-template-columns: repeat(3, 1fr); /*设置显示的列网格线,且重复3次1 ...

  3. 转 NAT技术详解

    NAT产生背景 今天,无数快乐的互联网用户在尽情享受Internet带来的乐趣.他们浏览新闻,搜索资料,下载软件,广交新朋,分享信息,甚至于足不出户获取一切日用所需.企业利用互联网发布信息,传递资料和 ...

  4. Neo4j:图数据库GraphDB(三)创建删除及高级操作

    本片继续前几篇介绍图数据库的创建,有疑问可以我的看看前两篇文章:http://www.cnblogs.com/rongyux/p/5537206.html 四 图数据库的创建 1 创建一个节点   P ...

  5. luoguP4779 【模板】单源最短路径

    题目描述 单源最短路径模板. 使用 SPFA 肯定是不行的啦,网格图hack. 所以我们使用 Dijkstra 算法. 这里有一篇写的很好的 blog,无必要赘述.最后贴上代码. #include&l ...

  6. NOIP2009 Hankson 的趣味题 : 数论

    题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲解 ...

  7. table表格中文字超出显示省略号

    第一步: table {table-layout:fixed:}列宽由表格宽度和列宽度设定,不随文字多少变化 第二步: td { white-space:nowrap;/*文本不会换行,文本会在在同一 ...

  8. 说说Object类下面有几种方法呢?

    今天说一道基础题型,不过很多人会忽略或者至少说不完整,但是面试时被问到的几率还是很大的. 面试题 Object有几种方法呢? Java语言是一种单继承结构语言,Java中所有的类都有一个共同的祖先.这 ...

  9. C#刷遍Leetcode面试题系列连载(5):No.593 - 有效的正方形

    上一篇 LeetCode 面试题中,我们分析了一道难度为 Easy 的数学题 - 自除数,提供了两种方法.今天我们来分析一道难度为 Medium 的面试题. 今天要给大家分析的面试题是 LeetCod ...

  10. 【阿里云IoT+YF3300】7.物联网设备表达式运算

    很多时候从设备采集的数据并不能直接使用,还需要进行处理一下.如果采用脚本处理,有点太复杂了,而采用表达式运算,则很方便地解决了此类问题. 一.  设备连接 运行环境搭建:Win7系统请下载相关的设备驱 ...