[Machine Test exercises]55. Greatest common Divisor & multiple number of greatest common divisor

Source: Internet
Author: User
Tags greatest common divisor



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&GT;&GT;A&GT;&GT;B) {cout&LT;&LT;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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.