Huawei Machine Test-Multiple number of greatest common divisor

Source: Internet
Author: User
Tags greatest common divisor

Title: Seeking Greatest common divisor

Enter a set of positive integers (less than ), outputting its greatest common divisor.

Input: 121 33 44 11 1111

Output: one


Basic ideas:

Start with the first number, compare it to the second number, and then find the greatest common divisor of the greatest common divisor and the third number, and so on.


#include <stdlib.h> #include <stdio.h>int a[30];int gcd (int a,int b) {return b==0?a:gcd (b,a%b);} int main () {    freopen ("a.in", "R", stdin);    int n;    int i=-1;    while (~SCANF ("%d", &a[++i]))        ;    n=i;    int t;    for (int j=1;j<n;++j)    {        t=gcd (a[0],a[j]);        a[0]=t;    }    printf ("%d\n", t);    return 0;}


Huawei Machine Test-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.