题目:求两个数的较大值,不能使用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. jump game(贪心算法)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. .NET之类型转换

    说起类型转换大家很容易的就会联想到将int类型转换成float类型或者是将double类型转转成int类型之类的转换.当然这可能是大多数人最先接触到的转换方式,也是最简单的转换方式.所谓转换就是从现有 ...

  3. HP电脑的增霸卡功能操作详解

    机房管理中HP电脑的增霸卡功能操作详解 一.软件去除保护 1).电脑开机后等待进入增霸卡选择系统界面: 2).按F1帮助,F10进入增霸卡BIOS界面: 3).光标切换到>>>系统还 ...

  4. .Net中的IO

    C#中的IO 本文环境为: Win 10 VS 2015 .net Framework 版本 4.0 File 类 File 类是一个工具类,可以用来判断文件是否存在和方便的创建文件, 读取.写入等操 ...

  5. iOS 11开发教程(一)

    iOS 11开发概述 iOS 11是目前苹果公司用于苹果手机和苹果平板电脑的最新的操作系统.该操作系统的测试版于2017年6月6号(北京时间)被发布.本章将主要讲解iOS 11的新特性.以及使用Xco ...

  6. mysql_connect的$new_link参数

    假设在127.0.0.1上有test1和test2两个库, 其中库test1库中有test1表,test2中有test2表 $servername = "127.0.0.1"; $ ...

  7. 2018-2019-2 20162318《网络攻防技术》Exp5 MSF基础应用

    1.实验内容 1.一个主动攻击实践,如ms08_067 2. 一个针对浏览器的攻击,如ms11_050) 3. 一个针对客户端的攻击,如Adobe 4. 成功应用任何一个辅助模块 2.基础问题回答 2 ...

  8. UVALive 6662 TheLastAnt

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  9. Linux的本地时间和网络时间同步

    Linux本地时间和网络时间不同步,更新了之后,重启还会变回去.可以通过一下方法修改并保存. 1.  安装ntpdate工具 sudo apt-get install ntpdate 2.  设置系统 ...

  10. Set常用子类特点

    HashSet:       重写   hashCode和equals方法                                        特点:无序,唯一      底层结构是:    ...