PTA---1062 最簡分數(C語言+測試點1)

2020-08-13 16:40:55

PTA—1062 最簡分數

題目直達鏈接點我

測試點1

題目沒說第一個給出的就是小的那個,後邊那個就是大的那個區間,so需要後續判斷…

AC程式碼

#include <stdio.h>
int not_have(int a,int b)				//看是否有最小公倍數 
{
	if(a == b)
	return 0;
	
	int x = b,y = a,z = 1;
	if(a > b)
	x = a,y = b;
	
	while(z)
	{
		z = x%y;
		x = y;
		y = z;
	}
	
	if(x == 1)
	return 1;
	return 0;
}
void swap(int &a,int &b)
{
    int x = a;
    a = b;
    b = x;
}
int main()
{
    int n1,m1,n2,m2;
    scanf("%d/%d %d/%d",&n1,&m1,&n2,&m2);
    if(n1/(double)m1 > n2/(double)m2)
    {
        swap(n1,n2);
        swap(m1,m2);
    }
    
    int k,i,j;
    scanf("%d",&k);
    if(n1 == 0)
    {
        int sum = 0;
        for(i=1;i*m2<k*n2;i++)
            if(not_have(i,k))
                printf(sum==0?"%d/%d":" %d/%d",i,k),sum++;
        return 0;
    }
    else if(n2 == 0)
        return 0;
        
    int sum=0;
	i=1;
    while(i*m1 <= n1*k)
    	i++;
    
    for(;i*m1 > n1*k && i*m2 < k*n2;i++)
        if(not_have(i,k))
            printf(sum==0?"%d/%d":" %d/%d",i,k),sum++;
    return 0;
}

---------------------------------------------------------------------------------------end