hdu 1030 Delta-wave

Delta-wave

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


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
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1035 1071 1032 1031 1041 
 
很简单的题,弄懂题意,很容易写。
 
题意:求给出的两个数字所在表中的位置的距离。
 
附上代码:
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 int abs(int a)
 7 {
 8     return a>0?a:-a;
 9 }
10 int main()
11 {
12     int n,m,i,j,cm,cn,rm,rn,lm,ln;
13     while(~scanf("%d%d",&m,&n))
14     {
15         cm=(int)ceil(sqrt(m));//ceil为向上取整函数“math.h”
16         cn=(int)ceil(sqrt(n));
17         rm=(m-(cm-1)*(cm-1)-1)/2+1;
18         rn=(n-(cn-1)*(cn-1)-1)/2+1;
19         lm=(cm*cm-m)/2+1;
20         ln=(cn*cn-n)/2+1;
21         int sum=(int)abs(cm-cn)+abs(rm-rn)+abs(lm-ln);
22         printf("%d\n",sum);
23     }
24     return 0;
25 }

 

posted @ 2016-04-04 11:27  lucky_少哖  阅读(128)  评论(0编辑  收藏  举报