检测两个整数是否有相反的符号

给定两个有符号整数,编写一个函数,如果给定整数的符号不同,则返回true,否则返回false。例如,函数应该返回true-1和+100,对于-100和-200应该返回false。该函数不应使用任何算术运算符。 设给定的整数为x和y。负数的符号位为1,正数的符号位为0。当x和y的符号相反时,它们的异或将符号位设为1。换句话说,当x和y有相反的符号时,x和y的XOR将是负数。下面的代码使用这种逻辑。

null

C++

// C++ Program to Detect
// if two integers have opposite signs.
#include<iostream>
using namespace std;
bool oppositeSigns( int x, int y)
{
return ((x ^ y) < 0);
}
int main()
{
int x = 100, y = -100;
if (oppositeSigns(x, y) == true )
cout << "Signs are opposite" ;
else
cout << "Signs are not opposite" ;
return 0;
}
// this code is contributed by shivanisinghss2110


C

// C++ Program to Detect
// if two integers have opposite signs.
#include<stdbool.h>
#include<stdio.h>
bool oppositeSigns( int x, int y)
{
return ((x ^ y) < 0);
}
int main()
{
int x = 100, y = -100;
if (oppositeSigns(x, y) == true )
printf ( "Signs are opposite" );
else
printf ( "Signs are not opposite" );
return 0;
}


JAVA

// Java Program to Detect
// if two integers have opposite signs.
class GFG {
static boolean oppositeSigns( int x, int y)
{
return ((x ^ y) < 0 );
}
public static void main(String[] args)
{
int x = 100 , y = - 100 ;
if (oppositeSigns(x, y) == true )
System.out.println( "Signs are opposite" );
else
System.out.println( "Signs are not opposite" );
}
}
// This code is contributed by prerna saini.


Python3

# Python3 Program to Detect
# if two integers have
# opposite signs.
def oppositeSigns(x, y):
return ((x ^ y) < 0 );
x = 100
y = 1
if (oppositeSigns(x, y) = = True ):
print ( "Signs are opposite" )
else :
print ( "Signs are not opposite" )
# This article is contributed by Prerna Saini.


C#

// C# Program to Detect
// if two integers have
// opposite signs.
using System;
class GFG {
// Function to detect signs
static bool oppositeSigns( int x, int y)
{
return ((x ^ y) < 0);
}
// Driver Code
public static void Main()
{
int x = 100, y = -100;
if (oppositeSigns(x, y) == true )
Console.Write( "Signs are opposite" );
else
Console.Write( "Signs are not opposite" );
}
}
// This code is contributed by Nitin Mittal.


PHP

<?php
// PHP Program to Detect if two
// integers have opposite signs.
function oppositeSigns( $x , $y )
{
return (( $x ^ $y ) < 0);
}
// Driver Code
$x = 100;
$y = -100;
if (oppositeSigns( $x , $y ) == true)
echo ( "Signs are opposite" );
else
echo ( "Signs are not opposite" );
// This code is contributed by vt_m.
?>


Javascript

<script>
// Javascript Program to Detect
// if two integers have opposite signs.
function oppositeSigns(x, y)
{
return ((x ^ y) < 0);
}
// Driver Code
let x = 100, y = -100;
if (oppositeSigns(x, y) == true )
document.write( "Signs are opposite" );
else
document.write( "Signs are not opposite" );
</script>


输出:

Signs are opposite

时间复杂性: O(1)

辅助空间: O(1)

资料来源: 检测两个整数是否有相反的符号 我们也可以通过使用两个比较运算符来解决这个问题。请参阅下面的代码。

CPP

bool oppositeSigns( int x, int y)
{
return (x < 0)? (y >= 0): (y < 0);
}


JAVA

class GFG{
static boolean oppositeSigns( int x, int y)
{
return (x < 0 )? (y >= 0 ): (y < 0 );
}
}
// This code contributed by umadevi9616


Python3

def oppositeSigns(x, y):
return (y > = 0 ) if (x < 0 ) else (y < 0 );
# This code is contributed by shivanisingjss2110


C#

using System;
class GFG{
static boo oppositeSigns( int x, int y)
{
return (x < 0)? (y >= 0): (y < 0);
}
}
// This code contributed by shivanisinghss2110


Javascript

<script>
function oppositeSigns(x, y)
{
return (x < 0)? (y >= 0): (y < 0);
}
// This code contributed by shivanisinghss2110
</script>


时间复杂性: O(1)

第一种方法更有效。第一种方法使用位异或和比较运算符。第二种方法使用两个比较运算符,与比较运算相比,按位异或运算效率更高。 我们也可以使用以下方法。它不使用任何比较运算符。该方法由洪亮提出,gaurav改进。

CPP

bool oppositeSigns( int x, int y)
{
return ((x ^ y) >> 31);
}


JAVA

import java.io.*;
class GFG {
static boolean oppositeSigns( int x, int y)
{
return ((x ^ y) >> 31 );
}
}
// This code is contributed by shivanisinghss2110


Python3

def oppositeSigns(x, y):
return ((x ^ y) >> 31 )
# this code is contributed by shivanisinghss2110


C#

using System;
class GFG {
static bool oppositeSigns( int x, int y)
{
return ((x ^ y) >> 31);
}
}
// This code is contributed by shivanisinghss2110


Javascript

<script>
function oppositeSigns(x, y)
{
return ((x ^ y) >> 31);
}
// This code is contributed by shivanisinghss2110
</script>


时间复杂性: O(1)

该函数仅适用于整数大小为32位的编译器。表达式基本上使用位运算符“>>”检查(x^y)的符号。如上所述,负数的符号位始终为1。符号位是二进制表示法中最左边的位。所以我们需要检查x^y的第32位(或最左边的位)是否为1。我们将x^y的值右移31,使符号位成为最低有效位。如果符号位为1,(x^y)>>31的值将为1,否则为0。

C++

// C++ Program to detect
// if two integers have opposite signs.
#include<iostream>
using namespace std;
bool oppositeSigns( int x, int y)
{
long long product = 1ll*x*y;
return (product<0);
}
int main()
{
int x = 100, y = -100;
if (oppositeSigns(x, y) == true )
cout << "Signs are opposite" ;
else
cout << "Signs are not opposite" ;
return 0;
}
// this code is contributed by shinjanpatra


Javascript

// JavaScript Program to detect
// if two integers have opposite signs.
function oppositeSigns(x,y)
{
const product = Number(x)*Number(y);
return (product<0);
}
// driver code
let x = 100, y = -100;
if (oppositeSigns(x, y) == true )
{
console.log( "Signs are opposite" );
}
else console.log( "Signs are not opposite" );
// this code is contributed by shinjanpatra


Python3

# Python Program to detect
# if two integers have opposite signs.
def oppositeSigns(x,y):
product = x * y
return (product< 0 )
# driver code
x = 100
y = - 100
if (oppositeSigns(x, y) = = True ):
print ( "Signs are opposite" )
else :
print ( "Signs are not opposite" )
# this code is contributed by shinjanpatra


方法: 基本的方法是计算两个整数的乘积,我们知道,两个符号相反的整数总是会产生一个负整数,我们只需要知道乘积是否为负。

时间复杂性 :O(1)

空间复杂性 :O(1)

如果您发现上述任何代码/算法不正确,请写下评论,或者寻找其他方法来解决相同的问题。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享