Without "If", "? : ", switch, or other judgment statement, ask for a larger or smaller number of two numbers

Source: Internet
Author: User
Tags abs

The following five methods are used to find out the larger number and the smaller number respectively. The smaller number of code is in the comment, but the test is not running.

int Find1 (int a, int b) {return ((A + B) + ABS (A-b))/2;
Return ((A + B)-ABS (A-b))/2;
	}/* When a is greater than B, a-b is positive, right move sizeof (int) * 8-1, the rightmost one is 0,0^1 = 0; When a is less than B, a-b is negative, and the rightmost one is 1,1^1 = 1 */int Find21 (int a, int b) {
	int c[2] = {A, b};
	int z = a-b;
	z = (z >> (sizeof (int) * 8-1)) & 1;
	return c[z];
	/* int c[2] = {b, a};
	int z = a-b;
	z = (z >> (sizeof (int) * 8-1)) & 1;
	return c[z];
	*/} int Find22 (int a, int b) {int flag = ((a-b) >> (sizeof (int) * 8-1)) & 1;
	Return (A-(a-b) * flag);
Return (b-(b-a) * flag);
	int Find3 (int a, int b) {int c[2] = {A, b};
	Return C[a < b];
return c[a > B];
    }/* This method is not able to handle a case with a negative number of two digits and a few 0 cases, but this is also listed here. A<b, a/b=0, so the divisor is b* (b/a), the divisor is b/a, the result is B a=b, a/b=1, so the divisor is a+b=2a, the divisor is 2, the result is a a>b, b/a=0, so the divisor is a A * (A/b), the divisor is a/b, the result is  
        A/int Find4 (int a, int b) {int larger = (A * (A/b) + b* (b/a))/(A/b + b/a); Long smaller = (b* (A/b) + A * (b/a))/(A/b + b/a);  
    return larger;
	int main () {int A, B;
		while (1) {cin >> a >> b;
		cout << Find1 (A, b) << "";
		cout << Find21 (A, b) << "";
		cout << Find22 (A, b) << "";
		cout << Find3 (A, b) << "";
	cout << Find4 (A, b) << Endl; }
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.