【PAT】B1062 最简分数(20 分)

如果了解分数运算,本题很简单。我有对分数知识进行总结
分数四则运算

#include<stdio.h>
#include<algorithm>
using namespace std;
int gongyue(int a,int b){
    return !b?a:gongyue(b,a%b);
}
struct fenshu{
    int fenzi;
    int fenmu;
}shu1,shu2,shu3;
bool fenshujudge(struct fenshu A,struct fenshu B){//返回A是否大于B
    return (double)A.fenzi/A.fenmu>(double)B.fenzi/B.fenmu;
}
int main(){
    shu3.fenzi=0;
    int flag=0;
    scanf("%d/%d",&shu1.fenzi,&shu1.fenmu);
    scanf("%d/%d",&shu2.fenzi,&shu2.fenmu);
    if(fenshujudge(shu1,shu2)){
        int a=shu1.fenzi,b=shu1.fenmu;
        shu1.fenzi=shu2.fenzi;shu1.fenmu=shu2.fenmu;
        shu2.fenzi=a;shu2.fenmu=b;
    }
    scanf("%d",&shu3.fenmu);
    for(int i=0;i<shu3.fenmu;i++){
        shu3.fenzi++;
        //shu3大于shu1,小于shu2
        if(fenshujudge(shu3,shu1)&&fenshujudge(shu2,shu3)&&gongyue(shu3.fenmu,shu3.fenzi)==1){
            if(flag==0)
            {printf("%d/%d",shu3.fenzi,shu3.fenmu);flag=1;continue;}
            printf(" %d/%d",shu3.fenzi,shu3.fenmu);
        }

    }
    return  0;
}

posted @ 2018-08-18 07:19  路明天  阅读(256)  评论(0编辑  收藏  举报