| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 387 人关注过本帖
标题:Delta-wave 谁能告诉我拿错啦
只看楼主 加入收藏
windthenrain
Rank: 2
等 级:论坛游民
帖 子:21
专家分:15
注 册:2014-10-27
结帖率:100%
收藏
 问题点数:0 回复次数:2 
Delta-wave 谁能告诉我拿错啦
A triangle field is numbered with successive integers in the way shown on the picture below.
Delta-wave  (图百度一下,搜“Delta-wave”就有,麻烦了  
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
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
int delta_wave(int n,int *p)
{
    int i=1,count=0,count1=-1;
    if(n==1)
    {
        p[0]=0,p[1]=0,p[2]=0;
        return *p;
    }
    while(1)
    {
        count++;
        n-=i;
        i+=2;
        if(n<=i) break;
    }
    p[2]=count1=count;
    if(n&1)
        count1*=2;
    else
        count1=count1*2-1;
    n=n-(count+1);
   
    p[0]=n,p[1]=count1;
    return *p;
}

void main()
{
   //freopen("a.txt","r",stdin);
    int N,M;
    scanf("%d %d",&N,&M);
        int t=-35;
        int a[3],b[3];
        if(N>M)  N^=M^=N^=M;
        delta_wave(N,a),delta_wave(M,b);
        if(a[0]==b[0])
        {
            t=(a[1]-b[1]);
            if(t<0) t=-t;
            printf("%d\n",t);
        }
        else
        {
          //  printf("a[2]=%d b[2]=%d ",a[2],b[2]);
            //printf("a[0]=%d b[0]=%d ",a[0],b[0]);
            int t2=a[2]-b[2],t0=a[0]-b[0];
            if(t2<0)  t2=-t2;
            if(t0<0)  t0=-t0;
            t=t2+t0;
            printf("%d\n",t);
        }

}
搜索更多相关主题的帖子: 百度一下 determine triangle through travel 
2014-11-26 16:42
windthenrain
Rank: 2
等 级:论坛游民
帖 子:21
专家分:15
注 册:2014-10-27
收藏
得分:0 
提交上去他说我错了,语法是对的
2014-11-26 18:01
windthenrain
Rank: 2
等 级:论坛游民
帖 子:21
专家分:15
注 册:2014-10-27
收藏
得分:0 
能帮我举出个反例就行,靠大家了
2014-11-27 08:11
快速回复:Delta-wave 谁能告诉我拿错啦
数据加载中...
 
   



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

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