在给定a和b的情况下,不使用上限函数即可找到a / b的上限值。
例子:
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
使用ceiling函数可以解决该问题,但是当将整数作为参数传递时,ceiling函数不起作用。因此,下面有以下两种方法来找到上限值。
方法1:
ceilVal = (a / b) + ((a % b) != 0)
a / b返回整数除法值,并且(((a%b)!= 0)是一个检查条件,如果我们在a / b除法后还有余数,则返回1,否则返回0。整数除法值与检查值相加以获得最大值。
下面给出了上述方法的说明:
C++
// C++ program to find ceil(a/b)
// without using ceil() function
#include
#include
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
C++
// C++ program to find ceil(a/b)
// without using ceil() function
#include
#include
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
输出:
The ceiling value of 4/3 is 2
The ceiling value of 6/3 is 2
方法二:
ceilVal = (a+b-1) / b
使用简单的数学运算,我们可以将分母加到分子上并从中减去1,然后将其除以分母得到上限值。
下面给出了上述方法的说明:
C++
// C++ program to find ceil(a/b)
// without using ceil() function
#include
#include
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
输出:
The ceiling value of 4/3 is 2
The ceiling value of 6/3 is 2