D beef cattle off winter and five cattle sister's dating (greedy)

Here Insert Picture Description
This question is obviously a greed issue, as far as possible every time we choose can make us closer distance method because open cube root of it must be better than the back of the front, so you can give ans initial value fabs (ab) and then use the cycle represents the number of impressions, each time can be saved in comparison with cbrt, min function of distance. To get the answer

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        double x,y;
        scanf("%lf%lf",&x,&y);
        double ans=fabs(x-y);
        for(int i=1;i<=16;i++){
            x=cbrt(x);
            ans=min(ans,fabs(x-y)+i);
        }
        printf("%.7lf\n",ans);
    }
    return 0;
}
Published 48 original articles · won praise 17 · views 4464

Guess you like

Origin blog.csdn.net/weixin_45757507/article/details/104317679