codeforces 281B Nearest Fraction

本文探讨了在限定分母范围内的最接近特定分数的整数分数的查找方法,详细介绍了枚举分母并计算分子的过程,以及如何避免数据溢出的问题。通过实例展示了算法的应用,并分析了数据处理中可能出现的挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Nearest Fraction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given three positive integers x, y, n. Your task is to find the nearest fraction to fraction whose denominator is no more than n.

Formally, you should find such pair of integers a, b (1 ≤ b ≤ n; 0 ≤ 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.

Input

A single line contains three integers x, y, n (1 ≤ x, y, n ≤ 105).

Output

Print the required fraction in the format "a/b" (without quotes).

Sample test(s)
Input
3 7 6
Output
2/5
Input
7 2 4
Output
7/2
  这题目,怎么说呢,思路不多久就想出来了, 枚举分母,求分子就可以,但是却是wa,到最后也没能解决,看数据的时候却发现 中间过程在计算的时候,数据出现了溢出。哎,杯具了。
#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 long int zhong;
    double mod;
    while(scanf("%d %d %d",&x,&y,&n)!=EOF)
    {
        top=0;
        for(i=1;i<=n;i++)
        {
            zhong=((long long int)i*(long 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值