码迷,mamicode.com
首页 > 其他好文 > 详细

欧拉工程第71题:Ordered fractions

时间:2015-09-27 22:46:23      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

 

题目链接:https://projecteuler.net/problem=71

 If n<d and HCF(n,d)=1, it is called a reduced proper fraction.

 

n/d 真分数升序排序后,离 3/7最近的数,d<=1000000

 

Java程序:

public class P71{
    void run(){
        calculate();
    }
    void calculate(){
        int max_n = 1000000;
        long a = 3;
        long b = 7;
        long r = 0;
        long s = 1;
        int q = 0;
        long p = 0;
        for( q = max_n;q>2;q--){
             p = (a*q-1)/b;
            if(p*s>r*q){
                s = q;
                r = p;
            }
        }
        System.out.println(r+"/"+s);
    }


    public static void main(String[] args){
        long t0 = System.currentTimeMillis();
        new P71().run();
        long t1= System.currentTimeMillis();
        System.out.println((t1-t0)+"ms");
    }
}

 

欧拉工程第71题:Ordered fractions

标签:

原文地址:http://www.cnblogs.com/theskulls/p/4842787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!