| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 378 人关注过本帖
标题:大家帮忙看看,太谢谢了
只看楼主 加入收藏
枫叶无痕
Rank: 2
等 级:论坛游民
帖 子:80
专家分:30
注 册:2011-2-10
结帖率:73.91%
收藏
已结贴  问题点数:20 回复次数:4 
大家帮忙看看,太谢谢了
Delta-wave

Time Limit:1000MS  Memory Limit:65536K
Total Submit:178 Accepted:66

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

Ural Collegiate Programming Contest 1998


#include<stdio.h>
main()
{
    long n,m;
    long i,j,k,e,f,d,s,h;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        e=f=1;
        while((n<e*e-2*e+2)||(n>e*e))
    {
        e++;
    }
        while((m<f*f-2*f+2)||(m>f*f))
    {
        f++;
    }
    if(e==f)
    {
        d=m-n;
        printf("%ld\n",d);
    }
    else
    {                                                                  
            if((n-(e*e-2*e+2)+1)%2==0)
            {
                s=f*f-2*f+2+n-(e*e-2*e+2)-1;
                h=s+2*(f-e+1);
                if(m>=s&&m<=h)
                {
                    if((m-(f*f-2*f+2)+1)%2==1)
                    {
                        d=2*(f-e)+1;
                        printf("%ld\n",d);
                    }
                    else
                    {
                        d=2*(f-e);
                        printf("%ld\n",d);
                    }
                }
                else
                {
                    if(m>h)
                    {
                        d=2*(f-e)+1+(m-h);
                        printf("%ld\n",d);
                    }
                    if(m<s)
                    {
                        d=2*(f-e)+1+(s-m);
                        printf("%ld\n",d);
                    }
                }
            }
            else
            {
                s=f*f-2*f+2+n-(e*e-2*e+2)-1;
                h=s+2*(f-e);
                if(m>=s&&m<=h)
                {
                    if((m-(f*f-2*f+1))%2==1)
                    {
                        d=2*(f-e);
                        printf("%ld\n",d);
                    }
                    else
                    {
                        d=2*(f-e)-1;
                        printf("%ld\n",d);
                    }
                }
                else
                {
                    if(m>h)
                    {
                        d=2*(f-e)+(m-h)-1;
                        printf("%ld\n",d);
                    }
                    if(m<s)
                    {
                        d=2*(f-e)+(s-m)-1;
                        printf("%ld\n",d);
                    }
                }
        }
    }
    }
}


大家帮忙看看
搜索更多相关主题的帖子: cell 
2011-04-13 16:58
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
Chinese,please!

   唯实惟新 至诚致志
2011-04-13 18:56
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:0 

                                         
===========深入<----------------->浅出============
2011-04-13 18:59
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:10 
好像是求6到12最小步长,看输出是3,  也即:6,9,12,等差为3

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-04-13 22:17
boxinchao
Rank: 4
等 级:业余侠客
帖 子:51
专家分:231
注 册:2011-4-13
收藏
得分:10 
回复 4楼 ansic
这种没有注释的代码真的很难看很不愿意看,更何况还是英文的
2011-04-13 22:20
快速回复:大家帮忙看看,太谢谢了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025751 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved