Integer Approximation(分治+枚举)

Integer Approximation

https://vjudge.net/problem/POJ-1650

直接暴力枚举每个数,得出最接近的两个整数

#include<stdio.h>
#include<iostream>
#include<map>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
//1 <= L <= 100000
int main()
{
    double num;
    double maxn;
    while(~scanf("%lf%lf",&num,&maxn))
    {
        double left=1;
        double right=1;
        double minn=9999999999;
        double mina=0;
        double minb=0;
        while(left<=maxn&&right<=maxn)
        {
            if(left/right>num)
            {
                if(fabs(left/right-num)<minn)
                {
                    minn=fabs(left/right-num);
                    mina=left;
                    minb=right;
                   // cout<<minn<<" "<<mina<<" "<<minb<<endl;
                }
                right++;
            }
            else
            {
                if(fabs(left/right-num)<minn)
                {
                    minn=fabs(left/right-num);
                    mina=left;
                    minb=right;
                   // cout<<minn<<" "<<mina<<" "<<minb<<endl;
                }
                left++;
            }
           // cout<<left<<" "<<right<<endl;
        }
       cout<<mina<<" "<<minb<<endl;
    }
    return 0;
}

  

Integer Approximation(分治+枚举)

上一篇:如何把Mybatis的Mapper.xml配置文件和dao接口放在同一个包下


下一篇:关于AndroidStudio项目app在手机上运行遇到登录网络问题的解决