求两个数中的较大值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 ...
随机推荐
- Java 中线程安全问题
不好意思,一个国庆假期给我放的都不知道东西南北了,放松,很放松,差一点就弃更了,感谢那些催更的小伙伴们! 虽然没有更新,但是日常的学习还是有的,以后我尽量给大家分享一些通用知识,非技术. 但是本期还是 ...
- Pearls in a Row CodeForces 620C 水题
题目:http://codeforces.com/problemset/problem/620/C 文章末有一些测试数据仅供参考 题目大意 给你一个数字串,然后将分成几个部分,要求每个部分中必须有一对 ...
- dhcp 提示could not load neutron.agent
错误日志如下: N版存在问题,其它版本不知道 解决方法(代码问题): /usr/lib/python2.7/site-packages/neutron/common/utils.py 在这个方法上(d ...
- luoguP3480 [POI2009]KAM-Pebbles 阶梯Nim
将序列差分并翻转之后,变成了阶梯\(Nim\)的模板题 QAQ #include <cstdio> #include <cstring> #include <iostre ...
- BZOJ 1029: [JSOI2007]建筑抢修 优先队列
1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec Memory Limit: 162 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- 选择问题(selection problem)
/* 本文是选择问题: 选择一组N个数当中的第k小的数(第k大的数类似) 集中方法的实现代码 */ #include "sorting.h" #incl ...
- weblogic安装使用: Could not Create the Java Virtual Machine
第一次使用weblogic,完全不明白是怎么一回事!找安装包花了大把时间!找到了不知道怎么安装 -- _ --||| 找了一篇安装文档<weblogic 安装部署手册.doc>, 位于:[ ...
- 重温PHP之冒泡排序
冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法. 它重复地走访过要排序的元素列,依次比较两个相邻的元素,如果他们的顺序(如从大到小.首字母从A到Z)错误就把他们交换过来.走 ...
- TeamViewer运行在Windows Server 2008下连接时错误提示:正在初始化显示参数
这个是使用远程桌面安装和使用Teamviewer的问题,解决方法: 实际上安装完成后TeamViewer有两个ID,一个是个人ID(就是上面卡住的780 567 914),另一个是服务器ID,我们通过 ...
- MAIN/autoslb.py · 林語/autoslb - 码云 - 开源中国
MAIN/autoslb.py · 林語/autoslb - 码云 - 开源中国 CloudXNS