Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:0MS 都是怎么做的啊????这么快,有神么算法吗??

Posted by jqctbu at 2012-05-08 16:02:28 on Problem 1079
In Reply To:0MS 都是怎么做的啊????这么快,有神么算法吗?? Posted by:dexter at 2004-04-18 12:54:12
> 请教!!
用数学方法做可以0MS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void)
{
    int a, b, p, q, tp, tq;

    while (~scanf("%d%d", &a, &b)) {
        tq = 1;
        tp = (int)((double)(a * tq) / (double)b + 0.5); // 四舍五入
        printf("%d/%d\n", tp, tq);
        for (q = 2; ; q++) {
            p = (int)((double)(q * a) / (double)b + 0.5);
            if (q * fabs(1.0 * tp * b - a * tq) > tq * fabs(1.0 * p * b - a * q)) {
                tp = p;
                tq = q;
                printf("%d/%d\n", tp, tq);
            }
            if (a * q == p * b)
                break;
        }
    }

    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator