Delta-wave

Delta-wave

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. 
InputInput contains two integer numbers M and N in the range from 1 to 1000000000 separated with space(s).OutputOutput should contain the length of the shortest route.Sample Input
6 12 
Sample Output
3


简单数学题,找规律,找到了就发现很水。

从三个角度看这个图,level,left,right,如下图,题目的答案就是3个图上2个点之间的层数的高度差之和。

例如 6 12 ,level=1,left=1,right=1,答案就是3。

例如 3 12 ,level=2,left=1,right=2,答案就是6。



#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
    int m,n,cm,cn,rm,rn,lm,ln;    //c表示level图 ,r表示right图,l表示left图
    while(scanf("%d%d",&m,&n)!=EOF)//
    {
        cm=(int)ceil(sqrt(m));//ceil为向上取整函数“math.h”
        cn=(int)ceil(sqrt(n));
        rm=(m-(cm-1)*(cm-1)-1)/2+1; //确定m在right图中的那一层
        rn=(n-(cn-1)*(cn-1)-1)/2+1;
        lm=(cm*cm-m)/2+1;          //确定m在left图中的那一层
        ln=(cn*cn-n)/2+1;
        int cnt=(int)(fabs(cm-cn)+fabs(lm-ln)+fabs(rm-rn));
        printf("%d\n",cnt);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值