PAT B -1062 minimalist score (20 points)

Click on the link full solution summary PAT B -AC

Title:
a fraction of the division of two integers is generally written in the form: 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 listed therebetween denominator is 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

My code:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#include<sstream>
using namespace std;
//有的时候题目是一起做的,所以会有不需要的头文件
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    int fenzi1,fenmu1,fenzi2,fenmu2,K;
    bool first=true;
    scanf("%d/%d %d/%d %d",&fenzi1,&fenmu1,&fenzi2,&fenmu2,&K);
    if(1.0*fenzi1/fenmu1>1.0*fenzi2/fenmu2)
    {
        int t=fenzi1;
        fenzi1=fenzi2;
        fenzi2=t;
        t=fenmu1;
        fenmu1=fenmu2;
        fenmu2=t;
    }

    for(int i=1;i<K;i++)
    {
        if(gcd(i,K)==1 && 1.0*i/K > 1.0*fenzi1/fenmu1 && 1.0*i/K < 1.0*fenzi2/fenmu2)
        {
            if(!first)cout<<" ";
            printf("%d/%d",i,K);
            first=false;
        }
    }
    return 0;
}
Published 82 original articles · won praise 1 · views 1673

Guess you like

Origin blog.csdn.net/qq_34451909/article/details/104902122
Recommended