笔试题练习(七)

xiaoxiao2026-08-27  4

1,链表的常见操作

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> struct Node{ int value; struct Node * next;}root; // 已知链表的头结点head,写一个函数把这个链表逆序 Node * ReverseList(Node * head){ // 链表逆序 assert(head != NULL); if (head -> next == NULL){ // 只有头节点 return head;}Node * pPre = head -> next;Node * pCur = pPre -> next,pTmp; if (pCur == NULL){ // 只有一个节点 return head;} while (pCur != NULL){pTmp = pCur -> next; // 记录下一个 head -> next = pCur;pCur -> next = pPre;pPre = pCur;pCur = pTmp;} return head;}Node * Merge(Node * head1,Node * head2){ // 已知两个链表head1和head2各自有序升序排列,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同) Node * head = NULL;Node * p1 = head1 -> next;Node * p2 = head2 -> next;Node * pCur = head; while (p1 != NULL && p2 != NULL){ if (p1 -> value < p2 -> value){pCur -> next = p1;pCur = p1;p1 = p1 -> next;} else {pCur -> next = p2;pCur = p2;p2 = p2 -> next;}} if (p1 != NULL){ // 第一个有剩余 pCur -> next = p1;} if (p2 != NULL){pCur -> next = p2;} return head;}Node * MergeRecursive(Node * head1,Node * head2){ // 已知两个链表head1和head2各自升序排列,请把它们合并成一个链表依然有序,这次要求用递归方法进行。 if (head1 == NULL) return head2; if (head2 == NULL) return head1;Node * head = NULL; if (head1 -> data < head2 -> data){head = head1;head -> next = MergeRecursive(head1 -> next,head2);} else {head = head2;head -> next = MergeRecursive(head1,head2 -> next);} return head;}

2,动态分配二维数组

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> int ** alloArrays(unsigned int nrows,unsigned int ncolumns){unsigned int i; int ** array = ( int ** )malloc(nrows * sizeof ( int * )); for (i = 0 ;i < nrows;i ++ )array[i] = ( int * )malloc(ncolumns * sizeof ( int )); return array;} int ** allocArrays2( int rows, int columns){ int ** array = new int * [rows]; int i,j; for (i = 0 ;i < rows; ++ i){array[i] = new int [columns];} for (i = 0 ;i < rows; ++ i){ for (j = 0 ;j < columns; ++ j){array[i][j] = i * j;}} return array;}

3.字符串简单操作

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> /* ********************************************************************** */ /* Author:phinecosDate:2009-06-2 */ /* ********************************************************************** */ #include < iostream > using namespace std; int strcmp_p( const char * s1, const char * s2){ int ret; while ((ret = * (unsigned char * )s1 ++ - * (unsigned char * )s2 ++ ) == 0 ); return ret;} int memcmp_p( const char * s1, const char * s2,size_tn){ int ret = 0 ; while (n -- && (ret = * (unsigned char * )s1 ++ - * (unsigned char * )s2 ++ ) == 0 ); return ret;} void * memcpy_p( void * dest, const void * src,size_tcount){ char * pDest = static_cast < char *> (dest); const char * pSrc = static_cast < const char *> (src); if ((pDest > pSrc) && (pDest < (pSrc + count))){ // 源地址和目标地址内存重叠 for (size_ti = count - 1 ;i != - 1 ; -- i)pDest[i] = pSrc[i];} else { for (size_ti = 0 ;i < count; ++ i)pDest[i] = pSrc[i];} return dest;} int main(){ char str[] = " 0123456789 " ; char str2[] = " 0 " ;cout << memcmp_p(str,str2, 3 ) << endl;memcpy_p(str + 1 ,str + 0 , 9 );cout << str << endl;memcpy_p(str,str + 5 , 5 );cout << str << endl; return 0 ;}

4,统计一个字符串中所有字符出现的次数

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #include < iostream > #include < map > using namespace std; static map < char , int > countMap; static int countArray[ 128 ]; void doCount( const char * str){ while ( * str){countMap[ * str ++ ] ++ ;}} void doCount2( const char * str){ while ( * str){countArray[ * str ++ ] ++ ;}} int main(){ char str[] = " fasdfdsfdferwefaasdf " ; // 使用map doCount(str);map < char , int > ::iteratoriter; for (iter = countMap.begin();iter != countMap.end(); ++ iter){cout << iter -> first << " : " << iter -> second << endl;} // 不用map,直接数组 doCount2(str); for ( int i = 0 ;i < 128 ; ++ i){ if (countArray[i] > 0 ){printf( " %c/t%d/n " ,i,countArray[i]);}} return 0 ;}

5. 在字符串中找出连续最长的数字串的长度

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> int FindMaxIntStr( char * outputstr, char * intputstr){ char * in = intputstr, * out = outputstr, * temp, * final; int count = 0 ,maxlen = 0 ; while ( * in != ' /0 ' ){ if ( * in >= ' 0 ' && * in <= ' 9 ' ){ for (temp = in ; * in >= ' 0 ' && * in <= ' 9 ' ; in ++ )count ++ ; if (maxlen < count){maxlen = count;final = temp; // temp保存了当前连续最长字串的首地址,final是总的最长 * (final + count) = ' /0 ' ; // 给字符串赋上结束符 }count = 0 ; // 不管当前累计的最长数字是否大于前面最长的,都应该清除count,以便下次计数 } // 到此处时*in肯定不是数字 in ++ ;} // 上述比较过程只保存了最长字串在输入数组中对应的地址,避免了反复拷贝到输出数组的过程 for ( int i = 0 ;i < maxlen;i ++ ) // 将最终的最长字串保存到输出数组 { * out ++ = * final ++ ;} * out = ' /0 ' ; return maxlen;}

6,最大公共子串

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> int maxCommonStr( char * s1, char * s2, char ** r1, char ** r2){ // 求最大公共子串 int len1 = strlen(s1); int len2 = strlen(s2); int maxlen = 0 ; int i,j; for (i = 0 ;i < len1;i ++ ){ for (j = 0 ;j < len2;j ++ ){ if (s1[i] == s2[j]) // 找到了第一个相等的 { int as = i,bs = j,count = 1 ; // 保存第一个相等的首地址 while ( as + 1 < len1 && bs + 1 < len2 && s1[ ++ as ] == s2[ ++ bs]) // 查找最大相等长度 count ++ ; if (count > maxlen) // 如果大于最大长度则更新 {maxlen = count; * r1 = s1 + i; * r2 = s2 + j;}}}}}

相关资源:全国计算机等级考试笔试题练习
转载请注明原文地址: https://www.6miu.com/read-5051959.html

最新回复(0)