编写函数求两个整数 a 和 b 之间的较大值。要求不能使用if, while, switch, for, ?: 以 及任何的比较语句。

本题要求不能使用if, while, switch, for, ?: 以 及任何的比较语句,也就是要求我们不能用常规的方法来判断两个数的大小。

那么按照以往的方法,要判断两个数的大小,应该要将两个数进行减法运算,将结果与0进行比较。那现在不行进行比较,我们应该怎么办?

我们知道变量分为signed 和 unsigned 两种,有符号变量用最高位代表符号位。

  • 当变量值为负数时,变量值的最高位为1,
  • 当变量值为正数时,最高位为0

基于这种特性,我们可以用一个数组保存用于比较的两个数的值。如array[0]=b,array[1]=a;

将a,b做减法运算,并将结果值存放在一个有符号变量中,变量最高位对应的数组元素就是俩个数中较大的那个数。接下来看代码实现

int min(int a, int b)
{
    int d = a - b;
    int flag = ((unsigned int)d) >> 31;
    int array[] = {b, a};

    return array[flag];
}

int main(int argc, char *argv[])
{
    cout<< min(1, 2) <<endl;
    cout<< min(2, 1) <<endl;
    cout<< min(2, 10000)<<endl;

    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}
时间: 2024-08-13 14:56:53

编写函数求两个整数 a 和 b 之间的较大值。要求不能使用if, while, switch, for, ?: 以 及任何的比较语句。的相关文章

求两个整数的最大公约数

<C和指针>第7章第2道编程题: 两个整型值M和N(M.N均大于0)的最大公约数可以按照下面的方法计算: 请编写一个名叫gcd的函数,它接受两个整型参数,并返回这两个数的最大公约数.如果这两个参数中的任何一个不大于零,函数返回零. 1 /* 2 ** 求两个整数的最大公约数 3 */ 4 5 #include <stdio.h> 6 7 int gcd( int M, int N ); 8 9 int 10 main() 11 { 12 int m, n; 13 scanf( &q

12_1求两个整数中的较小值,要求不能使用比较运算符, if-else, a&gt;b?a:b, while for

转载请注明出处:http://www.cnblogs.com/wuzetiandaren/p/4253932.html  声明:现大部分文章为寻找问题时在网上相互转载,此博是为自己做个记录记录,方便自己也方便有类似问题的朋友,本文的思想也许有所借鉴,但源码均为本人实现,如有侵权,请发邮件表明文章和原出处地址,我一定在文章中注明.谢谢. 题目:求两个整数中的较小值,要求不能使用比较运算符, if-else, a>b?a:b, while for, 内嵌汇编递归第三方函数. 在网上看到一些网友给出了

欧几里得算法求两个整数的最大公因数

unsigned int Gcd (unsigned int m,unsigned int n){ unsigned int rem; while(n>0){ rem = m % n; m = n; n = rem; } return m; } 对于m<n的情况,第一次循环m,n会交换 算法的时间复杂度计算 时间复杂度 logn 若M > N,则第一次循环交换M和N. 若想分析其时间复杂度,则要求循环次数,即生成余数的次数. 可以证明: 当M > N, 则M % N < M

求两个整数的最大公约数和最小公倍数

//求最大公约数是用辗转相除法,最小公倍数是根据公式 m,n 的 最大公约数* m,n最小公倍数 = m*n 来计算 #include<stdio.h> //将两个整数升序排列 void ascNum(int *p1,int *p2) { int temp; if(*p1 > *p2) { temp = *p2; *p2 = *p1; *p1 = temp; } } //求两个整数的最大公约数 辗转相除法 int getAppr(int a, int b) { int c; ascNum

002:求两个整数的最大公约数和最小公倍数

求最大公约数可采用辗转相除法,其流程如图所示. 最小公倍数就是两个整数的乘积除以其最大公约数. 1 #include <stdio.h> 2 3 int main() 4 { 5 unsigned long a, b, c=0; //两个整数和临时变量 6 unsigned long lcm=0, gcd=0; //最小公倍数和最大公约数 7 8 while( 1 ) 9 { 10 printf("Please input two positive integers(spacebar

【c语言】求两个整数中的较大者

// 求两个整数中的较大者 #include <stdio.h> int max( int a, int b ) { int temp; if( a > b ) { temp = a; } else { temp = b; } return temp; } int main() { int a,b; printf("请输入要比较的两个数:\n"); scanf("%d %d",&a,&b); printf("大数是:%d\

求两个整数的乘积

// 求两个整数的乘积#include<stdio.h>int product (int,int);int main(void) { int x,y,s; scanf("%d %d",&x,&y); s = product(x,y); printf("The mul is:%d",s); return 0;}int product(int a,int b){ int mul; mul = a*b; return mul;} 原文地址:ht

C++求两个整数的最大公约数和最小公倍数

最小公倍数=两个整数的成绩 / 最大公约数 求最大公约数的方法: (1)辗转相除法 1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int a,b,tmp,m; 6 cin>>a>>b; 7 m=a*b; 8 if(a<b) 9 { 10 tmp=b; 11 b=a; 12 a=tmp; 13 } 14 while(b!=0) 15 { 16 tmp=a%b; 17 a=b; 18 b

C++程序设计实践指导1.5求两个整数集合并集改写要求实现

改写要求1:改写为单链表结构可以对任意长度整数集合求并集 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode { int data; LinkNode* next; }; class SET { public: struct LinkNode* creat(int x[],int len); struct LinkNode* copy(LinkNode* aHead); int no