This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Author : Md.Mehadi Hasan Menon. | |
* Problem : 11827 - Maximum GCD. | |
* Status : Accepted. | |
* Date : 08.10.2014. | |
**/ | |
#include <stdio.h> | |
#define MAX(a, b) (a > b) ? a : b | |
int Gcd_Of_Two_Number_Is(int a, int b); | |
int main() | |
{ | |
char buff[105]; | |
int num[105]; | |
int i, j, k, tCase, maxGcd; | |
scanf("%d\n", &tCase); | |
while(tCase--) | |
{ | |
gets(buff); | |
i = 0; j = 0; | |
while(buff[i]) | |
{ | |
/* Take each number form buffer and keep it to the num[] array */ | |
num[j] = 0; | |
while(buff[i] && buff[i] != ' ') | |
{ | |
num[j] = num[j] * 10 + buff[i++] - '0'; | |
} | |
/* Skipe the space character from buffer */ | |
while(buff[i] == ' ') | |
{ | |
i++; | |
} | |
j++; | |
} | |
/* This is the most important line of the code . | |
If we get space character at first position of string | |
than we will skip num[j] = 0 value from the num Array */ | |
i = ( buff[0] == ' ' ) ? 1 : 0; | |
maxGcd = 0; | |
/* Find maximum GCD for all possible pair from num[] array */ | |
for(; i < j - 1; i++) | |
{ | |
for(k = i + 1; k < j; k++) | |
{ | |
maxGcd = MAX( maxGcd, Gcd_Of_Two_Number_Is(num[i], num[k]) ); | |
} | |
} | |
printf("%d\n", maxGcd); | |
} | |
return 0; | |
} | |
/* This function finds GCD of two numbrs .This is Uclide mathod */ | |
int Gcd_Of_Two_Number_Is(int a, int b) | |
{ | |
if(b == 0) return a; | |
return Gcd_Of_Two_Number_Is(b, a % b); | |
} |
Comments
Post a Comment