| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:0MS 都是怎么做的啊????这么快,有神么算法吗??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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator