您的位置:首页 > 其它

YT02-简单数学课后题-1002 Delta-wave -(5.31日-烟台大学ACM预备队解题报告)

2015-06-01 15:11 525 查看


Delta-wave


Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)


Total Submission(s) : 33 Accepted Submission(s) : 18


Font: Times New Roman | Verdana | Georgia


Font Size: ← →


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

Ural Collegiate Programming Contest 1998

计144-1
杨振宇

思路一:

第一步:确定两个数的所在位置;

第二步:找出上方的数对应数的区域;

第三步:计算上方数到其对应区域的步数;

第四步:判断下方数是否在所在区域;

第五步:计算实际的步数;
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        if(a>b)swap(a,b);//保证a小b大
        int ca=sqrt(a-1)+1;//ca为a数字的所在行数
        //cout<<ca<<endl;
        int cb=sqrt(b-1)+1;//cb为b数字的所在区域
        //cout<<cb<<endl;
        int cc=cb-ca;//cc为cb与ca行数之差
        if(cc==0)//如果在同一行则直接b-a
        {
            cout<<b-a<<endl;
            continue;
        }
        int left=a+(2*ca+2*cb-4)*(cb-ca)/2;  //范围的左开始
        int len=(cb-ca)*2;//范围的大小
      //cout<<left<<" "<<len<<endl;
        int k=0;//k为步数
if((a%2==0&&ca%2==0)||(a%2==1&&ca%2==1))//箭头方向向上的数
        {
            if(b<left)k+=left-b;//b在a范围的左边
            else if(b>left+len)k+=b-left-len;//b在a范围内
            else//b在a范围的右面
            {
                if((b%2==1&&cb%2==0)||(b%2==0&&cb%2==1))//b为箭头向下的数步数减一
                {
                    k--;
                }
            }
        }
        else//箭头方向向下的数
        {
            k++;
            if(b<left-1)k+=left-b-1;
            else if(b>left+len+1)k+=b-left-len-1;
            else if((b%2==1&&cb%2==0)||(b%2==0&&cb%2==1))k--;
        }
        k+=(cb-ca)*2;//走到相同位置的步数
        //cout<<(cb-ca)*2<<endl;
        cout<<k<<endl;
    }
    return 0;
}


思路二:





#include<stdio.h> 
#include<math.h> 
int main() 
{ 
int a,b; 
int aX,aY,bX,bY,aLayer,bLayer,step; 
while(scanf("%d%d",&a,&b)!=EOF) 
{ 
aLayer=ceil(sqrt((double)a));//求出数a所在层 
bLayer=ceil(sqrt((double)b));//求出数b所在层 
if(aLayer==bLayer) printf("%d\n",abs(a-b)); 
else 
{ 
aX=(aLayer*aLayer-a)/2;//计算a的X坐标 
bX=(bLayer*bLayer-b)/2;//计算b的X坐标 
aY=(a-(aLayer*aLayer-2*aLayer+2))/2;//计算a的Y坐标 
bY=(b-(bLayer*bLayer-2*bLayer+2))/2;//计算b的Y坐标 
step=abs(aX-bX)+abs(aY-bY)+abs(aLayer-bLayer); 
printf("%d\n",step);//求出最终步骤 
                 } 
         } 
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: