Codeforces 281B Nearest Fraction

Source: Internet
Author: User

B. nearest Fractiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three positive integers x, policy, limit n. your task is to find the nearest fraction to fraction whose denominator is no more than n. formally, you shoshould find such pair of integers a, partition B (1 ≤ partition B ≤ partition n; 0 ≤ partition a) that the value is as minimal as possible. if there are multiple "nearest "Fractions, choose the one with the minimum denominator. if there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator. inputA single line contains three integers x,   y, limit n (1 ≤ limit x, limit y, limit n ≤ limit 105 ). outputPrint the required fraction in the format "a/B" (without quotes ). sample test (s) Input3 7 6Output2/5Input7 2 4Output7/2. How can this problem be solved? It's not long before you come up with the idea. You just need to enumerate the denominator and find the numerator, However, it was wa, which could not be solved at the end. When we looked at the data, we found that the data overflows during the calculation of the intermediate process. Ah, the cup is ready. [Cpp] # include <stdio. h> # include <string. h> # include <math. h> struct num {int zi, mu; double val;} res [1000000]; int cmp (const void * e, const void * f) {struct num * p1 = (struct num *) e; struct num * p2 = (struct num *) f; if (fabs (p1-> val-p2-> val) <= 1e-15) {if (p1-> mu> p2-> mu) {return 1;} else if (p1-> mu <p2-> mu) {return-1;} else {if (p1-> zi> p2-> zi) {return 1;} else if (p1-> zi <p2-> zi) {return-1;} else {Return 0 ;}}if (p1-> val> p2-> val) {return 1 ;}else {return-1 ;}} int main () {int x, y, n, s, I, top, t; long int zhong; double mod; while (scanf ("% d", & x, & y, & n )! = EOF) {top = 0; for (I = 1; I <= n; I ++) {zhong = (long int) I * (long int) x); mod = (double) (zhong)/y; t = (int) (mod + 0.01); res [top]. mu = I; res [top]. zi = t; res [top ++]. val = fabs (double) x/(double) y-(double) t/(double) I); res [top]. mu = I; res [top]. zi = t + 1; res [top ++]. val = fabs (double) x/(double) y-(double) (t + 1)/(double) I); res [top]. mu = I; res [top]. zi = t + 2; res [top ++]. val = fabs (double) x/(double) y-(double) (t + 2)/(double) I); res [top]. mu = I; if (t-1> = 0) {res [top]. mu = I; res [top]. zi = T-1; res [top ++]. val = fabs (double) x/(double) y-(double) (t-1)/(double) I);} if (t-2> = 0) {res [top]. mu = I; res [top]. zi = T-2; res [top ++]. val = fabs (double) x/(double) y-(double) (t-2)/(double) I) ;}} qsort (res, top, sizeof (res [0]), cmp); printf ("% d/% d \ n", res [0]. zi, res [0]. mu);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.