Main topic:
Fractional fractions, but the denominator is limited to [1,1000], very obvious enumeration, but in the arena when the silly force, no matter how enumerated, how the two points is wa,wa to death.
(PS: I would like to send a blade to the author ~ ~ ~ ~ ~ ~), this topic should be noted that it is possible to output 0/1.
1#include <cmath>2#include <cstdio>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 Const intMAXN =501000;8 9 structnodeTen { One intx, y; A }; - node STU[MAXN]; - BOOLCMP (Node A, Node B) the { - returna.x*1.0/a.y < b.x*1.0/b.y?true:false; - } - + intGCD (intAintb) - { + returna%b==0? B:GCD (b, a%b); A } at - intBin_sreach (DoubleNintLowintHigh ) - { - while(Low <=High ) - { - intMid = (low + high)/2; in Doublenum = stu[mid].x *1.0/stu[mid].y; - if(Num <N) toLow =mid; + if(Num >N) -High =mid; the if(High-Low = =1) * Break; $ }Panax Notoginseng DoubleNUM1 = stu[low].x*1.0/stu[low].y; - Doublenum2 = stu[high].x*1.0/stu[high].y; the if(Fabs (N-NUM1) > Fabs (N-num2)) + returnHigh ; A returnLow ; the } + intMain () - { $ intT, k =1; $ -stu[0].Y =1; - for(intI=1; i<= +; i++) the for(intJ=i; j<= +; J + +) - {Wuyi if(GCD (j, i) = =1) the { -stu[k].x =i; WuSTU[K++].Y =J; - } About } $Sort (Stu, stu+K, CMP); - -scanf ("%d", &t); - while(T--) A { + DoubleN; thescanf ("%LF", &n); - intp = Bin_sreach (n,0, K-1); $printf ("%d/%d\n", stu[p].x, stu[p].y); the } the return 0; the}
2015 Xiangtan Seventh annual college Student Program design contest--fraction