C + + to find the maximum value of two numbers

Source: Internet
Author: User

//do not use if,:? and other inference statements. Find the largest of the two numbers. 

#include <iostream> using namespace STD ; int Main () {int a =- Ten ; int b =-100 ; int C = (A + B + abs (A-B)/2 ; //abs (x) is a function of absolute value, a+b+ (the difference between A and b) is twice times the maximum number, divided by 2 is the maximum number. cout << c << Endl; return 0 ;}

#include <iostream>using namespace STD;intMain () {intA =Ten;intb = -;intc = A-B;//    intSr[] = {A, b};inti =unsigned(c) >> (sizeof(int)*8-1);//infer that the highest bit of C is 0 or 1,0 C is a positive number and 1 C is a negative number. This allows the size to be obtained.     //unsigned type of number, left to move, no matter how left to fill 0.     cout<< Sr[i] << Endl;//Check out the maximum value according to the subscript.

return 0;}

C + + to find the maximum value of two numbers

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.