当前位置:网站首页>leetcode 面试题 05.06. 整数转换
leetcode 面试题 05.06. 整数转换
2022-07-19 19:29:00 【蓝猫骑士】
今天遇到这样一个题目,犯了一个低级错误 想想就觉可笑,又得挺有意思的 特来分享一下;
题目是这样的:
这是我一开始写的带码 写完的时候,我还在洋洋得意 觉得太简单了,但是运行完就打脸了;
int convertInteger(int A, int B){
int count = 0;
int i = 0;
for(i = 0;i<32;i++)
{
if(A&1 != B&1)
{
count++;
}
A=A>>1;
B=B>>1;
}
return count;
}
为什么会和结果不一样呢?
这是因为运算优先级的问题。 但是这里 & 的优先级的确要低于 != 。
所以我们要给 运算加一对括号;
我们再深入的思考一下:
如果我们遇到这样的代码 a*b + c*d + e*f
我们知道他们之间其实到底是怎么运算的吗?
是先a*b c*d e*f 之后再加一起 还是a*b , c*d , a*b + c*d , e*f ,a*b + c*d + e*f
我们的的确确知道 * 的运算级高于 +
但是把并不一定 这就意味着 第三个 * 比第一个 + 早一步执行;
因为这里 无论是第一种 还是第二种 计算的结果是一样的;
但是 我们要知道 如果 有一天 有一个代码 你虽然知道他们之间的运算优先级,但是不知道他们之间是怎么进行运算的,像这样
int fun()
{
static int count = 1;
return ++count;
}
int main()
{
int answer;
answer = fun() - fun() * fun();
printf( "%d\n", answer);//输出多少?
return 0;
}
但是上述代码 answer = fun() - fun() * fun(); 中我们只能通过操作符的优先级得知:先算乘法,
再算减法。
函数的调用先后顺序无法通过操作符的优先级确定。
边栏推荐
- 1. Figure introduction to machine learning Basics
- 这 20 道 Redis 经典面试题你还不会,就别去面试了!
- Vs+Qt 界面添加背景图的两种方式(非常实用)
- Node, NVM, NRM, NPM, and yarn tutorial
- 【开发教程3】开源蓝牙心率防水运动手环-开发环境搭建
- 场馆系统好用吗
- 霸榜MS MARCO!基于Transformer的混合列表感知排序模型
- 《最伟大的作品》,解密周杰伦新专辑背后的数据密码
- 第六届”蓝帽杯“全国大学生网络安全技能大赛WriteUp
- Comment répondre aux exigences de sécurité de divers environnements en nuage à l'ère de l'économie numérique?
猜你喜欢
Li Kou classic binary tree topic
What aspects does the beauty salon store management system help store management?
场馆系统好用吗
【JVM学习02】JVM的垃圾回收
【无标题】
web:从10到1的编译大重构
"Xiaodeng in operation and maintenance" searches log data as network security intelligence
Wsl2 installation tutorial and modifying the default installation directory to other disks
LLVM pass pwn 入门 (3)
1. Figure introduction to machine learning Basics
随机推荐
《Reinforcement based mobile robot navigation in dynamic environment》翻译
Analyze Intel's path of continuous innovation in five dimensions!
Characteristics, advantages and disadvantages of closures in JS
学习使用php获取重定向后的url地址链接
编译UE5.0
How to install the blue bookmark plug-in in the secure browser?
【JVM学习01】JVM的内存管理
【微信小程序】picker滚动选择器(85/100)
Mysql database
3、Graph Neural Network
DNS原理及配置
Huawei 520 million, Xinhua 3 440 million, Inspur 240 million, Lenovo 230 million, Shenxin 200 million, Dell 170 million, smartx 70 million, dawn 60 million
【微信小程序】label(86/100)
[combinational logic circuit] - display decoder
5. Complex graph network
The server automatically preempts the GPU running program
Web3的企业如何用Token激励员工?
web:从10到1的编译大重构
Li Kou classic binary tree topic
"Xiaodeng in operation and maintenance" searches log data as network security intelligence