题目:求两个数的较大值,不能使用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. Ionic实战二:购物车

    用户名密码都为空 此app功能主要有如下 1.首页轮播和商品列表展示 2.左侧侧滑页面分类展示 3.商品详情页面展示 以及购买 4.购物车 订单填写 支付等页面          

  2. poj-3268最短路

    title: poj-3268最短路 date: 2018-10-13 15:54:34 tags: acm 刷题 categories: ACM-最短路 概述 这是一道最短路的模板题,,,不过虽然是 ...

  3. mysql store procedure 存储过程

    参考资料: 1.http://blog.sina.com.cn/s/blog_52d20fbf0100ofd5.html 2.https://dev.mysql.com/doc/refman/5.7/ ...

  4. Django-url路由映射与views逻辑处理

    一.URL路由映射 路由映射模块,主要完成url与views视图函数的映射.当一个url请求到来时,会按照这个模块中的url地址从上到下进行匹配,如果匹配成功,将执行映射试图中的函数:反之将返回404 ...

  5. Django的orm中get和filter的不同

    Django的orm框架对于业务复杂度不是很高的应用来说还是不错的,写起来很方面,用起来也简单.对于新手来说查询操作中最长用的两个方法get和filter有时候一不注意就会犯下一些小错误.那么今天就来 ...

  6. GITC简单感触

    GITC短暂的2天,去参加主要是想参与其中,了解其他家的技术,技术使用, 那些大牛,及大牛公司,大牛团队的一些事. 早上的主会场主要是介绍和宣传.半小时后就出去逛逛外面的分会场: 参与听了下 智能硬件 ...

  7. Easy File Sharing Web Server 6.9远程溢出漏洞

    from struct import pack import socket,sys import os host="192.168.109.129" port=80 junk0 = ...

  8. Codeforces Round #294 (Div. 2)A - A and B and Chess 水题

    A. A and B and Chess time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. faceNet编译问题

    1.执行align_dataset_mtcnn.py出现无法导入检测模型的问题 a.现象如下 Creating networks and loading parameters Traceback (m ...

  10. php中赋值和引用真真的理解

    php的引用(就是在变量或者函数.对象等前面加上&符号) //最重要就是 删除引用的变量 ,只是引用的变量访问不了,但是内容并没有销毁 在PHP 中引用的意思是:不同的名字访问同一个变量内容. ...