前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU 1030 纯数学 找规律

HDU 1030 纯数学 找规律

作者头像
csxiaoyao
发布2019-02-18 17:59:28
5370
发布2019-02-18 17:59:28
举报
文章被收录于专栏:csxiaoyao

题目如下:大意是求两个三角形之间要通过多少条边

Delta-wave

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

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

代码语言:javascript
复制

6 12

Sample Output

代码语言:javascript
复制

3

代码语言:javascript
复制
#include<stdio.h>
#include<math.h>
void main()
{
	int m,n,h1,h2,x,y,temp,num,p1,p2;
	while(scanf("%d %d",&m,&n)!=EOF)
	{
		h1=sqrt((float)m);
		if(h1!=sqrt((float)m))
			h1++;
		h2=sqrt((float)n);
		if(h2!=sqrt((float)n))
			h2++;
		x=m-(h1-1)*(h1-1);
		y=n-(h2-1)*(h2-1);
		p1=(h1*h1-m)/2+1;
		p2=(h2*h2-n)/2+1;
		if(h1>h2)
		{
			temp=h2;
			h2=h1;
			h1=temp;
			temp=x;
			x=y;
			y=temp;
			temp=p1;
			p1=p2;
			p2=temp;
		}
		num=(h2-h1-1)*2;
		if(x%2==0)
			num++;
		if(y%2==0&&x<=y&&p1<=p2)
			num++;
		else
			num+=2;
		if(x>y)
			num+=x-y;
		if(p1>p2)
			num+=y-2*(h2-p1)-1;
		printf("%d\n",num);
	}
}

主要是分两个方向划分到同一个正三角形里面,然后中间的行数*2+头尾吧,头尾三角形分为正反两种,分情况讨论,也不怎么能说清楚啊。。。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014年02月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目如下:大意是求两个三角形之间要通过多少条边
  • Delta-wave
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档