关闭

HOJ Delta-wave

133人阅读 评论(0) 收藏 举报
分类:

题目链接

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

这是参考链接
这是参考链接
这是参考链接

这里写图片描述
这里写图片描述
总结:主要是找规律,重点在找两个数的坐标,可以巧妙的运用坐标系。
注意取绝对值函数,abs与fabs
int abs(int i); // 处理int类型的取绝对值
double fabs(double i); //处理double类型的取绝对值
float fabsf(float i); /处理float类型的取绝对值

代码:

#include <iostream>
#include <cmath>
using namespace std;

int main(){
    int a,b;
    int aLayer,bLayer;
    int aX,aY,bX,bY;
    int distance;

    while(cin >> a >> b){
        aLayer = ceil(sqrt(a));
        bLayer = ceil(sqrt(b));

        if(aLayer == bLayer)
            distance = b - a;
        else{
            aX = (aLayer * aLayer - a) / 2;
            aY = aLayer - (aLayer * aLayer - a + 1) / 2 - 1;
            bX = (bLayer * bLayer - b) / 2;
            bY = bLayer - (bLayer * bLayer - b + 1) / 2 - 1;

            distance = abs(aX - bX) + abs(aY - bY) + abs(aLayer - bLayer);
        }

        cout << distance << endl;
    }


}
0
0
查看评论

贪心法 hoj1003

#include #include using namespace std; pairmilk[5001]; int main() {int m,n,i,j,k,minnum,minmoney,need; while(cin>>n>>m) { minnum=0;k=0;m...
  • u013497977
  • u013497977
  • 2014-03-23 15:41
  • 441

HOJ 2662 Pieces Assignment(状态压缩DP)

题目特征: 1.求方法数目,告诉一堆约束条件(相邻,不攻击之类),其中有个数一般是 状态转移: 1.跟要相互影响的行有关,跟状态有关,跟给出条件有关(题目要求要达到一定数目等,但是不是具体说明一般不用,看看其他的) 2.一般题目情景为线是二维,面为三维 代码: 1.判断约束条件 2.记录状态中1的个...
  • booyoungxu
  • booyoungxu
  • 2015-07-23 11:15
  • 1050

HOJ 1003题 题解

杭电OJ 1003题题目链接:点击打开链接      本题思路: 为了避免复杂的判断,将测试用例全部为负数的情况单独处理。   (1)如果某个测试用例全部为负数,则遍历数据,找到最大的数,记录下标,该下标既是子序列的开始下标,也是结束下标。 ...
  • xiaoyi357
  • xiaoyi357
  • 2016-07-23 20:31
  • 431

Delta-wave

Problem DescriptionA triangle field is numbered with successive integers in the way shown on the picture below. The traveller needs to go from the cel...
  • happymawolf
  • happymawolf
  • 2010-12-07 22:00
  • 1577

Delta-wave hdu 1030 找规律的数学题目

Delta-wave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4602 ...
  • u011471397
  • u011471397
  • 2013-09-13 17:34
  • 652

HOJ 2634 网络流 【蕴含式最大获利问题】

参考: 蕴含式最大获利问题。
  • chm517
  • chm517
  • 2014-07-18 16:56
  • 330

HOJ 2634 网络流最小割 解题报告

2634 - How to earn moreProblem DescriptionXiao Ming is an expert in computer science and technology, so he can get a lot of projects every month. The ...
  • onepointo
  • onepointo
  • 2017-06-10 10:32
  • 166

HOJ 2634 最大权闭合子图

最大权闭合子图指的是对于n个点,选择每个点都必须选择其子结点,然后其子结点连接过去的箭头的全部点也必须选, 每个点有其自己的权值,有正也有负,问如何选子图可以使得权值和相加最大。 就有一个算法,把所有点权值为正的相加为sum,然后建立一个源点s,连接全部权值为正的点,再建立一个汇点,由权值为负的...
  • qq_36124802
  • qq_36124802
  • 2017-08-15 00:45
  • 96

HOJ cake

Cake Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3685 Accepted Submission(s)...
  • zl1085372438
  • zl1085372438
  • 2017-02-12 17:28
  • 191

Delta-wave

Delta-wave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4914 &#...
  • evi__
  • evi__
  • 2014-02-08 11:58
  • 354
    个人资料
    • 访问:39176次
    • 积分:1033
    • 等级:
    • 排名:千里之外
    • 原创:59篇
    • 转载:48篇
    • 译文:0篇
    • 评论:4条
    最新评论