hdu 1030 Delta-wave

http://acm.hdu.edu.cn/showproblem.php?pid=1030

 

 Delta-wave

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

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
 

一道找规律的题,每个三角形的位置用三维的坐标(x, y, z)表示, x表示水平层数从上到下,y表示从左到右,z表示从右到左

求从第n个三角到第m个三角的最近距离 = 各个坐标差之和

 即:d = abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2);

第n个三角说明从1到n共有n个三角,判断它在第几行即坐标值x

用for循环来找

 for(i = 1 ; ; i += 2)//i表示某一行三角的个数,每行三角的个数相差2,所以i每次加2
        {
            if(n - i <= 0)//如果此时三角的个数小于或等于某一行的三角个数,则已经可以确定了坐标,此时n表示第n个三角在x行的编号
            {
                y = (i - n) / 2 + 1;
                z = (n + 1) / 2;
                break;
            }//求出坐标y和z
            x++;//如果n大于前i行的三角个数,则n所在的行数再下一行,即x++
            n -= i;
        }

结合图来理解

http://images.cppblog.com/cppblog_com/guodongshan/1030.jpg

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>

using namespace std;

int main()
{
    int n, i, m, x1, y1, z1, x2, y2, z2, d;
    while(~scanf("%d%d", &n, &m))
    {
        x1 = x2 = 1;
        for(i = 1 ; ; i += 2)
        {
            if(n - i <= 0)
            {
                y1 = (i - n) / 2 + 1;
                z1 = (n + 1) / 2;
                break;
            }
            x1++;
            n -= i;
        }
        for(i = 1 ; ; i += 2)
        {
            if(m - i <= 0)
            {
                y2 = (i - m) / 2 + 1;
                z2 = (m + 1) / 2;
                break;
            }
            x2++;
            m -= i;
        }
        d = abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2);
        printf("%d\n", d);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/qq2424260747/p/4792288.html

本项目是一个基于SSM(Spring+SpringMVC+MyBatis)框架和Vue.js前端技术的大学生第二课堂系统,旨在为大学生提供一个便捷、高效的学习和实践平台。项目包含了完整的数据库设计、后端Java代码实现以及前端Vue.js页面展示,适合计算机相关专业的毕设学生和需要进行项目实战练习的Java学习者。 在功能方面,系统主要实现了以下几个模块:用户管理、课程管理、活动管理、成绩管理和通知公告。用户管理模块支持学生和教师的注册、登录及权限管理;课程管理模块允许教师上传课程资料、设置课程时间,并由学生进行选课;活动管理模块提供了活动发布、报名和签到功能,鼓励学生参与课外实践活动;成绩管理模块则用于记录和查询学生的课程成绩和活动参与情况;通知公告模块则实时发布学校或班级的最新通知和公告。 技术实现上,后端采用SSM框架进行开发,Spring负责业务逻辑层,SpringMVC处理Web请求,MyBatis进行数据库操作,确保了系统的稳定性和扩展性。前端则使用Vue.js框架,结合Axios进行数据请求,实现了前后端分离,提升了用户体验和开发效率。 该项目不仅提供了完整的源代码和相关文档,还包括了详细的数据库设计文档和项目部署指南,为学习和实践提供了便利。对于基础较好的学习者,可以根据自己的需求在此基础上进行功能扩展和优化,进一步提升自己的技术水平和项目实战能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值