当前位置:网站首页>牛客网刷题——运算符问题
牛客网刷题——运算符问题
2022-07-30 16:26:00 【命由己造~】
一、不用加减乘除做加法
题目描述:
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
数据范围:两个数都满足 -10 \le n \le 1000−10≤n≤1000
进阶:空间复杂度 O(1)O(1),时间复杂度 O(1)O(1)
示例:
示例1
输入:1,2
返回值:3
示例2
输入:0,0
返回值:0
分析思路:
题目不让用四则运算符号,所以我们需要用位运算来达到加法的目的。
位运算的本质是对二进制位操作,通过找规律可以发现:
位运算中两数进行^(异或运算)可以提供两数加和后二进制非进位信息,位运算中的两数进行&(与运算)的结果可以提供两数加和后的二进制进位信息
如图:
通过观察可以发现:如果在没有进位的情况下两个二进制序列^后就为结果sum,在加上进位信息就是有进位的情况。所以当进位信息不为0时,我们就可以先左移进位信息^后得到非进制信息,再&看进位信息是否为0,如此循环。
int Add(int num1, int num2 ) {
// write code here
int add = num2;
int sum = num1;
while(add != 0)
{
int tmp = add ^ sum;
//进位信息
add = (sum & add) << 1;
sum = tmp;
}
return sum;
}
二、二进制中1的个数
题目描述:
输入一个整数 n ,输出该数32位二进制表示中1的个数。其中负数用补码表示。
数据范围:- 2^31 <= n <= -2^31-1−2^31<=n<=2^31−1
即范围为:-2147483648<= n <= 2147483647−2147483648<=n<=2147483647
示例一:
输入:10
返回值:2
说明:
十进制中10的32位二进制表示为0000 0000 0000 0000 0000 0000 0000 1010,其中有两个1。
示例二:
输入:-1
返回值:32
说明:
负数使用补码表示 ,-1的32位二进制表示为1111 1111 1111 1111 1111 1111 1111 1111,其中32个1
思路分析:
因为因负数用补码表示,故不能用连除法。
我们可以让目标数字右移&数字1,如果结果为1,就说明最后一位是1,就可以看到有多少位1
int NumberOf1(int n ) {
int count = 0;
int i = 0;
for(i = 0; i < 32; i++)
{
if((n >> i) & 1 == 1)
count++;
}
return count;
}
三、有限制求1+2+3+…+n
题目描述:
求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
数据范围: 0 < n \le 2000<n≤200
进阶: 空间复杂度 O(1)O(1) ,时间复杂度 O(n)O(n)
示例一:
输入:5
返回值:15
示例二:
输入:1
返回值:1
思路分析:
不让用乘除法和条件判断语句就说明不能使用公式法和循环。
这很容易就能想到递归:
一般递归是这么实现:
int Sum_Solution(int n)
{
if (n == 1)
{
return 1;
}
return n + Sum_Solution(n - 1);
}
但是这里还是会用到条件判断语句,那我们就可以巧用 && 的短路性质,如果&&的左边为假,右边就不会进行:
int Sum_Solution(int n ) {
// write code here
n && (n += Sum_Solution(n - 1));
return n;
}
边栏推荐
- [HMS core] [FAQ] A collection of typical questions about push kit, analysis services, and video editing services 3
- php how to query string occurrence position
- The case of five little pigs (five little pigs compare the size of the body weight)
- How to use Redis for distributed applications in Golang
- Scheduling_Channel_Access_Based_on_Target_Wake_Time_Mechanism_in_802.11ax_WLANs
- Qt 动态库与静态库
- Moonbeam创始人解读多链新概念Connected Contract
- 服务器装好系统的电脑怎么分区
- Qt 容器控件之Tab Widget 使用详解
- Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer
猜你喜欢

How to remove last character from string in php

Why is there no data reported when the application is connected to Huawei Analytics in the application debugging mode?

SMI 与 Gateway API 的 GAMMA 倡议意味着什么?

大型综合办公管理系统源码(OA+HR+CRM)源码免费分享

3D激光SLAM:LeGO-LOAM论文解读---特征提取部分

云风:不加班、不炫技,把复杂的问题简单化

huato hot update environment construction (DLL method hot update C# code)

php how to query string occurrence position

PCIE下载的驱动安装

DTSE Tech Talk丨Phase 2: 1 hour in-depth interpretation of SaaS application system design
随机推荐
PMP每日一练 | 考试不迷路-7.30(包含敏捷+多选)
Mirror stand to collect
Scheduling_Channel_Access_Based_on_Target_Wake_Time_Mechanism_in_802.11ax_WLANs
武汉星起航跨境电商有前景吗?亚马逊的未来趋势如何发展?
【HMS core】【FAQ】push kit, WisePlay DRM, Location Kit, Health Kit, 3D Modeling Kit, SignPal Kit Typical Questions Collection 4
[NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍
Explore CSAPP Experiment 2-bomb lab-Section 1
Leetcode 119. 杨辉三角 II
华为云WeLink携手伙伴,共建协同办公生态
huato hot update environment construction (DLL method hot update C# code)
数组和指针(2)
华为云数据治理生产线DataArts,让“数据‘慧’说话”
MySql 和 PostgreSQL 数据库 根据一张表update另一张表数据
应用OPC解决方案实现控制系统数据的安全交换
(1) Cloud computing technology learning - virtualized vSphere learning
数据的存储
Google engineer "code completion" tool; "Transformers NLP" accompanying book code; FastAPI development template; PyTorch model acceleration tool; cutting-edge papers | ShowMeAI News Daily
Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer
【HMS core】【FAQ】A collection of typical questions about Account, IAP, Location Kit and HarmonyOS 1
C#西门子S7 协议通过偏移量的方式读写PLC DB块