PAT B brush title Road 1062 Minimalist score (20 points)

1062 Minimalist score (20 points)

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 fraction N1 / M1 and N2 / M2, requires you to press are listed in order from smallest to largest denominator between them is the most simple fraction K's.

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

Focus on the use of the Euclidean algorithm to find the common denominator and two scores the least common multiple of K, the numerator and denominator can find a common denominator between two fractions of common denominator is K

#include <iostream> 
#include <vector> 
using namespace std;

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

int main()
{
	int N1,M1,N2,M2,K;
	scanf("%d/%d %d/%d %d",&N1,&M1,&N2,&M2,&K);
	int gcd1=gcd(M1,M2);//分母的最大公约数
	int m1=M1*M2/gcd1;//求出通分后的分母
	int gcd2=gcd(m1,K);//求出通分后的分母与K的最大公约数
	int	m2=m1*K/gcd2;//求出通分后的分母
	int tmp=m2/K;//通分后的分母与K的最小公倍数
	int n1=N1*m2/M1;//分子通分
	int n2=N2*m2/M2;//分子通分
	int i;
	vector<int> N;
    //找两者间的分母分子的最大公约数为K的
	if(n1<n2){
		for(i=n1+1;i<n2;i++){
			if(gcd(i,m2)==tmp){
				N.push_back(i/tmp);
			}
		}
	}else{
		for(i=n2+1;i<n1;i++){
			if(gcd(i,m2)==tmp){
				N.push_back(i/tmp);
			}
		}
	}
	for(i=0;i<N.size();i++){
		printf("%d/%d",N[i],K);
		if(i<N.size()-1){
			printf(" ");
		}
	}
	return 0;
}
Published 73 original articles · won praise 0 · Views 526

Guess you like

Origin blog.csdn.net/derbi123123/article/details/103827959