2018四川省省赛H.Harmony(考察英语水平的水题)three

Titles:

    The value between two participants is defined as the GCD of their ability value. Now your two teammates' ablitily values x,y are given. You can change your ability value as you like.You are required to calculate the maximum sum of the values among each two of your team.

Constraints:

1 <= T <= 20  

1 <= x,y <= 10^8

Example:

standard input:                                              standard output:

2                                                                                    

4    6                                                         12

14   15                                                        30

解题思路:

    as you like.

GCD(greatest common divisor):辗转相除法又叫欧几里得算法

    1.首先利用辗转相除法时,要先确保x > y.

    2.证明辗转相除法:

因为x = k * y + b;   假设x和y的最小公约数为m,那么x | m && y | m;

又因为b = x - k * y,所以 b | m;故:gcd(x , y) == gcd(y , b);

当且仅当b == 0时,结束递归。

int gcd(int x,int y)
{
	if(x % y == 0)
		return y;
	else
		return gcd(y,x % y);
}

实现代码:

#include
#include
#include
#include
using namespace std;

int gcd(int x,int y)
{
	if(x % y == 0)
		return y;
	else
		return gcd(y,x % y);
}

int main()
{
	int t,n,m;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&m);
		printf("%d\n",n + m + gcd(n,m));
	}
	return 0;
}
欢迎大家观看我的博客,希望能够帮助到大家,如果觉得不错,请给我点赞鼓励鼓励我!

你可能感兴趣的:(紫书)