被C语言操作符优先级坑了
今天有一个枚举的题目的代码是这样的:
重点在于maxXor这个函数的实现,枚举两个数字,其中maxr保存了最大值的 i 异或 j , 可是这个程序执行结果大大出乎意外-_-. 然后就把 i 异或 j 的结果临时保存在int,进行比较,程序正确的执行了。原来是被操作符优先级坑到了。位操作的优先级比比较操作符的优先级更低。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
/*
* Complete the function below.
*/
int maxXor(int l, int r) {
int maxr = -1;
for(int i = l;i <= r;i++) {
for(int j = i;j <= r;j++) {
if( i^j > maxr ) {
maxr = i^j;
}
}
}
return maxr;
} int main() {
int res;
int _l;
cin >> _l; int _r;
cin >> _r; res = maxXor(_l, _r);
cout << res; return 0;
}
被C语言操作符优先级坑了的更多相关文章
- C语言操作符优先级
C语言操作符优先级 优先级 运算符 含 义 要求运算 对象的个数 结合方向 1 () [] -> . 圆括号 下标运算符 指向结构体成员运算符 结构体成员运算符 自左至右 2 ! 逻辑非运 ...
- C++ 语言操作符的优先级
cppreference.com -> C++ 操作符优先级 C++ 操作符优先级 优先级 操作符 1 () [] -> . :: ! ~ ++ ...
- C语言操作符
C语言操作符的分类: 算术操作符 逻辑运算符 位操作符 赋值操作符 单目操作符 关系操作符 条件操作符 逗号表达式 数组下标引用 函数调用 结构体成员使用 大体上,C语言的操作符具体就这么些, ...
- 一道c语言运算符优先级问题
一道c语言运算符优先级问题 #include <iostream> using namespace std; int main() { char test[] = {"This ...
- C语言运算符优先级及结合性
今天去翻了下C语言运算符的优先级和结合性,发现当初学习的时候就没认真记住,惭愧.发现一篇讲得不错的文章,编辑了下转来供以后翻阅. C语言运算符优先级表(由上至下,优先级依次递减) 运算符 结合性 () ...
- C语言 运算符优先级和结合方向
运算符优先级和结合方向 初级运算符( ).[ ].->.. 高于 单目运算符 高于 算数运算符(先乘除后加减) 高于 关系运算符 高于 逻辑运算符(不包括!) 高于 条件运算 ...
- C语言运算符优先级总结
一 写在开头1.1 本文内容本文内容为C语言中运算符优先级的总结.转载于:https://blog.csdn.net/huangblog/article/details/8271791,感谢原作者的付 ...
- C语言运算符优先级和ASCII表
1. C语言运算符优先级及结合性 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 -- () 圆括号 (表达式)/函数名(形参表) -- . 成 ...
- C++操作符优先级带来的错误
在刷LeetCode题目:190. 颠倒二进制位:颠倒给定的 32 位无符号整数的二进制位时,可以利用左移和右移操作符来实现数字翻转: 错误解法: class Solution { public: u ...
随机推荐
- 【python-proxy by sockets5】pysocks
pip install pysocks https://stackoverflow.com/questions/2317849/how-can-i-use-a-socks-4-5-proxy-with ...
- koa中上传文件到阿里云oss实现点击在线预览和下载
比较好的在线预览的方法: 跳转一个新的页面,里面放一个iframe标签,或者object标签 <iframe src="xxx"></iframe> < ...
- level 1 -- unit 4 -- where 引导的疑问句
where be sb/sth 询问地点,用where where is the restaurant? where are my socks ? where was tome just now? w ...
- 备忘录:在alpine上安装kvm
原文: https://wiki.alpinelinux.org/wiki/KVM KVM is an open source virtualization solution in a ke ...
- scala中Map和Set
scala中Set包含可变set和不可变Set,set的子类HashSet,各有一个扩展了可变和不可变的set特质. 可变set import scala.collection.mutable.Se ...
- [转]好文章:Android的AlertDialog详解
refer:http://www.2cto.com/kf/201205/131876.html AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDial ...
- Installshield Major upgrade
Major upagrade: delete old version firstly, then install new version. need to change [product code] ...
- 2018-10-29 A股主要指数的市盈率(PE)估值高度
全指材料(SH000987) - 2018-10-29日,当前值:11.9289,平均值:30.66,中位数:26.1407,当前 接近历史新低.全指材料(SH000987)的历史市盈率PE详情 全指 ...
- javascript生成m位随机数
根据时间生成m位随机数,最大13位随机数,并且不能保证首位不为0 function ran(m) { m = m > 13 ? 13 : m; var num = new Date().getT ...
- 禁止requests请求https的提示InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more
提示这个 InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from ...