Dados dos puntos, es decir (a, 0) a (b, 0). La tarea es comprobar si es posible pasar de (a,0) a (b,0) o no. Uno puede moverse como (a, 0), (a+x, 0), (a+x+1, 0), (a, 2*x, 0), (a, 2*x+1, 0)… …
Ejemplos:
Input: a = 3, x = 10, b = 4 Output: No Input: a = 3, x = 2, b = 5 Output: Yes
Enfoque: Una respuesta será posible si
- a + n*x = b, donde n es un número entero no negativo.
- a + n*x + 1 = b donde n es un número entero positivo.
Entonces,
(b – a) / x es un número entero o (b – a – 1) / x es un número entero
(b – a) % x = 0 o (b – a – 1) % x = 0
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to move form // (a, 0) to (b, 0) with given jumps #include <bits/stdc++.h> using namespace std; // Function to check if it is possible bool Move(int a, int x, int b) { if ((((b - a) % x == 0) || ((b - a - 1) % x == 0) && a + 1 != b) && b >= a) return true; return false; } // Driver code int main() { int a = 3, x = 2, b = 7; // function call if (Move(a, x, b)) cout << "Yes"; else cout << "No"; }
C
// C program to move form // (a, 0) to (b, 0) with given jumps #include <stdio.h> #include <stdbool.h> // Function to check if it is possible bool Move(int a, int x, int b) { if ((((b - a) % x == 0) || ((b - a - 1) % x == 0) && a + 1 != b) && b >= a) return true; return false; } // Driver code int main() { int a = 3, x = 2, b = 7; // function call if (Move(a, x, b)) printf("Yes"); else printf("No"); } // This code is contributed by kothavvsaakash.
Java
// Java program to move form // (a, 0) to (b, 0) with given jumps import java.io.*; class GFG { // Function to check if it is possible static boolean Move(int a, int x, int b) { if ((((b - a) % x == 0) || ((b - a - 1) % x == 0) && a + 1 != b) && b >= a) return true; return false; } // Driver code public static void main (String[] args) { int a = 3, x = 2, b = 7; // function call if (Move(a, x, b)) System.out.println( "Yes"); else System.out.println( "No"); } } //This code is contributed by shs..
Python 3
# Python 3 program to move form # (a, 0) to (b, 0) with given jumps # Function to check if it # is possible def Move(a, x, b): if ((((b - a) % x == 0) or ((b - a - 1) % x == 0) and a + 1 != b) and b >= a): return True return False # Driver code if __name__ == "__main__": a = 3 x = 2 b = 7 # function call if (Move(a, x, b)): print("Yes") else: print("No") # This code is contributed # by ChitraNayal
C#
// C# program to move form // (a, 0) to (b, 0) with given jumps using System; class GFG { // Function to check if it is possible static bool Move(int a, int x, int b) { if ((((b - a) % x == 0) || ((b - a - 1) % x == 0) && a + 1 != b) && b >= a) return true; return false; } // Driver code public static void Main () { int a = 3, x = 2, b = 7; // function call if (Move(a, x, b)) Console.WriteLine( "Yes"); else Console.WriteLine( "No"); } } // This code is contributed // by inder_verma
PHP
<?php // PHP program to move form // (a, 0) to (b, 0) with given jumps // Function to check if it is possible function Move($a, $x, $b) { if (((($b - $a) % $x == 0) || (($b - $a - 1) % $x == 0) && $a + 1 != $b) && $b >= $a) return true; return false; } // Driver code $a = 3; $x = 2; $b = 7; // function call if (Move($a, $x, $b)) echo "Yes"; else echo"No"; // This code is contributed // by anuj_67 ?>
Javascript
<script> // Javascript program to move form // (a, 0) to (b, 0) with given jumps // Function to check if it is possible function Move(a,x,b) { if ((((b - a) % x == 0) || ((b - a - 1) % x == 0) && a + 1 != b) && b >= a) return true; return false; } // Driver code let a = 3, x = 2, b = 7; // function call if (Move(a, x, b)) document.write( "Yes"); else document.write( "No"); // This code is contributed by avanitrachhadiya2155 </script>
Producción:
Yes
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA