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);
}
}