zoukankan      html  css  js  c++  java
  • HDU 1030 Deltawave (找规律)

    Delta-wave

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


    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
     
     
     
    找规律。。。具体的不详细说了,我也是参考了别人的思路~~~
     
     
    #include<stdio.h>
    
    int hang(int n)//返回  n 所在的行数
    {
        int cnt=0;
        int i,s;
        for(i=1,s=0;s<n;i+=2)
        {
            s+=i;
            ++cnt;
        }    
        return cnt;
    }     
    bool isUP(int n)
    {
        int t=hang(n);
        if(t%2==n%2) return 1;
        else return 0;
    }    
    int wei(int n)
    {
        int t=hang(n)-1;
        return n-t*t;
    }    
    int main()
    {
        int M,N;
        while(scanf("%d%D",&M,&N)!=EOF)
        {
            if(M>N)
            {
                M=M^N;
                N=M^N;
                M=M^N;
            }   
            int h1=hang(M);
            int h2=hang(N);
            int dish=h2-h1;
            
            if(h1==h2)
            {
                printf("%d\n",N-M);
                continue;
            }     
            int x1=wei(M);
            int x2=wei(N);
            int w1=M-(h1-1)*(h1-1)+(h2-1)*(h2-1);
            int w2=w1+2*dish;
            int res=0;
            if(N>=w1&&N<=w2)
            {
                res=2*dish;
                if(!isUP(M)&&isUP(N)) res++;
                if(isUP(M)&&!isUP(N)) res--;
            }    
            else
            {
                if(N<w1) res=2*dish+w1-N;
                else res=2*dish+N-w2;
            }    
            printf("%d\n",res);
        }    
        return 0;
    }    
  • 相关阅读:
    Linux排序命令sort笔记
    Linux排序命令sort笔记
    Linux文本截取命令cut​笔记
    排序和搜索
    栈和队列
    链表
    顺序表
    Linux编辑器|gedit|vi|vim编辑器
    Linux简介
    简单学习Git
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2495242.html
Copyright © 2011-2022 走看看