#33066: 暴力解


howie13579 (技職水龍頭)

學校 : 國立臺灣科技大學
編號 : 131965
來源 : [223.137.239.85]
最後登入時間 :
2024-03-15 13:51:51
a158. 11827 - Maximum GCD -- UVa11827 | From: [223.137.163.82] | 發表日期 : 2022-11-26 02:41

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

/******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute i
*******************************************************************************/
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>

int re = 0;
void gcd(int a, int b)
{


    if (b == 0)
    {
        re = a;
    }
    else {
        gcd(b, a % b);
    }
}

int main()
{
    bool space = false;
    int n = 0, count = 0, m = 0, output = 0;
    char input[100000] = { '\0' };
    int num[100000] = { 0 };
    char temp[100000] = { '\0' };
    scanf("%d", &n);
    getchar();

    while (n)
    {
        gets_s(input);
        for (int i = 0; i < strlen(input); i++)
        {
            if (input[i] == ' ')//&&space==false)
            {

                num[m] = atoi(temp);
                m++;
                memset(temp, '\0', sizeof(temp));
                count = 0;
                space = true;
            }
            else// if(input[i]!=' ')
            {
                temp[count] = input[i];
                count++;
            }
        }

        num[m] = atoi(temp);
        m++;
        memset(temp, '\0', sizeof(temp));


        for (int i = 0; i < m - 1; i++)
        {
            for (int j = i + 1; j < m; j++)
            {
                if (num[i] != 0 && num[j] != 0)
                {
                    gcd(num[i], num[j]);
                    if (re > output)
                    {
                        output = re;
                    }
                }
            }
        }


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

        re = 0;
        output = 0;
        count = 0;
        n--;
        m = 0;
        memset(input, '\0', sizeof(input));
        memset(num, 0, sizeof(num));
    }

    return 0;
}

 
ZeroJudge Forum