2020牛客寒假算法基础集训营5 D.牛牛与牛妹的约会

D.牛牛与牛妹的约会

题目链接-牛牛与牛妹的约会
在这里插入图片描述
在这里插入图片描述
解题思路
贪心,用牛牛现位置和牛妹的坐标计算距离,再用闪现后的坐标计算几个距离,所以直接判断闪现快还是走路快,如果如果前者减去后者大于1.0,说明闪现快,否则说明闪现慢了,小心闪现过头往回走的情况,模拟即可

cbrt() 函数返回 x 的立方根值,这个函数不会失败,因为任何可表示的实数总有一个可表示的立方根

函数原理:

double cbrt(double x){
	if(x<0)
		return -pow(-x,1.0/3.0);
	return pow(x,1.0/3.0);
}

附上代码

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=1e5+5;
const int M=1e9+7;
typedef long long ll;
typedef pair<int,int> PII;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int T;
    scanf("%d",&T);
    while (T--){
        int a, b;
        cin>>a>>b;
        double x=a;
        double y=b;
        double res=0.0;
        while(1){
            double z=cbrt(x);
            double last=abs(x-y);
            double now=abs(z-y);
            if(last-now>1.0){
                x=z;
                res+=1.0;
            }
            else{
                res+=last;
                break;
            }
        }
        cout<<fixed<<setprecision(9)<<res<<endl;
    }
    return 0;
}
 
发布了78 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/104316064