HDU-Delta-wave(C++中的强制转换与sqrt函数的一些小细节)

来源:互联网 发布:文泰刻绘2002端口设置 编辑:程序博客网 时间:2024/06/03 11:15

Problem Description
A triangle field is numbered with successive integers in the way shown on the picture below.

hdu <wbr>1030 <wbr>Delta-wave


The traveller needs to go from the cell with number M to the cell with number N. The traveller isable 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
//////////////////////////////////////////////////////////////////////////////////////////////


说一下大概思路

建立一个坐标系!一个点可以用3个坐标来确定

关键就是找坐标了!

//基本思路://1.建立一个坐标系,一个点可以用3个坐标确定,暂时定位 left right level//2.坐标的确定,level就不多说了,开个根号就确定了,关键是left,right//  找到了level,那么先找X(X为当前行的最小数)Y(Y为当前行的最大数)//  看这个数与X,Y中间有几个数!如13  在第4行,x为10,y为16 //  13到 10 有4个数,其中每两个数一组,left加1 同理fight!#include<stdio.h>#include<math.h>int zs(int a){    if(a<=0)        return -a;    else        return a;}void find(double a,int &left,int &right,int &level){    int x,y;    level=(int)sqrt(a)+1;    if(a==((int)sqrt(a)*(int)sqrt(a)))    {        level=sqrt(a);    }    x=(level-1)*(level-1)+1;    y=level*level;    left=((int)a-x+1)/2+(((int)a-x+1)%2);    right=(y-(int)a+1)/2+((y-(int)a+1)%2);}int main(){    double n,m;    int num=0;    int left1,left2,right1,right2,level1,level2;    while(scanf("%lf%lf",&n,&m)!=EOF)    {        find(n,left1,right1,level1);        find(m,left2,right2,level2);        //printf("%d %d %d %d %d %d\n",left1,right1,level1,left2,right2,level2);        num=zs(left1-left2)+zs(right1-right2)+zs(level1-level2);        printf("%d\n",num);    }    return 0;}

有一个大大的问题!

导致我WA了很久,是C与C++里面的细节问题!看来我要回去温习书本了

sqrt里面只能加入double型的数!如果加int型的数C++会报错CE

但是G++不报错!会WA啊!不CE只会WA是最骚的!

你根本想不到这个小细节!

还有C++里面变量前面加括号(int)表示强制转换为int型

教训!




0 0
原创粉丝点击