Interesting C + + algorithm _ (: З"∠) ___web

Source: Internet
Author: User
Summary

In the brush algorithm problem in the process, by some great God's idea of admiration, Fang know behind, mountain outside the mountain _ (: З"∠) _

NOTICE: The following code is based on c++11 to achieve the judgment palindrome

BOOL Judge (const string& str)
{
    string _str = str;
    Reverse (Str.begin (), Str.end ());
    return str==_str;
}
Convert into system
void Trans ()
{
    int dec_num = 0;
    cin>>hex>>dec_num;
    cout<<dec_num;
}

input:0x16
output:22
void Trans ()
{
    int dec_num =;
    cout<
To find 1 of the complement of an integer or negative number
public int NumberOf1 (int n) { 
    int count = 0; 
    while (n!= 0) { 
        count++; 
        n = n & (n-1);
     } 
    return count; 

For example: A binary number of 1100, from the right number three is at the rightmost 1. Minus 1, the third digit becomes 0, and the two-bit 0 behind it turns 1, and the previous 1 stays the same, so the result is 1011. We found that the result of minus 1 is to reverse all the bits that start at the far right 1. This time, if we do the same with the original integer and minus 1 after the result of the operation, from the original integer rightmost 1 that one start all bits will become 0. such as 1100&1011=1000. That is, by subtracting 1 from an integer and then doing it with the original integer, the rightmost 1 of the integer is converted to 0. How many times can you do this if the binary of an integer has 1? No subtraction, no additions.

Q: Write a function that asks for the sum of two integers, requiring that the + 、-、 *,/arithmetic symbol not be used in the body of the function.

S

int Add (int num1, int num2) {
    while (num2!=0) {//This cannot be written as num2>0 because there are negative numbers.
        int tmp = NUM1^NUM2;
        num2 = (num1&num2) <<1;
        NUM1 = tmp;
    }
    return NUM1;
}

First look at how the decimal is done: 5+7=12, three steps.
The first step: Add your values, do not count, get 2.
Step two: Calculate the carrying value, get 10. If this step carries a value of 0, then the first step is the final result.

The third step: Repeat the above two steps, just add the value into the above two steps obtained results 2 and 10, get 12.

Again, we can compute the binary value by three steps: 5-101,7-111 The first step: Add your values, do not count, get 010, binary each add equivalent to each of you do different or operation, 101^111.

The second step: Calculate the carry value, get 1010, the equivalent of you do and operation to get 101, and then move to the left one get 1010, (101&111) <<1.

The third step repeats the above two steps, everybody adds 010^1010=1000, the Carry value is 100= (010&1010) <<1.
Continue to repeat the above two steps: 1000^100 = 1100, carry value is 0, jump out of the loop, 1100 is the final result. Ask 1+2+3+...+n

Q: Must not use the multiplication and division method, for, while, if, else, switch, case and other keywords and conditional judgment statement (A? B:C).

S: A sudden deadly using the && operator

int sum_solution (int n) {
    int ans = n;
    Ans && (ans+=sum_solution (n-1));
    return ans;
}
To find a string substring of a rotation

Q: A string that asks for a string to rotate before and after the nth character

such as String (ABCDEF), if n is 3, the output is string (DEFABC)

Solution One:

void ReverseString (string A, int n) {return
    (a+a). substr (N,a.size ());
}

Solution Two: This comparison has the technical content

Assuming that the string two we are seeking is divided into a and B, the original string is AB, then we are asking for a BA, which has the following relation:
BA = (A ' B ') ' (here we assume that X ' is the transpose of X ')

The code is as follows

void ReverseString (string A, int n) {
    reverse (a.begin (), A.begin () +n);
    Reverse (A.begin () +n, A.end ());
    Reverse (A.begin (), A.end ());
    return A;
}

Perfect solution. _ (: З"∠) _! Rotate the numeric matrix 90 degrees clockwise without taking up extra memory space (that is, without using the cache matrix)

Solution

//Clockwise 90 degrees: first flip up and down, and then flip the main diagonal 1 2 3 7 8 9 7 4, 1, 4->, 5 6 4    --->    5 6 2< C14/>7 8 9             1 2 3 9 6 3

/counterclockwise 90 degrees: first flip by main diagonal, then flip up and down
 1    2 3, 1 4, 7 3 6 9 4 5->
  2 5 8    --->    2 5 8
 7             8            9 3 6 9 1 4 7

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.