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

79 篇文章 0 订阅

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;
} 





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值