Stay Hungry,Stay Foolish!

1053. Pinocchio

1053. Pinocchio

https://acm.timus.ru/problem.aspx?space=1&num=1053

 

思路

每次取两个,进行比较,

相等则舍去一个,

不相等,则从长的里面减去短的。

 

对于不相等的情况, 则相当于将长的中多减几次短的,直到不能减为止,这就相当于求最大公约数。

 

https://www.cnblogs.com/staginner/archive/2012/05/07/2487331.html

最大公约数求发就是辗转相除法。

gcd 中对于 0 和 x 求最大公约数, 结果就是x,

因为 0 % x == 0  x % x == 0

 

#include<stdio.h>
#include<string.h>
#define MAXD 1010
int N, a[MAXD];
int gcd(int x, int y)
{
    return y == 0 ? x : gcd(y, x % y);
}
void init()
{
    int i;
    for(i = 1; i <= N; i ++)
        scanf("%d", &a[i]);
}
void solve()
{
    int i;
    for(i = 1; i <= N; i ++)
        a[i + 1] = gcd(a[i], a[i + 1]);
    printf("%d\n", a[N]);
}
int main()
{
    while(scanf("%d", &N) == 1)
    {
        init();
        solve();
    }
    return 0;
}

 

posted @ 2022-12-10 15:33  lightsong  阅读(21)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel