zoukankan      html  css  js  c++  java
  • POH 4180 RealPhobia(连分数)

    RealPhobia

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 69    Accepted Submission(s): 27


    Problem Description
    Bert is a programmer with a real fear of floating point arithmetic. Bert has quite successfully used rational numbers to write his programs but he does not like it when the denominator grows large. Your task is to help Bert by writing a program that decreases the denominator of a rational number, whilst introducing the smallest error possible. For a rational number A/B, where B > 2 and 0 < A < B, your program needs to identify a rational number C/D such that:
    1. 0 < C < D < B, and
    2. the error |A/B - C/D| is the minimum over all possible values of C and D, and
    3. D is the smallest such positive integer.
     
    Input
    The input starts with an integer K (1 <= K <= 1000) that represents the number of cases on a line by itself. Each of the following K lines describes one of the cases and consists of a fraction formatted as two integers, A and B, separated by “/” such that:
    1. B is a 32 bit integer strictly greater than 2, and
    2. 0 < A < B
     
    Output
    For each case, the output consists of a fraction on a line by itself. The fraction should be formatted as two integers separated by “/”.
     
    Sample Input
    3 1/4 2/3 13/21
     
    Sample Output
    1/3 1/2 8/13
     
    Source
     
    Recommend
    lcy
     

    分解成连分数,然后最后一个数减一;

    //HDU 4180 连分数 
    
    #include<stdio.h>
    #include<math.h>
    #include<algorithm>
    using namespace std;
    int an[100];
    int gcd(int a,int b)
    {
        int r=0;
        while(b!=0)
        {
            r=a%b;
            a=b;
            b=r;
        }    
        return a;
    }    
    int fenjie(int a,int b,int an[])//连分数分解 
    {
        int n=0;
        int t;
        while(a!=1)
        {
            an[n++]=b/a;
            t=b%a;
            b=a;
            a=t;
        }    
        an[n++]=b;
        return n;
    }    
    int main()
    {
        int n,A,B,C,D;
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=0;i<n;i++)
            {
                scanf("%d/%d",&A,&B);
                int d=gcd(A,B);
                if(d>1)
                {
                    printf("%d/%d\n",A/d,B/d);
                    continue;
                }  
                int len=fenjie(A,B,an); 
                an[len-1]--;
                C=1;
                D=an[len-1];
                for(int j=len-2;j>=0;j--)
                {
                    int t=an[j]*D+C;
                    C=D;
                    D=t;
                }    
                printf("%d/%d\n",C,D);
                
            }    
            
        }   
        return 0; 
    }   
  • 相关阅读:
    eyoucms遍历子栏目,有二级栏目的点击展开或者收缩
    eyoucms 遍历栏目下的子栏目
    帝国cms 内容页根据关键词来调用相关内容
    帝国cms 上传的图片前台不显示
    帝国cms 通过字段内容来获取数据
    eyoucms 去掉 index.php后缀
    通过jquery插件复制文字
    帝国cms 表单弹窗提交,判断后才能提交到后台
    动态库和静态库
    J-520-2018年第二届河北省大学生程序设计竞赛(快速幂取模)
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2465455.html
Copyright © 2011-2022 走看看