几何思维题1

J - Delta-wave;
杭电oj1030题;看图
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

一道几何类型的题;先摆代码

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
    int a, b, sum;
    int h1,h2,t1,t2,tt1,tt2,wz1,wz2,wy1,wy2;
    while(scanf("%d %d",&a, &b) != EOF){
        h1 = (int)sqrt(a);
        if(h1*h1!=a){
            h1=h1+1;
        }
        h2 = (int)sqrt(b);
        if(h2*h2!=b){
            h2=h2+1;
        }
        t1=h1*h1;
        t2=h2*h2;
        wz1=abs(a-t1)/2+1;
        wz2=abs(b-t2)/2+1;
        tt1=(h1-1)*(h1-1)+1;
        tt2=(h2-1)*(h2-1)+1;
        wy1=abs(a-tt1)/2+1;
        wy2=abs(b-tt2)/2+1;
        sum = abs(wy1-wy2)+abs(wz1-wz2)+abs(h1-h2);
        printf("%d\n",sum);                                                                                                                                          
    }

    return 0 ;
}

题目意思就是如图(hdu1030题)排列数字,输入两个数字,输出他们间的最短路径也就是从这个数字空间到那个数字表记的空间经过的线;
如图,我们可以把这些线划分为三类,
一个是水平线
一个是向左的线
一个是向右的线
在想要如何计算;
水平线就是他们之间相差几行就是会有几个行线;这就要知道他们分别是那几行;
就是把a进行sqrt;如果结果是小数则要进行加一;

`
h1 = (int)sqrt(a);
if(h1*h1!=a){
h1=h1+1;
}

向右和向左其实是类似的;
这里写图片描述
就是按图片那种说法;找到各自的行;去相减最后得到的就是各自的线的个数
//

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值