p2 = head2->next ;
p1 = head1 ; }
Node *pcurrent = head ;
while ( p1 != NULL && p2 != NULL) {
if ( p1->data <= p2->data ) {
pcurrent->next = p1 ; pcurrent = p1 ; p1 = p1->next ; } else {
pcurrent->next = p2 ; pcurrent = p2 ; p2 = p2->next ; } }
if ( p1 != NULL )
pcurrent->next = p1 ; if ( p2 != NULL )
pcurrent->next = p2 ; return head ; }
132.已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序,这次要求用递归方法进行。 ( Autodesk) 答案:
Node * MergeRecursive(Node *head1 , Node *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 ; }
133.分析一下这段程序的输出: class B{
public:
B(){ cout<<\
~B(){ cout<<\
B(int i):data(i){ cout<<\
private: int data; };
B Play( B b){ return b ; }
int main(int argc, char* argv[]){ }
B temp = Play(5); return 0;
结果:constructed by parameter 5 destructed destructed
136. 求下面函数的返回值(微软) int func(x){
int countx = 0; while(x){ countx++; x = x&(x-1); }
return countx; }
假定x = 9999 答案:8
思路:将x转化为2进制,看含有的1的个数
138. 编写一个C函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的:
char * search(char *cpSource, char ch){ char *cpTemp=NULL, *cpDest=NULL; int iTemp, iCount=0; while(*cpSource){
if(*cpSource == ch){ iTemp = 0;
cpTemp = cpSource;
while(*cpSource == ch)
++iTemp, ++cpSource; if(iTemp > iCount) iCount = iTemp, cpDest = cpTemp; if(!*cpSource) }
break;
++cpSource; }
cpDest[iCount] = ‘\\0’; return cpDest;
} int main() {
//char *str = \ char str[256] = \
char *ptr = search(str, 'f'); printf(\return 0;
}
注意问题:1. 字符串遇‘\\0’才能结束
2. 指针只有四个字节的大小
3. char *str = \ cpDest[iCount] = ‘\\0’; 不能修改字符串常量的值
139. 请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值。
int search(char *cpSource, int n, char ch) {
int i;
for(i=0; i 140.一个单向链表,不知道头节点,一个指针指向其中的一个节点,问如何删除这个指针指向的节点? 将这个指针指向的next节点值copy到本节点,将next指向next->next,并随后删除原next指向的节点。 141、用指针的方法,将字符串“ABCD1234efgh”前后对调显示 #i nclude char str[] = \ int length = strlen(str); char * p1 = str; char * p2 = str + length - 1; while(p1 < p2) { char c = *p1; *p1 = *p2; *p2 = c; ++p1; --p2; } printf(\ system(\ return 0; } 142、有一分数序列:1/2,1/4,1/6,1/8??,用函数调用的方法,求此数列前20项的和 #i nclude double result = 0; int i = 2; while(i < 42) { result += 1.0 / i;//一定要使用1.0做除数,不能用1,否则结果将自动转化成整数,即0.000000 i += 2; } return result; } int main() { printf(\ system(\ return 0; } 143、有一个数组a[1000]存放0--1000;要求每隔二个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。 以7个数为例: {0,1,2,3,4,5,6,7} 0-->1-->2(删除)-->3-->4-->5(删除)-->6-->7-->0(删除),如此循环直到最后一个数被删除。 方法1:数组 #i nclude using namespace std; #define null 1000 int main() { int arr[1000]; for (int i=0;i<1000;++i) arr[i]=i; int j=0; int count=0; while(count<999) { while(arr[j00]==null) j=(++j)00; j=(++j)00; while(arr[j00]==null) j=(++j)00; j=(++j)00; while(arr[j00]==null) j=(++j)00; arr[j]=null; ++count; } while(arr[j]==null) j=(++j)00; cout< 方法2:链表 #i nclude int data; node* next; }; int main() { node* head=new node; head->data=0; head->next=null; node* p=head; for(int i=1;i<1000;i++) {