Dados a y b, encuentre el valor máximo de a/b sin usar la función de techo.
Ejemplos:
Input : a = 5, b = 4 Output : 2 Explanation: a/b = ceil(5/4) = 2 Input : a = 10, b = 2 Output : 5 Explanation: a/b = ceil(10/2) = 5
El problema se puede resolver usando la función de techo, pero la función de techo no funciona cuando se pasan números enteros como parámetros. Por lo tanto, existen los siguientes 2 enfoques a continuación para encontrar el valor máximo.
Enfoque 1:
ceilVal = (a / b) + ((a % b) != 0)
a/b devuelve el valor de la división de enteros, y ((a % b) != 0) es una condición de verificación que devuelve 1 si nos queda algún resto después de la división de a/b, de lo contrario devuelve 0. El valor de la división de enteros se suma con el valor de verificación para obtener el valor máximo.
A continuación se muestra la ilustración del enfoque anterior:
C++
// C++ program to find ceil(a/b) // without using ceil() function #include <cmath> #include <iostream> using namespace std; // Driver function int main() { // taking input 1 int a = 4; int b = 3; int val = (a / b) + ((a % b) != 0); cout << "The ceiling value of 4/3 is " << val << endl; // example of perfect division // taking input 2 a = 6; b = 3; val = (a / b) + ((a % b) != 0); cout << "The ceiling value of 6/3 is " << val << endl; return 0; }
Java
// Java program to find // ceil(a/b) without // using ceil() function import java.io.*; class GFG { // Driver Code public static void main(String args[]) { // taking input 1 int a = 4; int b = 3, val = 0; if((a % b) != 0) val = (a / b) + (a % b); else val = (a / b); System.out.println("The ceiling " + "value of 4/3 is " + val); // example of perfect // division taking input 2 a = 6; b = 3; if((a % b) != 0) val = (a / b) + (a % b); else val = (a / b); System.out.println("The ceiling " + "value of 6/3 is " + val); } } // This code is contributed by // Manish Shaw(manishshaw1)
Python3
# Python3 program to find ceil(a/b) # without using ceil() function import math # Driver Code # taking input 1 a = 4; b = 3; val = (a / b) + ((a % b) != 0); print("The ceiling value of 4/3 is", math.floor(val)); # example of perfect division # taking input 2 a = 6; b = 3; val = int((a / b) + ((a % b) != 0)); print("The ceiling value of 6/3 is", val); # This code is contributed by mits
C#
// C# program to find ceil(a/b) // without using ceil() function using System; class GFG { // Driver Code static void Main() { // taking input 1 int a = 4; int b = 3, val = 0; if((a % b) != 0) val = (a / b) + (a % b); else val = (a / b); Console.WriteLine("The ceiling " + "value of 4/3 is " + val); // example of perfect // division taking input 2 a = 6; b = 3; if((a % b) != 0) val = (a / b) + (a % b); else val = (a / b); Console.WriteLine("The ceiling " + "value of 6/3 is " + val); } } // This code is contributed by // Manish Shaw(manishshaw1)
PHP
<?php // PHP program to find ceil(a/b) // without using ceil() function // Driver function // taking input 1 $a = 4; $b = 3; $val = ($a / $b) + (($a % $b) != 0); echo "The ceiling value of 4/3 is " , floor($val) ,"\n"; // example of perfect division // taking input 2 $a = 6; $b = 3; $val = ($a / $b) + (($a % $b) != 0); echo "The ceiling value of 6/3 is " , $val ; // This code is contributed by anuj_67. ?>
Javascript
<script> // javascript program to find // ceil(a/b) without // using ceil() function // Driver Code // taking input 1 var a = 4; var b = 3, val = 0; if ((a % b) != 0) val = parseInt(a / b) + (a % b); else val = parseInt(a / b); document.write("The ceiling " + "value of 4/3 is " + val+"<br/>"); // example of perfect // division taking input 2 a = 6; b = 3; if ((a % b) != 0) val = parseInt(a / b) + (a % b); else val = parseInt(a / b); document.write("The ceiling " + "value of 6/3 is " + val); // This code is contributed by gauravrajput1 </script>
The ceiling value of 4/3 is 2 The ceiling value of 6/3 is 2
Enfoque 2:
ceilVal = (a+b-1) / b
Usando matemáticas simples, podemos agregar el denominador al numerador y restarle 1 y luego dividirlo por el denominador para obtener el valor máximo.
A continuación se muestra la ilustración del enfoque anterior:
C++
// C++ program to find ceil(a/b) // without using ceil() function #include <cmath> #include <iostream> using namespace std; // Driver function int main() { // taking input 1 int a = 4; int b = 3; int val = (a + b - 1) / b; cout << "The ceiling value of 4/3 is " << val << endl; // example of perfect division // taking input 2 a = 6; b = 3; val = (a + b - 1) / b; cout << "The ceiling value of 6/3 is " << val << endl; return 0; }
Java
// Java program to find ceil(a/b) // without using ceil() function class GFG { // Driver Code public static void main(String args[]) { // taking input 1 int a = 4; int b = 3; int val = (a + b - 1) / b; System.out.println("The ceiling value of 4/3 is " + val); // example of perfect division // taking input 2 a = 6; b = 3; val = (a + b - 1) / b; System.out.println("The ceiling value of 6/3 is " + val ); } } // This code is contributed by Jaideep Pyne
Python3
# Python3 program to find # math.ceil(a/b) without # using math.ceil() function import math # Driver Code # taking input 1 a = 4; b = 3; val = (a + b - 1) / b; print("The ceiling value of 4/3 is ", math.floor(val)); # example of perfect division # taking input 2 a = 6; b = 3; val = (a + b - 1) / b; print("The ceiling value of 6/3 is ", math.floor(val)); # This code is contributed by mits
C#
// C# program to find ceil(a/b) // without using ceil() function using System; class GFG { // Driver Code public static void Main() { // taking input 1 int a = 4; int b = 3; int val = (a + b - 1) / b; Console.WriteLine("The ceiling" + " value of 4/3 is " + val); // example of perfect division // taking input 2 a = 6; b = 3; val = (a + b - 1) / b; Console.WriteLine("The ceiling" + " value of 6/3 is " + val ); } } // This code is contributed by anuj_67.
PHP
<?php // PHP program to find ceil(a/b) // without using ceil() function // Driver function // taking input 1 $a = 4; $b = 3; $val = ($a + $b - 1) /$b; echo "The ceiling value of 4/3 is " , floor($val) ,"\n"; // example of perfect division // taking input 2 $a = 6; $b = 3; $val = ($a + $b - 1) / $b; echo "The ceiling value of 6/3 is " ,floor($val) ; // This code is contributed by anuj_67. ?>
Javascript
<script> // javascript program to find ceil(a/b) // without using ceil() function // Driver Code // taking input 1 var a = 4; var b = 3; var val = (a + b - 1) / b; document.write("The ceiling value of 4/3 is " + val+"<br/>"); // example of perfect division // taking input 2 a = 6; b = 3; val = parseInt((a + b - 1) / b); document.write("The ceiling value of 6/3 is " + val); // This code contributed by Rajput-Ji </script>
The ceiling value of 4/3 is 2 The ceiling value of 6/3 is 2
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)