练习题

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
int GCD(int a,int b)
{
    int i,temp_gcd;
    for(i=a; i>=1; i--)
    {
        if(a%i==0){
            if(b%i==0)
                {temp_gcd=i; return temp_gcd;}
        }
    }
}
int LCM(int a,int b)
{
    int temp_lcm;
    temp_lcm=a*b/GCD(a,b); //最小公倍数等于两数之积除以最大公约数   
    return temp_lcm;
}
void search(int a,int b,int c,int d,int sum,int count,int j)
{
    int tmp;
    if(b%a==0){printf("ending : 1/%d\n",b/a);return;}
    if(count > d||sum > c)return;
    for(;count <= d&&sum*j <= c;){
    if(b%a==0){printf("ending : 1/%d\n",b/a);return;}
    if((a/b) >= (1/j)&&a >= 1){
        //printf("a/b==%d/%d    1/j==1/%d     sum==%d\n",a,b,j,sum);
        tmp=LCM(b,j);
        if(a*(tmp/b)-(tmp/j)<0)break;
        printf("1/%d\t",j);
        a=a*(tmp/b)-(tmp/j);
        b=tmp,sum*=j;
        count++;
        }
    }
    search(a,b,c,d,sum,count,j);
    return;

}

int main()
{
    int p,q,a,n,tmp;
    int x,y,sum;
    int count = 0;
    sum = 1;
    scanf("%d %d %d %d",&p,&q,&a,&n);
    for(int j = 2;j <= 100;j++)
    search(p,q,a,n,sum,count,j);
    system("pause");
    return 0;
}

posted on 2011-04-23 10:45  小影帆  阅读(138)  评论(0编辑  收藏  举报

导航