zoukankan      html  css  js  c++  java
  • hdu 1030 Delta-wave

    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

    简单数学题,找规律,找到了就发现很水。

    从三个角度看这个图,level,left,right,如下图,题目的答案就是3个图上2个点之间的层数的高度差之和。

    例如 6 12 ,level=1,left=1,right=1,答案就是3。

    例如 3 12 ,level=2,left=1,right=2,答案就是5。


    //#include<queue>
    //#include<stack>
    //#include<vector>
    //#include<math.h>
    //#include<stdio.h>
    //#include<numeric>//STL数值算法头文件
    //#include<stdlib.h>
    //#include<string.h>
    //#include<iostream>
    //#include<algorithm>
    //#include<functional>//模板类头文件
    //using namespace std;
    //
    //const int INF=0x3f3f3f3f;
    //const int maxn=10100;
    //
    //int Getn(int x)
    //{
    //    int t = int(sqrt(double(x)));
    //    if(t *t == x)
    //        t--;
    //    return t;
    //}
    //int main()
    //{
    //    int m,n;
    //    while(scanf("%d %d",&m,&n)!=EOF)
    //    {
    //        int sn,sm,pn,pm;
    //        pn = Getn(n);
    //        pm = Getn(m);
    //        sn = pn*pn+1;
    //        sm = pm*pm+1;
    //        int sum =0;
    //        sum += abs((pn+1-(n-sn+1)/2)-(pm+1-(m-sm+1)/2));
    //        sum += abs((1+(n-sn)/2)-(1+(m-sm)/2));
    //        sum += abs((pn+1)-(pm+1));
    //        printf("%d
    ",sum);
    //    }
    //    return 0;
    //}
    
    
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int main()
    {
        int m,n,cm,cn,rm,rn,lm,ln;    //c表示level图 ,r表示right图,l表示left图
        while(scanf("%d%d",&m,&n)!=EOF)
        {
            cm=(int)ceil(sqrt(m));//ceil为向上取整函数“math.h”
            cn=(int)ceil(sqrt(n));
            rm=(m-(cm-1)*(cm-1)-1)/2+1; //确定m在right图中的那一层
            rn=(n-(cn-1)*(cn-1)-1)/2+1;
            lm=(cm*cm-m)/2+1;          //确定m在left图中的那一层
            ln=(cn*cn-n)/2+1;
            int cnt=(int)(abs(cm-cn)+abs(lm-ln)+abs(rm-rn));
            printf("%d
    ",cnt);
        }
        return 0;
    }

  • 相关阅读:
    尘误解
    了解了解你自己的话zookeeper(从那时起,纠正了一些说法在线)
    HDU 5055 Bob and math problem(结构体)
    Linux通过编辑器vi使用介绍
    OCP-1Z0-051-名称解析-文章32称号
    刘强东:解密京东10甘蔗理论
    Android结构分析Android智能指针(两)
    hbase ganglia监控配置
    第一个位和一个真正的项目件
    Html5 の 微信飞机大战
  • 原文地址:https://www.cnblogs.com/nyist-xsk/p/7264861.html
Copyright © 2011-2022 走看看