编写一个程序,找到最小的三个整数,而无需使用任何运算符。
令3个输入数字为x,y和z。
方法1(重复减法)
取一个计数器变量c并将其初始化为0。在循环中,将x,y和z重复减去1,然后递增c。首先变成0的数字最小。循环终止后,c将保留最小值3。
C++
// C++ program to find Smallest
// of three integers without
// comparison operators
#include
using namespace std;
int smallest(int x, int y, int z)
{
int c = 0;
while (x && y && z) {
x--;
y--;
z--;
c++;
}
return c;
}
// Driver Code
int main()
{
int x = 12, y = 15, z = 5;
cout << "Minimum of 3 numbers is "
<< smallest(x, y, z);
return 0;
}
// This code is contributed
// by Akanksha Rai
C
// C program to find Smallest
// of three integers without
// comparison operators
#include
int smallest(int x, int y, int z)
{
int c = 0;
while (x && y && z) {
x--;
y--;
z--;
c++;
}
return c;
}
int main()
{
int x = 12, y = 15, z = 5;
printf("Minimum of 3 numbers is %d", smallest(x, y, z));
return 0;
}
Java
// Java program to find Smallest
// of three integers without
// comparison operators
class GFG {
static int smallest(int x, int y, int z)
{
int c = 0;
while (x != 0 && y != 0 && z != 0) {
x--;
y--;
z--;
c++;
}
return c;
}
public static void main(String[] args)
{
int x = 12, y = 15, z = 5;
System.out.printf("Minimum of 3"
+ " numbers is %d",
smallest(x, y, z));
}
}
// This code is contributed by Smitha Dinesh Semwal.
Python3
# Python3 program to find Smallest
# of three integers without
# comparison operators
def smallest(x, y, z):
c = 0
while ( x and y and z ):
x = x-1
y = y-1
z = z-1
c = c + 1
return c
# Driver Code
x = 12
y = 15
z = 5
print("Minimum of 3 numbers is",
smallest(x, y, z))
# This code is contributed by Anshika Goyal
C#
// C# program to find Smallest of three
// integers without comparison operators
using System;
class GFG {
static int smallest(int x, int y, int z)
{
int c = 0;
while (x != 0 && y != 0 && z != 0) {
x--;
y--;
z--;
c++;
}
return c;
}
// Driver Code
public static void Main()
{
int x = 12, y = 15, z = 5;
Console.Write("Minimum of 3"
+ " numbers is " + smallest(x, y, z));
}
}
// This code is contributed by Sam007
PHP
Javascript
C++
// C++ implementation of above approach
#include
using namespace std;
#define CHAR_BIT 8
/*Function to find minimum of x and y*/
int min(int x, int y)
{
return y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));
}
/* Function to find minimum of 3 numbers x, y and z*/
int smallest(int x, int y, int z)
{
return min(x, min(y, z));
}
// Driver code
int main()
{
int x = 12, y = 15, z = 5;
cout << "Minimum of 3 numbers is " << smallest(x, y, z);
return 0;
}
// This code is contributed by Code_Mech.
C
// C implementation of above approach
#include
#define CHAR_BIT 8
/*Function to find minimum of x and y*/
int min(int x, int y)
{
return y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));
}
/* Function to find minimum of 3 numbers x, y and z*/
int smallest(int x, int y, int z)
{
return min(x, min(y, z));
}
int main()
{
int x = 12, y = 15, z = 5;
printf("Minimum of 3 numbers is %d", smallest(x, y, z));
return 0;
}
Java
// Java implementation of above approach
class GFG
{
static int CHAR_BIT = 8;
// Function to find minimum of x and y
static int min(int x, int y)
{
return y + ((x - y) & ((x - y) >>
((Integer.SIZE/8) * CHAR_BIT - 1)));
}
// Function to find minimum of 3 numbers x, y and z
static int smallest(int x, int y, int z)
{
return Math.min(x, Math.min(y, z));
}
// Driver code
public static void main (String[] args)
{
int x = 12, y = 15, z = 5;
System.out.println("Minimum of 3 numbers is " +
smallest(x, y, z));
}
}
// This code is contributed by mits
Python3
# Python3 implementation of above approach
CHAR_BIT = 8
# Function to find minimum of x and y
def min(x, y):
return y + ((x - y) & \
((x - y) >> (32 * CHAR_BIT - 1)))
# Function to find minimum
# of 3 numbers x, y and z
def smallest(x, y, z):
return min(x, min(y, z))
# Driver code
x = 12
y = 15
z = 5
print("Minimum of 3 numbers is ",
smallest(x, y, z))
# This code is contributed
# by Mohit Kumar
C#
// C# implementation of above approach
using System;
class GFG
{
static int CHAR_BIT=8;
/*Function to find minimum of x and y*/
static int min(int x, int y)
{
return y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));
}
/* Function to find minimum of 3 numbers x, y and z*/
static int smallest(int x, int y, int z)
{
return Math.Min(x, Math.Min(y, z));
}
// Driver code
static void Main()
{
int x = 12, y = 15, z = 5;
Console.WriteLine("Minimum of 3 numbers is "+smallest(x, y, z));
}
}
// This code is contributed by mits
Javascript
C++
// C++ implementation of above approach
#include
using namespace std;
// Using division operator to find
// minimum of three numbers
int smallest(int x, int y, int z)
{
if (!(y / x)) // Same as "if (y < x)"
return (!(y / z)) ? y : z;
return (!(x / z)) ? x : z;
}
int main()
{
int x = 78, y = 88, z = 68;
cout << "Minimum of 3 numbers is " << smallest(x, y, z);
return 0;
}
// this code is contributed by shivanisinghss2110
C
#include
// Using division operator to find
// minimum of three numbers
int smallest(int x, int y, int z)
{
if (!(y / x)) // Same as "if (y < x)"
return (!(y / z)) ? y : z;
return (!(x / z)) ? x : z;
}
int main()
{
int x = 78, y = 88, z = 68;
printf("Minimum of 3 numbers is %d", smallest(x, y, z));
return 0;
}
Java
// Java program of above approach
class GfG {
// Using division operator to
// find minimum of three numbers
static int smallest(int x, int y, int z)
{
if ((y / x) != 1) // Same as "if (y < x)"
return ((y / z) != 1) ? y : z;
return ((x / z) != 1) ? x : z;
}
// Driver code
public static void main(String[] args)
{
int x = 78, y = 88, z = 68;
System.out.printf("Minimum of 3 numbers"
+ " is %d",
smallest(x, y, z));
}
}
// This code has been contributed by 29AjayKumar
python3
# Using division operator to find
# minimum of three numbers
def smallest(x, y, z):
if (not (y / x)): # Same as "if (y < x)"
return y if (not (y / z)) else z
return x if (not (x / z)) else z
# Driver Code
if __name__== "__main__":
x = 78
y = 88
z = 68
print("Minimum of 3 numbers is",
smallest(x, y, z))
# This code is contributed
# by ChitraNayal
C#
// C# program of above approach
using System;
public class GfG {
// Using division operator to
// find minimum of three numbers
static int smallest(int x, int y, int z)
{
if ((y / x) != 1) // Same as "if (y < x)"
return ((y / z) != 1) ? y : z;
return ((x / z) != 1) ? x : z;
}
// Driver code
public static void Main()
{
int x = 78, y = 88, z = 68;
Console.Write("Minimum of 3 numbers"
+ " is {0}",
smallest(x, y, z));
}
}
/* This code contributed by PrinciRaj1992 */
Javascript
输出:
Minimum of 3 numbers is 5
此方法不适用于负数。方法2也适用于负数。
方法2(使用位操作)
这篇文章的使用方法2找出两个数的最小值(我们不能用方法1的方法1层的用途比较运算符)。一旦我们具有查找最少2个数字的功能,便可以使用它查找最少3个数字。
C++
// C++ implementation of above approach
#include
using namespace std;
#define CHAR_BIT 8
/*Function to find minimum of x and y*/
int min(int x, int y)
{
return y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));
}
/* Function to find minimum of 3 numbers x, y and z*/
int smallest(int x, int y, int z)
{
return min(x, min(y, z));
}
// Driver code
int main()
{
int x = 12, y = 15, z = 5;
cout << "Minimum of 3 numbers is " << smallest(x, y, z);
return 0;
}
// This code is contributed by Code_Mech.
C
// C implementation of above approach
#include
#define CHAR_BIT 8
/*Function to find minimum of x and y*/
int min(int x, int y)
{
return y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));
}
/* Function to find minimum of 3 numbers x, y and z*/
int smallest(int x, int y, int z)
{
return min(x, min(y, z));
}
int main()
{
int x = 12, y = 15, z = 5;
printf("Minimum of 3 numbers is %d", smallest(x, y, z));
return 0;
}
Java
// Java implementation of above approach
class GFG
{
static int CHAR_BIT = 8;
// Function to find minimum of x and y
static int min(int x, int y)
{
return y + ((x - y) & ((x - y) >>
((Integer.SIZE/8) * CHAR_BIT - 1)));
}
// Function to find minimum of 3 numbers x, y and z
static int smallest(int x, int y, int z)
{
return Math.min(x, Math.min(y, z));
}
// Driver code
public static void main (String[] args)
{
int x = 12, y = 15, z = 5;
System.out.println("Minimum of 3 numbers is " +
smallest(x, y, z));
}
}
// This code is contributed by mits
Python3
# Python3 implementation of above approach
CHAR_BIT = 8
# Function to find minimum of x and y
def min(x, y):
return y + ((x - y) & \
((x - y) >> (32 * CHAR_BIT - 1)))
# Function to find minimum
# of 3 numbers x, y and z
def smallest(x, y, z):
return min(x, min(y, z))
# Driver code
x = 12
y = 15
z = 5
print("Minimum of 3 numbers is ",
smallest(x, y, z))
# This code is contributed
# by Mohit Kumar
C#
// C# implementation of above approach
using System;
class GFG
{
static int CHAR_BIT=8;
/*Function to find minimum of x and y*/
static int min(int x, int y)
{
return y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));
}
/* Function to find minimum of 3 numbers x, y and z*/
static int smallest(int x, int y, int z)
{
return Math.Min(x, Math.Min(y, z));
}
// Driver code
static void Main()
{
int x = 12, y = 15, z = 5;
Console.WriteLine("Minimum of 3 numbers is "+smallest(x, y, z));
}
}
// This code is contributed by mits
Java脚本
输出:
Minimum of 3 numbers is 5
方法3(使用部门运算符)
我们还可以使用除法运算符查找两个数的最小值。如果(a / b)的值为零,则b大于a,否则a较大。感谢gopinath和Vignesh提出了这种方法。
C++
// C++ implementation of above approach
#include
using namespace std;
// Using division operator to find
// minimum of three numbers
int smallest(int x, int y, int z)
{
if (!(y / x)) // Same as "if (y < x)"
return (!(y / z)) ? y : z;
return (!(x / z)) ? x : z;
}
int main()
{
int x = 78, y = 88, z = 68;
cout << "Minimum of 3 numbers is " << smallest(x, y, z);
return 0;
}
// this code is contributed by shivanisinghss2110
C
#include
// Using division operator to find
// minimum of three numbers
int smallest(int x, int y, int z)
{
if (!(y / x)) // Same as "if (y < x)"
return (!(y / z)) ? y : z;
return (!(x / z)) ? x : z;
}
int main()
{
int x = 78, y = 88, z = 68;
printf("Minimum of 3 numbers is %d", smallest(x, y, z));
return 0;
}
Java
// Java program of above approach
class GfG {
// Using division operator to
// find minimum of three numbers
static int smallest(int x, int y, int z)
{
if ((y / x) != 1) // Same as "if (y < x)"
return ((y / z) != 1) ? y : z;
return ((x / z) != 1) ? x : z;
}
// Driver code
public static void main(String[] args)
{
int x = 78, y = 88, z = 68;
System.out.printf("Minimum of 3 numbers"
+ " is %d",
smallest(x, y, z));
}
}
// This code has been contributed by 29AjayKumar
python3
# Using division operator to find
# minimum of three numbers
def smallest(x, y, z):
if (not (y / x)): # Same as "if (y < x)"
return y if (not (y / z)) else z
return x if (not (x / z)) else z
# Driver Code
if __name__== "__main__":
x = 78
y = 88
z = 68
print("Minimum of 3 numbers is",
smallest(x, y, z))
# This code is contributed
# by ChitraNayal
C#
// C# program of above approach
using System;
public class GfG {
// Using division operator to
// find minimum of three numbers
static int smallest(int x, int y, int z)
{
if ((y / x) != 1) // Same as "if (y < x)"
return ((y / z) != 1) ? y : z;
return ((x / z) != 1) ? x : z;
}
// Driver code
public static void Main()
{
int x = 78, y = 88, z = 68;
Console.Write("Minimum of 3 numbers"
+ " is {0}",
smallest(x, y, z));
}
}
/* This code contributed by PrinciRaj1992 */
Java脚本
输出:
Minimum of 3 numbers is 68