Several algorithm questions

Source: Internet
Author: User

Any string contains numbers and general characters.

For example, ad2ef35adx1wewe76
Note that there are four numbers in this string: 1, 2, 35, 76, regardless of the large number.

Sorts numbers in ascending order to form a new string.

A function is required for processing.

Assume it is fun (char * src, char * des)
{
}

Src input string ad2ef35adx1wewe76

Des output string 1-2-35-76


View plaincopy to clipboardprint? Void Convert (const char * str)
{
Int res = 0;
Vector <int> vec;
While (* str! = '\ 0 ')
{
If (isdigit (* str ))
{
While (isdigit (* str ))
{
Res = res * 10 + (* str-'0 ');
Str ++;
}
 
Vec. push_back (res );
}
Else
{
Res = 0;
Str ++;
}
}
 
Sort (vec. begin (), vec. end ());
 
Copy (vec. begin (), vec. end (), ostream_iterator <int> (cout ,"-"));
Cout <endl;
}
Void Convert (const char * str)
{
Int res = 0;
Vector <int> vec;
While (* str! = '\ 0 ')
{
If (isdigit (* str ))
{
While (isdigit (* str ))
{
Res = res * 10 + (* str-'0 ');
Str ++;
}

Vec. push_back (res );
}
Else
{
Res = 0;
Str ++;
}
}

Sort (vec. begin (), vec. end ());

Copy (vec. begin (), vec. end (), ostream_iterator <int> (cout ,"-"));
Cout <endl;
}

 

 

In an integer array, elements in the array are first increased and then subtracted according to the size. design an algorithm to obtain the index of the given element in the array.


View plaincopy to clipboardprint? # Include "stdafx. h"
# Include <iostream>
# Include <vector>
# Include <iterator>
# Include <algorithm>
Using namespace std;
 
// Use the binary search idea to obtain the index of the largest element
Int FindMax (int arr [], int n)
{
Int low = 0;
Int high = n-1;
Int mid = 0;
While (low + 2 <= high)
{
Mid = (low + high)/2;
 
If (arr [mid]> arr [mid-1] & (arr [mid]> arr [mid + 1]) // the max element
Return mid;
Else if (arr [mid]> arr [mid-1] & (arr [mid + 1]> arr [mid]) // Increment
Low = mid;
Else
High = mid;
}
}
 
// Binary search of the incremental part
Int BinSearchLeft (int arr [], int begin, int end, int target)
{
Int low = begin;
Int high = end-1;
Int mid = 0;
 
While (low <= high)
{
Mid = (low + high)/2;
 
If (arr [mid] = target)
Return mid;
Else if (arr [mid] <target)
Low = mid + 1;
Else
High = mid-1;
}
Return-1;
}
// Binary search of the descending part
Int binsearshortght (int arr [], int begin, int end, int target)
{
Int low = begin;
Int high = end-1;
Int mid = 0;
 
While (low <= high)
{
Mid = (low + high)/2;
 
If (arr [mid] = target)
Return mid;
Else if (arr [mid]> target)
Low = mid + 1;
Else
High = mid-1;
}
Return-1;
}
// Algorithm idea: first find the index of the largest element, and then perform a binary search for the Left and Right Parts
Int FindTargetIndex (int arr [], int n, int target)
{
Int maxIndex = FindMax (arr, n );

If (arr [maxIndex] = target)
Return maxIndex;
 
Int res = 0;
If (res = BinSearchLeft (arr, 0, maxIndex, target ))! =-1)
Return res;
 
If (res = binsearshortght (arr, maxIndex + 1, n, target ))! =-1)
Return res;
 
Return-1;
}
Int main ()
{
Int arr [] = {2, 3, 4, 5, 10, 7, 6, 1 };
Int size = sizeof (arr)/sizeof (int );
 
Int maxIndex = FindMax (arr, size );
If (-1! = MaxIndex)
Cout <"the max element's index is" <maxIndex <", the max elemet is" <arr [maxIndex] <endl;
 
Int res = FindTargetIndex (arr, size, 3 );
If (-1! = Res)
Cout <"the target's position is" <res <endl;
Else
Cout <"can not find this target! "<Endl;
 
Return 0;
}
# Include "stdafx. h"
# Include <iostream>
# Include <vector>
# Include <iterator>
# Include <algorithm>
Using namespace std;

// Use the binary search idea to obtain the index of the largest element
Int FindMax (int arr [], int n)
{
Int low = 0;
Int high = n-1;
Int mid = 0;
While (low + 2 <= high)
{
Mid = (low + high)/2;

If (arr [mid]> arr [mid-1] & (arr [mid]> arr [mid + 1]) // the max element
Return mid;
Else if (arr [mid]> arr [mid-1] & (arr [mid + 1]> arr [mid]) // Increment
Low = mid;
Else
High = mid;
}
}

// Binary search of the incremental part
Int BinSearchLeft (int arr [], int begin, int end, int target)
{
Int low = begin;
Int high = end-1;
Int mid = 0;

While (low <= high)
{
Mid = (low + high)/2;

If (arr [mid] = target)
Return mid;
Else if (arr [mid] <target)
Low = mid + 1;
Else
High = mid-1;
}
Return-1;
}
// Binary search of the descending part
Int binsearshortght (int arr [], int begin, int end, int target)
{
Int low = begin;
Int high = end-1;
Int mid = 0;

While (low <= high)
{
Mid = (low + high)/2;

If (arr [mid] = target)
Return mid;
Else if (arr [mid]> target)
Low = mid + 1;
Else
High = mid-1;
}
Return-1;
}
// Algorithm idea: first find the index of the largest element, and then perform a binary search for the Left and Right Parts
Int FindTargetIndex (int arr [], int n, int target)
{
Int maxIndex = FindMax (arr, n );
 
If (arr [maxIndex] = target)
Return maxIndex;

Int res = 0;
If (res = BinSearchLeft (arr, 0, maxIndex, target ))! =-1)
Return res;

If (res = binsearshortght (arr, maxIndex + 1, n, target ))! =-1)
Return res;

Return-1;
}
Int main ()
{
Int arr [] = {2, 3, 4, 5, 10, 7, 6, 1 };
Int size = sizeof (arr)/sizeof (int );

Int maxIndex = FindMax (arr, size );
If (-1! = MaxIndex)
Cout <"the max element's index is" <maxIndex <", the max elemet is" <arr [maxIndex] <endl;

Int res = FindTargetIndex (arr, size, 3 );
If (-1! = Res)
Cout <"the target's position is" <res <endl;
Else
Cout <"can not find this target! "<Endl;

Return 0;
}


Author: "wangyangkobe's column"

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.