题目:求两个数的较大值,不能使用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,>)的更多相关文章

  1. [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 ...

  2. java求两个数中的大数

    java求两个数中的大数 java中的max函数在Math中 应用如下: int a=34: int b=45: int ans=Math.max(34,45); 那么ans的值就是45.

  3. 【C语言】求两个数中不同的位的个数

    //求两个数中不同的位的个数 #include <stdio.h> int count_different(int a, int b) { int count = 0; int c = a ...

  4. c# 求两个数中最大的值

    1.三元运算符: class Program { static void Main(string[] args) { ,); Console.WriteLine("最大数:{0}" ...

  5. if语句求三个数中最大的

    Console.WriteLine("请输入第一个数:"); int a = Convert.ToInt32( Console.ReadLine()); Console.Write ...

  6. 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 ...

  7. Java实现 LeetCode 421 数组中两个数的最大异或值

    421. 数组中两个数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, - , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算 ...

  8. C++中用辗转相除法求两个数的最大公约数和最小公倍数

    两个数的最大公约数:不能大于两个数中的最小值,算法口诀:小的给大的,余数给小的,整除返回小的,即最大公约数,(res=max%min)==0?  max=min,min=res return min; ...

  9. 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 ...

随机推荐

  1. Django网站搭建(相关命令-Win10环境)

    为什么需要使用virtualenv? 虚拟环境的作用是创建一个独立的python环境,将每一个应用的环境独立开来,解决了不同应用及多版本之间的冲突问题.在开发的日常中,我们会将python及配套的应用 ...

  2. 机器学习之路:python 集成回归模型 随机森林回归RandomForestRegressor 极端随机森林回归ExtraTreesRegressor GradientBoostingRegressor回归 预测波士顿房价

    python3 学习机器学习api 使用了三种集成回归模型 git: https://github.com/linyi0604/MachineLearning 代码: from sklearn.dat ...

  3. .vs目录有什么用?

    写这篇博文的目的就是方便后来者能够在百度里轻松搜到. 反正我找了半天没找到关于.vs目录的介绍,最后还是在同事的帮助下才找到的. 参考地址:https://developercommunity.vis ...

  4. git服务器安装和配置

    http://www.cnblogs.com/dee0912/p/5815267.html

  5. Get started with IDA and disassembly SH7058

    http://www.romraider.com/forum/viewtopic.php?f=25&t=6303 All of the 16-bit guidance in the follo ...

  6. Syncthing -- 开源的云储存和同步服务工具

    Syncthing  -- an open-source file synchronization client/server application Syncthing是一个开源的云存储和同步服务工 ...

  7. Leptonica在VS2010中的编译及简单使用举例

    在tesseract-ocr中会用到leptonica库.这里对leptonica简介下. Leptonica是一个开源的图像处理和图像分析库,它的license是BSD 2-clause.它主要包括 ...

  8. winform datagridview 打印

    转载:http://www.cnblogs.com/Irving/archive/2012/10/12/2721666.html c#实现打印功能 http://www.cnblogs.com/zhc ...

  9. powerdesigner反向SQLServer2008数据库生成物理数据模型

    方法一:通过数据库脚本生成物理数据模型 具体步骤如下图所示:

  10. Remon Spekreijse CSerialPort串口类的修正版2014-01-10

    转自:http://m.blog.csdn.net/blog/itas109/18358297# 2014-1-16阅读691 评论0 如需转载请标明出处:http://blog.csdn.net/i ...