Three simple algorithm questions (C ++)

Source: Internet
Author: User

I haven't done any algorithm questions for a long time. I will repeat several simple algorithm questions.
Question 1: Calculate the largest sum of sub-Arrays
This is a very common algorithm question. Many people can quickly write algorithms, but many cannot write completely correctly. The problem lies mainly in sum initialization,
Many incorrect answers initialize them to 0. If all elements in the array are negative, the maximum value obtained is 0, and sum is initialized to the first element of the array.

Question 2: 1 + 2 +... + N. You cannot use keywords such as multiplication and division, for, while, if, else, switch, and case, as well as condition judgment statements.
This question is also available in many versions on the Internet, including addition in constructor, using two static variables for one storage result and one for the current value, create an array of n elements in one dimension, and the static variables in the stored results are the expected values,
Another method is to use two methods. One method is recursive, And the other value returns the constant value 0, which is to change the determination in Recursion to a method with a return value always 0.
I want to talk about the third-party method: using the template and keyword inline, the compiled result is: 1 + 2 +... + n, does not generate a bunch of method calls, because the method is defined as inline.

Question 3: enter a binary tree, print each node of the tree from top to bottom, and print it from left to right on the same layer.
This topic mainly uses the queue idea, first-in-first-out, because we can easily Insert elements in the binary tree into the queue in layer order,
First, insert the root node into the queue. When each node goes out of the queue, its subnodes are added to the queue. Print the nodes in the queue.


// Obtain the largest sum of sub-Arrays
Int maxSum (int * arr, int len)
{
Int sum, max;
Sum = max = arr [0];
For (int I = 1; I <len; I ++)
{
If (sum <= 0)
{
Sum = arr [I];
} Else {
Sum + = arr [I];
}
If (sum> max)
{
Max = sum;
}
}
Return max;
}

// Calculate 1 + 2 +... + N. You cannot use keywords such as multiplication and division, for, while, if, else, switch, and case, as well as condition judgment statements.
Template <int n> inline int Sum (int m)
{
Return Sum <n-1> (m-1) + m;
}
 
Template <> inline int Sum <1> (int m)
{
Return 1;
}

Template <> inline int Sum <0> (int m)
{
Return 0;
}

// Question 3: enter a binary tree, print each node of the tree from top to bottom, and print it from left to right on the same layer.
Class PrintByFloor
{
Public:
Struct Node
{
Int value;
Node * left;
Node * right;
Node (int val): value (val), left (NULL), right (NULL ){}
};

PrintByFloor (): root (new Node (-1 )){}


~ PrintByFloor (){
MakeEmpty (root );
}

Void Print ()
{
If (root = NULL)
{
Return;
}
Queue <Node *> queue;
If (root-> left! = NULL ){
Queue. push (root-> left );
} Else
{
Queue. push (root-> right );
}
While (queue. size ())
{
Node * cur = queue. front ();
Cout <cur-> value <"\ t ";
If (cur-> left! = NULL)
{
Queue. push (cur-> left );
}
If (cur-> right! = NULL)
{
Queue. push (cur-> right );
}
Queue. pop ();
}
}
 
Node * Add (int value, Node * t)
{
If (t = NULL)
{
T = new Node (value );
} Else if (value <t-> value)
{
If (t-> left = NULL)
{
T-> left = new Node (value );
} Else {
Return Add (value, t-> left );
}
} Else if (value> t-> value)
{
If (t-> right = NULL)
{
T-> right = new Node (value );
} Else {
Return Add (value, t-> right );
}
}

Return t;
}

Node * Add (int value)
{
Return Add (value, root );
}

Private:
Void MakeEmpty (Node * t)
{
If (t! = NULL)
{
MakeEmpty (t-> left );
MakeEmpty (t-> right );
Delete t;
T = NULL;
}
}
 
Node * root;

};
The test code is as follows:


// Test code
Int main (){
Int arr [] = {1,-3, 5,-6,-2,-7 };
Int maxValue = maxSum (arr, sizeof (arr)/sizeof (arr [0]);
Cout <maxValue <endl;

{
PrintByFloor floor;
Floor. Add (8 );
Floor. Add (6 );
Floor. Add (5 );
Floor. Add (7 );
Floor. Add (11 );
Floor. Add (9 );
Floor. Add (12 );
Floor. Add (10 );
Floor. Add (3 );
Floor. Print ();
}
Cout <endl;
Int sum = Sum <100> (100 );
Cout <sum <endl;
Getchar ();
Return 0;
}
Result:

 




Author Chen taihan

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.