PAT 1062 B-fraction /// /// The method of seeking the least common multiple number of three Euclidean greatest common divisor

Typically written as a fraction of an integer division of the two forms: N / M, where M is not 0. The most simple fraction is the fraction of the numerator and denominator divisor no representation.
Now given two unequal positive fractional N . 1 / M . 1 and N 2 / M 2 , ask you in ascending order of the denominator between them are listed in the K-fraction.

Input formats:
on a single line is given by N / M of the two positive score format, then the denominator is a positive integer K, separated by a space therebetween. All topics integer guarantee given no more than 1,000.

Output formats:
in the format listed in row N / M of the denominator of the fraction between two given all the K-fraction, in ascending order, separated by a space therebetween. Line from beginning to end may not have the extra space. Title ensure that at least one output.

Sample input:
7/18 13/20 12

Sample output:
5/12 7/12

Ideas: the sum of two as the denominator and a number k of the three largest common denominator as multiples, such as the subject to be the least common multiple of 180 sample 18,20,12. Numerator of the fraction is then amplified into multiples 70/180 117/180, 15 can then find (180 except 12) divisible (15 divisible be described denominator simplify to make 12) between 70-117 figures sort into the container, and then output, note that the output condition of the molecule must be met and 12 prime (i.e., the greatest common divisor is 1)

After reading a few explanations will be found in two pits blog:

  1. Note that the title to the two scores is not necessarily small, large, first ordering the right to the left (which I took into account)
  2. This pit is not the cause of test points by 2, pay attention to the subject output requirements listed in the denominator of the fraction between two given to all K-fraction , between scores draw focus. So the first for loop written judgment for (int i = min; i <= max; i++)is wrong. I was wrong on this point, or to examine the topic ah
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int gcd(int m, int n); //求最大公约数
int getmin(int x, int y, int z); //求最小公倍数
int getmax(int a, int b, int c); //求三个数中的最大数
int main()
{
	int n1, m1, n2, m2, k;
	scanf("%d/%d %d/%d %d", &n1, &m1, &n2, &m2, &k);
	int num = getmin(m1, m2, k); //num是最小公倍数
	int n = num / k;
	n1 *= (num / m1); //将两个分数的分子按倍数放大
	n2 *= (num / m2);
	int min = n1 > n2 ? n2 : n1; //min取分子中最小的,max取最大的
	int max = n1 > n2 ? n1 : n2;
	vector<int> vec;
	for (int i = min + 1; i < max; i++) //注意条件!!!
	{
		if (i % n == 0 && gcd(i / n, k) == 1) //能被n整除,且除了之后和k互质
			vec.push_back(i / n);
	}
	sort(vec.begin(), vec.end()); //排序
	for (int i = 0; i < vec.size(); i++)
	{
		if (i != 0) printf(" ");
		printf("%d/%d", vec[i], k);
	}
	return 0;
}
int getmax(int a, int b, int c)
{
	int m = a > b ? a : b;
	m = m > c ? m : c;
	return m;
}
int getmin(int x, int y, int z)
{
	int i = 1, j, k;
	k = getmax(x, y, z);
	while (true)
	{
		j = k * i;
		if ((j % x == 0) && (j % y == 0) && (j % z == 0))
			break;
		i++;
	}
	return j;
}
int gcd(int a, int b)
{
	int c;
	while (a != 0)
	{
		c = b % a;
		b = a;
		a = c;
	}
	return b;
}

Attach: three minutes appreciated Euclidean
both tags:

int gcd(int a, int b) //迭代
{
	int c;
	while (a != 0)
	{
		c = b % a;
		b = a;
		a = c;
	}
	return b;
}
int gcb(int a,int b) //递归
{
    return a % b ? gcb(b, a % b) : b;
}

You do not know find the least common multiple of three numbers Solution

Published 101 original articles · won praise 1 · views 1953

Guess you like

Origin blog.csdn.net/weixin_43318827/article/details/105364605