Delta-wave

Delta-wave

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4914    Accepted Submission(s): 1865


Problem Description
A triangle field is numbered with successive integers in the way shown on the picture below. 



The traveller needs to go from the cell with number M to the cell with number N. The traveller is able to enter the cell through cell edges only, he can not travel from cell to cell through vertices. The number of edges the traveller passes makes the length of the traveller's route. 

Write the program to determine the length of the shortest route connecting cells with numbers N and M. 
 

Input
Input contains two integer numbers M and N in the range from 1 to 1000000000 separated with space(s).
 

Output
Output should contain the length of the shortest route.
 

Sample Input
  
  
6 12
 

Sample Output
  
  
3
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1035  1041  1207  1108  1024 


#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
int main()
{
    int i,j,k,n,m,tem;
    while(scanf("%d%d",&m,&n)!=EOF)
    {

        if(m>n)
        {
            tem=m;
            m=n;
            n=tem;
        }
        int ha=(int)ceil(sqrt((double)m));
        int hb=(int)ceil(sqrt((double)n));
        int la=(m-((ha-1)*(ha-1)+1))/2+1;
        int lb=(n-((hb-1)*(hb-1)+1))/2+1;
        int ra=(ha*ha-m)/2+1;
        int rb=(hb*hb-n)/2+1;
       // printf("%d\n",(int)hb-ha+lb-la+rb-ra);
        int cnt=(int)(fabs(la-lb)+fabs(ha-hb)+fabs(ra-rb));
        printf("%d\n",cnt);







    }
    return 0;
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是改写后的代码: ```python import math import numpy as np def generate_gradient_wave(h1, h2, t_seg, dh_dt_max, t_step): t_delta = math.ceil((abs(h2 - h1) / dh_dt_max + t_seg + 1) / t_step) * t_step if t_delta < 2 * t_seg: t_seg = t_delta / 2 t_delta_tmp = math.ceil(abs(h2 - h1) / dh_dt_max * 4) if t_delta_tmp < t_delta: t_ramp = np.arange(0, t_delta_tmp + 1, t_step) h_ramp = calculate_height(h1, h2, t_ramp) return [t_ramp, h_ramp] dh_dt = (h2 - h1) / (t_delta - t_seg) t_ramp = np.arange(0, t_delta + 1, t_step) h_ramp = np.zeros(len(t_ramp)) for i, t in enumerate(t_ramp): if t <= t_seg: h_ramp[i] = dh_dt / t_seg / 2 * t**2 + h1 elif t <= t_delta - t_seg: h_ramp[i] = h_ramp[i - 1] + dh_dt * t_step else: h_ramp[i] = -dh_dt / t_seg / 2 * (t_delta - t)**2 + h2 return [t_ramp, h_ramp] def calculate_height(h1, h2, t): h = np.zeros(len(t)) for i, it in enumerate(t): dt = it / t[-1] h[i] = h1 + (h2 - h1) * (3 * dt**2 - 2 * dt**3) return h ``` 我对原始代码进行了以下改动: - 将原始代码放入了一个名为 `generate_gradient_wave` 的函数中,并将原始函数名修改为 `calculate_height`。 - 添加了导入语句 `import math` 和 `import numpy as np`,以确保所需的库正确导入。 - 将原始代码中的函数名 `gradient_wave` 修改为 `calculate_height`,以更准确地描述其功能。 - 修改了函数 `generate_gradient_wave` 的参数列表,将 `h1`、`h2`、`t_seg`、`dh_dt_max`、`t_step` 作为参数传入,并在函数内部使用这些参数。 - 返回值不变,仍然返回 `[t_ramp, h_ramp]`。 请注意,这只是对原始代码的简单改写,并没有更改其逻辑或实现方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值