算法 - 可以从一个点(a,b)到达另一个点(C,d)。 [英] Algorithm for - can reach from one point(a, b) to other (C, d).

查看:152
本文介绍了算法 - 可以从一个点(a,b)到达另一个点(C,d)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题 的算法是什么 -  
如果输入(a,b) 可以达到(c,d)即(a,b)你可以去(a + b) ,b)或(a,a + b)n次。答案应 '是'或'否'。
eg- ie if ur(a,b) - ( 1 4 )它可以移动到( 5 4 )==>> ( 5 9 )==>>( 5 14 )等等。
Java语法 -
字符串 可能( int a, int b, int c, int d)。 {}





我尝试过:



我认为图形会有所帮助,但我不确定。

解决方案

(x0,y0)如果x0可以达到(x1,y1) x1和y0可以达到y1,假设x0 <= x1且y0 <= y1,然后使用bool指示(x0,y0)是否可以达到(x1,y1)以下可能会这样做



 使用系统; 

命名空间 CanReach
{
internal class 程序
{
public static int CanReachOne( int x, int y, int tx)
{
int f = 0 ;
while (x < tx)
{
x + = Ÿ;
f ++;
}
返回 x == tx? f:-1;
}

public static bool CanReach( int a, int b, int c, int d)
{
int aCan = CanReachOne(a,b,c);
int bCan = CanReachOne(b,a,d);
返回 aCan > -1&& bCan > -1;
}

private static void Main( string [] args)
{

Console.WriteLine( ({0},{1})=>({2},{3})= {4} 1 4 5 14
CanReach( 1 4 5 14 ));
Console.ReadLine();
}
}
}


我们不做你的HomeWork。

HomeWork是不会在乞求其他人做你的工作时测试你的技能,它将帮助你的老师检查你对所学课程的理解以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,试一试,重读你的课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。

Quote:

我认为图形会有所帮助,但我不确定。

如果图形可以提供帮助,你应该知道原因,否则就是猜测。

看看声明:你在坐标(a,b)的地图上,你需要在每个步骤上达到坐标(c,d),你只允许2次移动。试着把它放在一起。



出于测试目的,尝试从(1,2)到(8,13)

路径是(1,2)=> (3,2)=> (3,5)=> (8,5)=> (8,13)


What would be the algorithm for following question- 
If you are given input(a,b) is it possible to reach(c,d) i.e from (a,b) you can go (a+b,b) or (a,a+b) n number of times .Answers should be in ‘yes’ or ‘No’.
 eg- i.e if ur (a,b) is -(1,4) it can move to (5,4)==>> (5,9)==>>(5,14) and so on. 
Java Syntax-
 String is Possible(int a, int b, int c, int d). { }



What I have tried:

I think graph will help , but I am not sure about it .

解决方案

(x0, y0) can reach (x1, y1) if x0 can reach x1 and y0 can reach y1, assuming x0 <= x1 and y0 <= y1, then using bool to indicate whether (x0, y0) can reach (x1, y1) the following might do it

using System;

namespace CanReach
{
    internal class Program
    {
        public static int CanReachOne(int x, int y, int tx)
        {
            int f = 0;
            while (x < tx)
            {
                x += y;
                f++;
            }
            return x == tx ? f : -1;
        }

        public static bool CanReach(int a, int b, int c, int d)
        {
            int aCan = CanReachOne(a, b, c);
            int bCan = CanReachOne(b, a, d);
            return aCan > -1 && bCan > -1;
        }

        private static void Main(string[] args)
        {

            Console.WriteLine("({0},{1}) => ({2},{3}) = {4}", 1, 4, 5, 14, 
                              CanReach(1, 4, 5, 14));
            Console.ReadLine();
        }
    }
}


We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

Quote:

I think graph will help , but I am not sure about it .

If a graph can help, you should know why, otherwise it is a guess.
Look at the statements: you are in map at coordinates (a,b) and you need to reach coordinates (c,d) on each steps, you are only allowed 2 moves. Try to put this together.

For testing purpose, try to go from (1,2) to (8,13)
The path is (1,2) => (3,2) => (3,5) => (8,5) => (8,13)


这篇关于算法 - 可以从一个点(a,b)到达另一个点(C,d)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆