zoukankan      html  css  js  c++  java
  • 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
    ",sum);
    23     }
    24     return 0;
    25 }
  • 相关阅读:
    L1范式和L2范式的区别
    随机森林
    LDA-math-神奇的Gamma函数
    (转)共轭先验的理解
    Hits算法
    朴素贝叶斯分类算法(3)
    朴素贝叶斯分类算法(2)
    朴素贝叶斯分类算法(1)
    多项分布(multinominal distribution)
    从对偶问题到KKT条件
  • 原文地址:https://www.cnblogs.com/pshw/p/5351630.html
Copyright © 2011-2022 走看看