Delta-wave问题:c++

4 篇文章 0 订阅

【问题描述】
一个三角形字段用下图所示的连续整数编号。

这里写图片描述
旅行者需要从m号的细胞到N号的细胞,旅行者只能通过细胞边缘进入细胞,不能通过顶点从细胞到细胞。边缘的旅行次数使得旅行者的路径的长度。
编写程序,确定连接n和m的最短路径的长度。

输入包含两个整数m和n,在1到1000000000之间用空格隔开。

【输出】
输出应包含最短路径的长度。

【输入输出样例】
in
6 12
out
3
【源程序清单】

#include<iostream>
#include<cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

    int calculate_x(int n)   //该数所在层
    {
        return (ceil(sqrt(n)));
    }
    int calculate_y(int n, int x)   //该数所在右列
    {
        return(n - (x - 1)*(x - 1) + 1) / 2;
    }
    int calculate_z(int n, int x)   //该数所在左列
    {
        return (x*x - n) / 2 + 1;
    }
    int main()
    {
        int n, m, x1, x2, y1, y2, z1, z2, sum;
        while (scanf_s("%d%d", &n, &m)!=EOF)
        {   
            x1 = calculate_x(n);
            y1 = calculate_y(n, x1);
            z1 = calculate_z(n, x1);
            x2= calculate_x(m);
            y2=calculate_y(m, x2);
            z2 = calculate_z(m, x2);
            sum = abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2);  //以三维坐标形式表示出来并进行计算
            printf("%d\n", sum);
        }
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值