hdu 1030 Delta-wave(找规律)
来源:互联网 发布:网络订票如何选下铺 编辑:程序博客网 时间:2024/06/05 17:58
Delta-wave
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
[参考博客]:(http://www.cnblogs.com/ACMan/archive/2012/05/30/2526798.html)
ps:分析一下图可以发现,这张图可以从三个角度来看
而题目的答案就是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>#include<stdlib.h>int main(){ double n,m; while(~scanf("%lf%lf",&n,&m)) { int x1,y1,z1,x2,y2,z2;//x=level,y=left,z=right if((int)sqrt(n)==sqrt(n)) x1=sqrt(n); else x1=sqrt(n)+1; if(n==((int)sqrt(n))*((int)sqrt(n))) y1=sqrt(n); else y1=(n-((int)sqrt(n))*((int)sqrt(n))-1)/2+1; if(n==((int)sqrt(n))*((int)sqrt(n))) z1=1; else z1=(((int)sqrt(n)+1)*((int)sqrt(n)+1)-n)/2+1; if((int)sqrt(m)==sqrt(m)) x2=sqrt(m); else x2=sqrt(m)+1; if(m==((int)sqrt(m))*((int)sqrt(m))) y2=sqrt(m); else y2=(m-((int)sqrt(m))*((int)sqrt(m))-1)/2+1; if(m==((int)sqrt(m))*((int)sqrt(m))) z2=1; else z2=(((int)sqrt(m)+1)*((int)sqrt(m)+1)-m)/2+1; printf("%d\n",abs(x1-x2)+abs(y1-y2)+abs(z1-z2)); } return 0;}
- hdu 1030 Delta-wave(找规律)
- HDU 1030 Delta-wave(找规律)
- HDU ACM 1030 Delta-wave 找规律
- hdu 1030 Delta-wave(数学题+找规律)
- Delta-wave hdu 1030 找规律的数学题目
- 杭电1030 Delta-wave (找规律)
- HDU1030 - Delta-wave (找规律)
- HDU1030 Delta-wave(找规律)
- HDU 1030 ( Delta-wave )
- HDU 1030 Delta-wave
- HDU 1030 Delta-wave
- HDU 1030 Delta-wave
- HDU-1030-Delta-wave
- hdu 1030 Delta-wave
- hdu 1030 Delta-wave
- hdu 1030 Delta-Wave+
- HDU 1030 Delta-wave
- Hdu 1030 Delta Wave
- Android Studio的安装配置过程
- Handler、Looper和MessageQueue的关系
- Android系统架构详解
- (51nod)1002
- 20170312 HTML5学习笔记1
- hdu 1030 Delta-wave(找规律)
- h5标签
- cf-778B (拆位)
- HTML5第一课时
- 网站测试流程、要求及测试报告
- 智能一代云平台(十五):Keepalived+Nginx实现高可用,反向代理---Nginx安装及配置
- 关于JFrame里setbackground()失效的解释
- CodeForces
- 超越spark性能数百倍的性能测试