不可以!
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
1
-
描述
-
判断:两个数x、y的正负性。
要求:不可以使用比较运算符,即"<",">","<=",">=","==","!="。
-
输入
-
有多组数据,每组数据占一行,每一行两个数x,y。
x、y保证在int范围内。
输出
-
每组数据输出占一行。
如果两个数是一正一负,输出"Signs are opposite"
如果是同为正或同为负,输出"Signs are not opposot"
如果无法确定,输出"Signs can't be sure"
输出不包括引号
样例输入
-
1 1 -1 1
样例输出
-
Signs are not opposot Signs are opposite
提示
- 如对本题有何疑问,请到讨论区提问或邮件我! 来源
- 爱生活 上传者
问题链接:点击打开链接
问题分析:
不可以用比较运算符,可以先将两个数都取其符号位,然后看符号位是否相同即可。
取符号位用右移运算符,符号位是否相同可以用异或运算符。
百度百科:
在c++中,移位运算符有双目移位运算符:<<(左移)和>>(右移)。移位运算符组成的表达式也属于算术表达式,其值为算术值。左移运算是将一个二进制位的操作数按指定移动的位数向左移位,移出位被丢弃,右边的空位一律补0。右移运算是将一个二进制位的操作数按指定移动的位数向右移动,移出位被丢弃,左边移出的空位或者一律补0,或者补符号位,这由不同的机器而定。在使用补码作为机器数的机器中,正数的符号位为0,负数的符号位为1。(均由机器来定:这个不正确)(MSDN原文明确说明,右移对于无符号类型强制补0,对于有符号类型续补符号位)
^ 号即为异或运算符
代码:
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <vector> #include <queue> #include <stack> #include <map> #include <string> #include <algorithm> #include <iomanip> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { /*freopen("file/input.txt","r",stdin); freopen("file/output.txt","w",stdout);*/ int a,b; while(scanf("%d%d",&a,&b)!=EOF){ if(!(a*b)) { printf("Signs can't be sure\n"); }else if((a>>31)^(b>>31)){ printf("Signs are opposite\n"); }else{ printf("Signs are not opposot\n"); } } return 0; }
-
有多组数据,每组数据占一行,每一行两个数x,y。