Dado un número, la única operación permitida es multiplicar el número por 2. Calcula el número mínimo de operaciones para hacer que el número sea divisible por 10.
NOTA: Si no es posible convertir, imprime -1.
Ejemplos:
Entrada: 10
Salida: 0
Como el número dado es divisible por 10,
la respuesta es 0.
Entrada: 1
Salida: -1
Como al multiplicar con 2, dado el no. no se puede
convertir en un número que sea divisible por 10,
por lo tanto, la respuesta es -1.
Enfoque: cualquier número dado es divisible por 10 solo si el último dígito del número es 0. Para este problema, extraiga el último dígito del número de entrada y compruébelo de las siguientes maneras:
1) Si el último dígito es 0, entonces ya es divisible por 10, por lo que el número mínimo de pasos es 0.
2) Si el último dígito es 5, al multiplicarlo por 2 una vez lo hará divisible por 10, por lo que el número mínimo de pasos es 1.
3) Si el El último dígito es un número par o impar (aparte de 0 y 5), luego multiplicarlo por 2 cualquier número de veces solo producirá un número par, por lo que nunca podemos hacerlo divisible por 10. Por lo tanto, el número de pasos es -1.
C++
// C++ code for finding // number of operations #include <bits/stdc++.h> using namespace std; int multiplyBy2(int n) { int rem, value; // Find the last digit or remainder rem = n % 10; switch (rem) { // If the last digit is 0 case 0: value = 0; break; // If the last digit is 5 case 5: value = 1; break; // If last digit is other // than 0 and 5. default: value = -1; } return value; } // Driver code int main() { int n = 28; cout << multiplyBy2(n) << endl; n = 255; cout << multiplyBy2(n) << endl; return 0; }
Java
// JAVA code for finding // number of operations import java.io.*; class GFG { static int multiplyBy2(int n) { int rem, value; // Find the last digit // or remainder rem = n % 10; switch (rem) { // If the last digit is 0 case 0: value = 0; break; // If the last digit is 5 case 5: value = 1; break; // If last digit is other // than 0 and 5. default: value = -1; } return value; } // Driver code public static void main (String[] args) { int n = 28; System.out.println(multiplyBy2(n)); n = 255; System.out.println(multiplyBy2(n)); } } // This code is contributed // by shiv_bhakt.
Python3
# Python3 code for finding number # of operations def dig(argu): switcher = { 0: 0, 5: 1, } return switcher.get(argu, -1) def multiplyBy2(n): # Find the last digit or remainder rem = n % 10; return dig(rem); # Driver code n = 28; print(multiplyBy2(n)); n = 255; print(multiplyBy2(n)); # This code is contributed by mits
C#
// C# code for finding // number of operations using System; class GFG { static int multiplyBy2(int n) { int rem, value; // Find the last digit // or remainder rem = n % 10; switch (rem) { // If the last // digit is 0 case 0: value = 0; break; // If the last // digit is 5 case 5: value = 1; break; // If last digit is // other than 0 and 5. default: value = -1; break; } return value; } // Driver code public static void Main () { int n = 28; Console.WriteLine(multiplyBy2(n)); n = 255; Console.WriteLine(multiplyBy2(n)); } } // This code is contributed // by shiv_bhakt.
PHP
<?php // PHP code for finding // number of operations function multiplyBy2($n) { $rem; $value; // Find the last digit or remainder $rem = $n % 10; switch ($rem) { // If the last digit is 0 case 0: $value = 0; break; // If the last digit is 5 case 5: $value = 1; break; // If last digit is other // than 0 and 5. default: $value = -1; } return $value; } // Driver code $n = 28; echo multiplyBy2($n),"\n"; $n = 255; echo multiplyBy2($n),"\n"; // This code is contributed by aj_36 ?>
Javascript
<script> // Javascript code for finding // number of operations function multiplyBy2(n) { var rem, value; // Find the last digit // or remainder rem = n % 10; switch (rem) { // If the last digit is 0 case 0: value = 0; break; // If the last digit is 5 case 5: value = 1; break; // If last digit is other // than 0 and 5. default: value = -1; } return value; } // Driver code var n = 28; document.write(multiplyBy2(n)+"<br/>"); n = 255; document.write(multiplyBy2(n)); // This code is contributed by todaysgaurav </script>
-1 1
Método: Usar if-else anidado
C++
// C++ code to multiply given number // by 2 such that it is divisible by 10 // using nested if else #include <iostream> using namespace std; int main() { int n = 10; // checking if the number is divisible // by 10 or not if divisible print 0 if (n % 10 == 0) cout << 0; // if not divisible by 10 then multiply // it by 2 and check again else{ n = n*2; if (n % 10 == 0) cout<<0; else cout<<-1; } return 0; } // This code is contributed by akashish__
Java
// Java code to multiply given number // by 2 such that it is divisible by 10 // using nested if else import java.io.*; class GFG { public static void main (String[] args) { int n = 10; // checking if the number is divisible // by 10 or not if divisible print 0 if (n % 10 == 0) System.out.println("0"); // if not divisible by 10 then multiply // it by 2 and check again else{ n = n*2; if (n % 10 == 0) System.out.println("0"); else System.out.println("-1"); } } } // This code is contributed by laxmigangarajula03.
Python3
# Python code to multiply given number # by 2 such that it is divisible by 10 # using nested if else n = 10 # checking if the number is divisible # by 10 or not if divisible print 0 if n % 10 == 0: print(0) # if not divisible by 10 then multiply # it by 2 and check again else: n = n*2 if n % 10 == 0: print(0) else: print(-1) # this code is contributed by gangarajula laxmi
C#
// C# code to multiply given number // by 2 such that it is divisible by 10 // using nested if else using System; public class GFG{ static public void Main (){ // Code int n = 10; // checking if the number is divisible // by 10 or not if divisible print 0 if (n % 10 == 0) { Console.WriteLine("0"); } // if not divisible by 10 then multiply // it by 2 and check again else{ n = n*2; if (n % 10 == 0) { Console.WriteLine("0"); } else { Console.WriteLine("-1"); } } } } // This code is contributed by akashish__
Javascript
<script> // JavaScript code for the above approach let n = 10; // checking if the number is divisible // by 10 or not if divisible print 0 if (n % 10 == 0) document.write(0); // if not divisible by 10 then multiply // it by 2 and check again else { n = n * 2; if (n % 10 == 0) document.write(0); else document.write(-1); } // This code is contributed by Potta Lokesh </script>
0
Publicación traducida automáticamente
Artículo escrito por RohanDeoli y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA