求两个数中的较大值max(a,b)。(不用if,>)
题目:求两个数的较大值,不能使用if、>。
1.不使用if、>,还要比较大小,貌似就只能使用条件表达式:
x=<表达式1>?<表达式2>:<表达式3>; (表达式1为true时,返回表达式2;否则返回表达式3)
2. 本题目中使用条件表达式:
max(a.b)=<表达式1>? b:a; (表达式1为true时,返回b;否则返回a)
3.如何写表达式1,区分a与b的大小。(不用>)
可以使用位运算,判断a-b的符号位。符号位为1(负数),a<b;符号位为0(正数),a>=b。
bool运算,非0表示true,0表达false。
实例代码看如下:
#include<stdio.h> /**
(a-b)&0x80000000
32为机器,int,最高位置1
使用“与“运算判断a-b是否为非负数
*/
#define max(a,b) (((a-b)&0x80000000)?b:a)
int main()
{
printf("sizeof(int)=%d\n",sizeof(int));
printf("max(2,3)=%d\n",max(,));
printf("max(2,2)=%d\n",max(,));
printf("max(0,2)=%d\n",max(,));
return ;
}
输出:

求两个数中的较大值max(a,b)。(不用if,>)的更多相关文章
- [CareerCup] 17.4 Maximum of Two Numbers 两数中的较大值
17.4 Write a method which finds the maximum of two numbers. You should not use if-else or any other ...
- java求两个数中的大数
java求两个数中的大数 java中的max函数在Math中 应用如下: int a=34: int b=45: int ans=Math.max(34,45); 那么ans的值就是45.
- 【C语言】求两个数中不同的位的个数
//求两个数中不同的位的个数 #include <stdio.h> int count_different(int a, int b) { int count = 0; int c = a ...
- c# 求两个数中最大的值
1.三元运算符: class Program { static void Main(string[] args) { ,); Console.WriteLine("最大数:{0}" ...
- if语句求三个数中最大的
Console.WriteLine("请输入第一个数:"); int a = Convert.ToInt32( Console.ReadLine()); Console.Write ...
- LeetCode 421. 数组中两个数的最大异或值(Maximum XOR of Two Numbers in an Array) 71
421. 数组中两个数的最大异或值 421. Maximum XOR of Two Numbers in an Array 题目描述 给定一个非空数组,数组中元素为 a0, a1, a2, - , a ...
- Java实现 LeetCode 421 数组中两个数的最大异或值
421. 数组中两个数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, - , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算 ...
- C++中用辗转相除法求两个数的最大公约数和最小公倍数
两个数的最大公约数:不能大于两个数中的最小值,算法口诀:小的给大的,余数给小的,整除返回小的,即最大公约数,(res=max%min)==0? max=min,min=res return min; ...
- leetcode-747-Largest Number At Least Twice of Others(求vector的最大值和次大值)
题目描述: In a given integer array nums, there is always exactly one largest element. Find whether the l ...
随机推荐
- JQuery重定向
window.location.href = "这里写页面的路径"; 如:window.location.href ="www.baidu.com";
- Docker应用系列(六)| 如何去掉sudo及避免权限问题
一.如何在使用docker时去掉sudo 1.添加账户 $ sudo groupadd docker 2.授权给docker账户 sudo gpasswd -a yourname docker 3.重 ...
- Java 集合Collection——初学者参考,高手慎入(未完待续)
1.集合简介和例子 Collection,集合.和数学定义中的集合类似,把很多元素放在一个容器中,方便我们存放结果/查找等操作. Collection集合实际上是很多形式集合的一个抽象. 例如十九大就 ...
- 理解Linux的进程,线程,PID,LWP,TID,TGID
在Linux的top和ps命令中,默认看到最多的是pid (process ID),也许你也能看到lwp (thread ID)和tgid (thread group ID for the threa ...
- BoneBlack am335x利用SD卡烧写板卡上的emmc
参考ti论坛上面的一篇文章: 链接:https://pan.baidu.com/s/1SLSUbCRrIULJJf_BNI3sEQ 密码: hvem 自己稍微修改的debrick.sh 链接: htt ...
- 1016 Phone Bills (25)(25 point(s))
problem A long-distance telephone company charges its customers by the following rules: Making a lon ...
- Linux驱动之IIC总线
<作用> 电子设备中有很多IIC设备之间需要进行相互通信,这样就产生了IIC总线,常用来实现设备之间的数据通信. <IIC总线结构> IIC总线只有两条线,一条是串行数据线 ...
- Metasploit小技巧
meterpreter > 进入meterpreter之后即可进行一些相关木马操作了,下面是正式本章内容 首先可以查看帮助文档,命令“help”,挑常用操作来讲↓↓↓ ------------- ...
- Linux知识(7)----远程登录 和远程拷贝
一.远程登录 1.安装客户端 可以使用ssh(Secure Shell(缩写为SSH))来进行远程的登录.安装ssh的命令为: sudo apt-get install openssh-server ...
- centos 6.5安装VMware tools
系统描述:win7旗舰版64位系统+VMware Workstation10+CentOS6.5(win7系统上安装了VMware Workstation10虚拟化软件,在该虚拟化软件上安装了Cent ...