Topic
Describe:
输入2个数字,最后输出2个数字的最大公约数
Topic Category:
Difficulty:
Run time limit:
无限制
Memory Limit:
无限制
Stage:
Input:
2个整数
Output:
输出数字1和2的最大公约数
Sample input:
2 3
Sample output:
1
Code
/* ---------------------------------------* Date: 2015-07-05* sjf0115* title: Greatest Common Divisor * Source: Machine Test Exercises---------------------- -------------------*/#include <iostream>#include <vector>using namespace STD;//Greatest common divisorintGCD (intAintb) {if(b = =0){returnA }//if returnGCD (b,a%b);}intMain () {intb;//freopen ("C:\\users\\administrator\\desktop\\c++.txt", "R", stdin); while(Cin>>A>>B) {cout<<GCD (b) <<endl; }//while return 0;}
Title [Number of greatest common divisor]
Topic:
多个数的最大公约数
Describe:
输入一组正整数(数量小于20),输出其最大公约数。
Input:
121 33 44 11 1111
Output:
11
Basic ideas:
从第一个数开始,和第二个数比较找它两的最大公约数,然后找出的最大公约数和第三个数比较,依次类推
Code
/* ---------------------------------------* Date: 2015-07-05* sjf0115* title: Number of Greatest common divisor * Source: Machine Test Exercises------------------- ----------------------*/#include <iostream>#include <vector>using namespace STD;//Greatest common divisorintGCD (intAintb) {if(b = =0){returnA }//if returnGCD (b,a%b);}intMain () {intN//freopen ("C:\\users\\administrator\\desktop\\c++.txt", "R", stdin); while(Cin>>n) {intNum,result; for(inti =0; i < n;++i) {Cin>>num;if(i = =0{result = num; }//if Else{result = GCD (result,num); }//else}//for cout<<result<<endl; }//while return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Machine Test exercises]55. Greatest common Divisor & multiple number of greatest common divisor