C + + written interview (algorithm problem set III)

Source: Internet
Author: User

1> writing strcpy function, known function prototype char*strcpy (char* strdest,char* strsrc)

ANSWER:

chat* strcpy (char* strdest,char* strsrc)

{

If (Strsrc==null) return NULL;

Char*ch1=strsrc,*ch2=strdest;

while (*ch1!= ')

{

*ch2++=*ch1++;

}

*ch2= ' + ';

Return strdest;

}

2> use recursive method to determine the integer group A[n] is not ascending sort

ANSWER:

Boolean isascending (int a[])

{

Returnisascending (a,0);

}

Bool isascending (int a[],int start)

{

returnstart==length-1 | | Isascending (a,start+1);

}

3> Delete a number in a string and compress a string

CHAR * Partition (const char *STR)

{

char* I=str;

char* J=str;

while (*i!= ')

{

If (*i> ' 9 ' | | | *i< ' 0 ')

*j++=*i++;

Else

*i++;

}

*j= ' + ';

Return str;

}

The 4> function moves the character ' * ' of the string to the front of the string, and the preceding non-' * ' character moves to the back part of the string.

ANSWER:

Int Partitionstr (char a[])

{

Int count=0;

Inti=a.length-1,j=a. Length-1;

while (i>=0)

{

If (a[i]!= ' * ')

{

Swap (a,i--, j--)

}

Else

{

i--;

count++;

}

}

Return count;

}

5> is known as a string that looks for the number of occurrences of the string sub in the original string.

ANSWER:

Int count_of_substr (chat *str,char *sub)

{

Int N=strlen (sub);

Int count=0;

Char *p=stt;

while (p!= ')

{

If (strcmp (p,sub,n))

count++;

p++;

}

Return count;

}

6> A minimal heap, also a fully binary tree, is represented by a layer traversal array.

1, how to access the child nodes of node A[n]

2, insert a node of program void add_element (int *a,int size,int val);

3, remove the minimum node of the program

Anwser:

Void add_element (int *a,int size,int val)

{

A[size]=val;

Int p=size/2-1;

Int c=size;

while (p>=0)

{

If (A[p]<a[c])

break;

Else

{

A[C]=A[P];

C=p;

p= (p-1)/2;

}

}

A[c]=val;

}

Void Del (int *a,int size)

{

A[0]=A[SIZE-1];

Int Val=a[0];

size--;

Int p=0;

Int c=2*p+1;

while (c<=size-1)

{

If (c<size-1 &&a[c]>a[c+1])

C + +;

If (A[p]<=a[c])

break;

Else

{

A[P]=A[C];

P=c; c=2*p+1;

}

}

A[p]=val;

}

7> a combination of strings

Anwser:

Void combination_m (chat *pstr,intm,vector<char> &result)

{

If (pstr==null| | (*pstr== ' &&m!=0))

Return;

If (m==0)

cout<<result<<endl;

Result.push_back (*PSTR);

Combination_m (Pstr+1,m-1,result);

Result.pop_back ();

Combination_m (Pstr+1,m,result);

}

Void combination (char *pstr)

{

If (pstr==null| | *pstr== ' + ')

Return;

Intlen=strlen (str);

for (inti=1;i<=len;i++)

{

vector<char> result;

Combination_m (PSTR,I,RESULR);

}

}

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.