您当前的位置: 首页 >  面试

phymat.nico

暂无认证

  • 0浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C语言面试题大汇总

phymat.nico 发布时间:2017-12-19 23:23:31 ,浏览量:0

Meditation Drop 水滴冥想 点击放大

 static有什么用途?(请至少说明两种) 1.限制变量的作用域 2.设置变量的存储域 7. 引用与指针有什么区别? 1) 引用必须被初始化,指针不必。 2) 引用初始化以后不能被改变,指针可以改变所指的对象。 2) 不存在指向空值的引用,但是存在指向空值的指针。 8. 描述实时系统的基本特性 在特定时间内完成特定的任务,实时性与可靠性 9. 全局变量和局部变量在内存中是否有区别?如果有,是什么区别? 全局变量储存在静态数据库,局部变量在堆栈 10. 什么是平衡二叉树? 左右子树都是平衡二叉树 且左右子树的深度差值的绝对值不大于1 11. 堆栈溢出一般是由什么原因导致的? 没有回收垃圾资源 12. 什么函数不能声明为虚函数? constructor 13. 冒泡排序算法的时间复杂度是什么? O(n^2) 14. 写出float x 与“零值”比较的if语句。 if(x>0.000001&&x>)%256 } 请问hash(16),hash(256)的值分别是: A.1.16;B.8.32;C.4.16;D.1.32 四.找错题: 1.请问下面程序有什么错误? int a[60][250][1000],i,j,k; for(k=0;knext) { p->next->next=p->next->next->next; p=p->next->next; } coutnext; if(Pointer->number==key) { Back->next=Pointer->next; free(Pointer); break; } void delete(Node* p) { if(Head = Node) while(p) } 有一个16位的整数,每4位为一个数,写函数求他们的和。 解释: 整数1101010110110111 和 1101+0101+1011+0111 感觉应该不难,当时对题理解的不是很清楚,所以写了一个函数,也不知道对不对。 疑问: 既然是16位的整数,1101010110110111是2进制的,那么函数参数怎么定义呢,请大虾指教。 答案:用十进制做参数,计算时按二进制考虑。 /* n就是16位的数,函数返回它的四个部分之和 */ char SumOfQuaters(unsigned short n) { char c = 0; int i = 4; do { c += n & 15; n = n >> 4; } while (--i); return c; } 有1,2,....一直到n的无序数组,求排序算法,并且要求时间复杂度为O(n),空间复杂度O(1),使用交换,而且一次只能交换两个数.(华为) #include int main() { int a[] = {10,6,9,5,2,8,4,7,1,3}; int len = sizeof(a) / sizeof(int); int temp; for(int i = 0; i < len; ) { temp = a[a[i] - 1]; a[a[i] - 1] = a[i]; a[i] = temp; if ( a[i] == i + 1) i++; } for (int j = 0; j < len; j++) coutnext; while(q!=NULL) { r=q->next; q->next=p; p=q; q=r; } head->next=NULL; head=p; return head; } 2 写出程序删除链表中的所有接点 void del_all(node *head) { node *p; while(head!=NULL) { p=head->next; free(head); head=p; } cout= 4 ) { 298 *d++ = *s++; 299 *d++ = *s++; 300 *d++ = *s++; 301 *d++ = *s++; 302 len -= 4; 303 } 304 while ( len-- ) { 305 *d++ = *s++; 306 } 307 } 308 return dst; 309 } 公司考试这种题目主要考你编写的代码是否考虑到各种情况,是否安全(不会溢出) 各种情况包括: 1、参数是指针,检查指针是否有效 2、检查复制的源目标和目的地是否为同一个,若为同一个,则直接跳出 3、读写权限检查 4、安全检查,是否会溢出 memcpy拷贝一块内存,内存的大小你告诉它 strcpy是字符串拷贝,遇到'/0'结束 /* memcpy ─── 拷贝不重叠的内存块 */ void memcpy(void* pvTo, void* pvFrom, size_t size) { void* pbTo = (byte*)pvTo; void* pbFrom = (byte*)pvFrom; ASSERT(pvTo != NULL && pvFrom != NULL); //检查输入指针的有效性 ASSERT(pbTo>=pbFrom+size || pbFrom>=pbTo+size);//检查两个指针指向的内存是否重叠 while(size-->0) *pbTo++ == *pbFrom++; return(pvTo); } 华为面试题:怎么判断链表中是否有环? bool CircleInList(Link* pHead) { if(pHead = = NULL || pHead->next = = NULL)//无节点或只有一个节点并且无自环 return (false); if(pHead->next = = pHead)//自环 return (true); Link *pTemp1 = pHead;//step 1 Link *pTemp = pHead->next;//step 2 while(pTemp != pTemp1 && pTemp != NULL && pTemp->next != NULL) { pTemp1 = pTemp1->next; pTemp = pTemp->next->next; } if(pTemp = = pTemp1) return (true); return (false); } 两个字符串,s,t;把t字符串插入到s字符串中,s字符串有足够的空间存放t字符串 void insert(char *s, char *t, int i) { memcpy(&s[strlen(t)+i],&s[i],strlen(s)-i); memcpy(&s[i],t,strlen(t)); s[strlen(s)+strlen(t)]='/0'; } 1。编写一个 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; } return cpDest; } 2。请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值。 int search(char *cpSource, int n, char ch) { int i; for(i=0; ireturn i; } 一个单向链表,不知道头节点,一个指针指向其中的一个节点,问如何删除这个指针指向的节点? 将这个指针指向的next节点值copy到本节点,将next指向next->next,并随后删除原next指向的节点。 #include void foo(int m, int n) { printf("m=%d, n=%d/n", m, n); } int main() { int b = 3; foo(b+=3, ++b); printf("b=%d/n", b); return 0; } 输出:m=7,n=4,b=7(VC6.0) 这种方式和编译器中得函数调用关系相关即先后入栈顺序。不过不同 编译器得处理不同。也是因为C标准中对这种方式说明为未定义,所以 各个编译器厂商都有自己得理解,所以最后产生得结果完全不同。 因为这样,所以遇见这种函数,我们首先要考虑我们得编译器会如何处理 这样得函数,其次看函数得调用方式,不同得调用方式,可能产生不同得 结果。最后是看编译器优化。 2.写一函数,实现删除字符串str1中含有的字符串str2. 第二个就是利用一个KMP匹配算法找到str2然后删除(用链表实现的话,便捷于数组) /*雅虎笔试题(字符串操作) 给定字符串A和B,输出A和B中的最大公共子串。 比如A="aocdfe" B="pmcdfa" 则输出"cdf" */ //Author: azhen #include #include #include char *commanstring(char shortstring[], char longstring[]) { int i, j; char *substring=malloc(256); if(strstr(longstring, shortstring)!=NULL) //如果……,那么返回shortstring return shortstring; for(i=strlen(shortstring)-1;i>0; i--) //否则,开始循环计算 { for(j=0; jstrlen(str2)) //将短的字符串放前面 comman=commanstring(str2, str1); else comman=commanstring(str1, str2); printf("the longest comman string is: %s/n", comman); } 11.写一个函数比较两个字符串str1和str2的大小,若相等返回0,若str1大于 str2返回1,若str1小于str2返回-1 int strcmp ( const char * src,const char * dst) { int ret = 0 ; while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst) { ++src; ++dst; } if ( ret < 0 ) ret = -1 ; else if ( ret > 0 ) ret = 1 ; return( ret ); } 3,求1000!的未尾有几个0(用素数相乘的方法来做,如72=2*2*2*3*3); 求出1->1000里,能被5整除的数的个数n1,能被25整除的数的个数n2,能被125整除的数的个数n3, 能被625整除的数的个数n4. 1000!末尾的零的个数=n1+n2+n3+n4; #include #define NUM 1000 int find5(int num){ int ret=0; while(num%5==0){ num/=5; ret++; } return ret; } int main(){ int result=0; int i; for(i=5;idata == Value) { if (pNode->front == NULL) { pHeader = pNode->next; pHeader->front = NULL; } else { if (pNode->next != NULL) { pNode->next->front = pNode->front; } pNode->front->next = pNode->next; } Node *pNextNode = pNode->next; delete pNode; pNode = pNextNode; bRet = TRUE; //不要break或return, 删除所有 } else { pNode = pNode->next; } } return bRet; } void DE(Node *pHeadA, Node *pHeadB) { if (pHeadA == NULL || pHeadB == NULL) { return; } Node *pNode = pHeadA; while (pNode != NULL) { if (DeteleNode(pHeadB, pNode->data)) { if (pNode->front == NULL) { pHeadA = pNode->next; pHeadA->front = NULL; } else { pNode->front->next = pNode->next; if (pNode->next != NULL) { pNode->next->front = pNode->front; } } Node *pNextNode = pNode->next; delete pNode; pNode = pNextNode; } else { pNode = pNode->next; } } } 2. 编程实现:找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad" int GetCommon(char *s1, char *s2, char **r1, char **r2) { int len1 = strlen(s1); int len2 = strlen(s2); int maxlen = 0; for(int i = 0; i < len1; i++) { for(int 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; } } } } 3. 编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列库函数 char* test3(long num) { char* buffer = (char*)malloc(11); buffer[0] = '0'; buffer[1] = 'x'; buffer[10] = '/0'; char* temp = buffer + 2; for (int i=0; i < 8; i++) { temp[i] = (char)(num28); temp[i] = temp[i] >= 0 ? temp[i] : temp[i] + 16; temp[i] = temp[i] < 10 ? temp[i] + 48 : temp[i] + 55; } return buffer; } 输入N, 打印 N*N 矩阵 比如 N = 3,打印: 1 2 3 8 9 4 7 6 5 N = 4,打印: 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 解答: 1 #define N 15 int s[N][N]; void main() { int k = 0, i = 0, j = 0; int a = 1; for( ; k < (N+1)/2; k++ ) { while( j < N-k ) s[i][j++] = a++; i++; j--; while( i < N-k ) s[i++][j] = a++; i--; j--; while( j > k-1 ) s[i][j--] = a++; i--; j++; while( i > k ) s[i--][j] = a++; i++; j++; } for( i = 0; i < N; i++ ) { for( j = 0; j < N; j++ ) cout next = head; for (i = 1; tail != head; ++i) { for (j = 1; j < m; ++j) { tail = head; head = head->next; } tail->next = head->next; printf("第%4d个出局的人是:%4d号/n", i, head->index); free(head); head = tail->next; } i = head->index; free(head); return i; } int main() { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!/n", Josephu(n, m)); system("pause"); return 0; } 已知strcpy函数的原型是: char * strcpy(char * strDest,const char * strSrc); 1.不调用库函数,实现strcpy函数。 2.解释为什么要返回char *。 解说: 1.strcpy的实现代码 char * strcpy(char * strDest,const char * strSrc) { if ((strDest==NULL)||(strSrc==NULL)) file://[/1] throw "Invalid argument(s)"; //[2] char * strDestCopy=strDest; file://[/3] while ((*strDest++=*strSrc++)!='/0'); file://[/4] return strDestCopy; } 错误的做法: [1] (A)不检查指针的有效性,说明答题者不注重代码的健壮性。 (B) 检查指针的有效性时使用((!strDest)||(!strSrc))或(!(strDest&&strSrc)),说明答题者对C语言中类型的隐式转换没有深刻认识。在本例中char *转换为bool即是类型隐式转换,这种功能虽然灵活,但更多的是导致出错概率增大和维护成本升高。所以C++专门增加了bool、true、false 三个关键字以提供更安全的条件表达式。 (C)检查指针的有效性时使用((strDest==0)||(strSrc==0)),说明答题者不知道使用常量的好处。直接使用字面常量(如本例中的0)会减少程序的可维护性。0虽然简单,但程序中可能出现很多处对指针的检查,万一出现笔误,编译器不能发现,生成的程序内含逻辑错误,很难排除。而使用NULL代替0,如果出现拼写错误,编译器就会检查出来。 [2] (A)return new string("Invalid argument(s)");,说明答题者根本不知道返回值的用途,并且他对内存泄漏也没有警惕心。从函数中返回函数体内分配的内存是十分危险的做法,他把释放内存的义务抛给不知情的调用者,绝大多数情况下,调用者不会释放内存,这导致内存泄漏。 (B)return 0;,说明答题者没有掌握异常机制。调用者有可能忘记检查返回值,调用者还可能无法检查返回值(见后面的链式表达式)。妄想让返回值肩负返回正确值和异常值的双重功能,其结果往往是两种功能都失效。应该以抛出异常来代替返回值,这样可以减轻调用者的负担、使错误不会被忽略、增强程序的可维护性。 [3] (A)忘记保存原始的strDest值,说明答题者逻辑思维不严密。 [4] (A)循环写成while (*strDest++=*strSrc++);,同[1](B)。 (B)循环写成while (*strSrc!='/0') *strDest++=*strSrc++;,说明答题者对边界条件的检查不力。循环体结束后,strDest字符串的末尾没有正确地加上'/0'。

from 凡得人间

Meditation Drop 水滴冥想 点击放大

 static有什么用途?(请至少说明两种) 1.限制变量的作用域 2.设置变量的存储域 7. 引用与指针有什么区别? 1) 引用必须被初始化,指针不必。 2) 引用初始化以后不能被改变,指针可以改变所指的对象。 2) 不存在指向空值的引用,但是存在指向空值的指针。 8. 描述实时系统的基本特性 在特定时间内完成特定的任务,实时性与可靠性 9. 全局变量和局部变量在内存中是否有区别?如果有,是什么区别? 全局变量储存在静态数据库,局部变量在堆栈 10. 什么是平衡二叉树? 左右子树都是平衡二叉树 且左右子树的深度差值的绝对值不大于1 11. 堆栈溢出一般是由什么原因导致的? 没有回收垃圾资源 12. 什么函数不能声明为虚函数? constructor 13. 冒泡排序算法的时间复杂度是什么? O(n^2) 14. 写出float x 与“零值”比较的if语句。 if(x>0.000001&&x>)%256 } 请问hash(16),hash(256)的值分别是: A.1.16;B.8.32;C.4.16;D.1.32 四.找错题: 1.请问下面程序有什么错误? int a[60][250][1000],i,j,k; for(k=0;knext) { p->next->next=p->next->next->next; p=p->next->next; } coutnext; if(Pointer->number==key) { Back->next=Pointer->next; free(Pointer); break; } void delete(Node* p) { if(Head = Node) while(p) } 有一个16位的整数,每4位为一个数,写函数求他们的和。 解释: 整数1101010110110111 和 1101+0101+1011+0111 感觉应该不难,当时对题理解的不是很清楚,所以写了一个函数,也不知道对不对。 疑问: 既然是16位的整数,1101010110110111是2进制的,那么函数参数怎么定义呢,请大虾指教。 答案:用十进制做参数,计算时按二进制考虑。 /* n就是16位的数,函数返回它的四个部分之和 */ char SumOfQuaters(unsigned short n) { char c = 0; int i = 4; do { c += n & 15; n = n >> 4; } while (--i); return c; } 有1,2,....一直到n的无序数组,求排序算法,并且要求时间复杂度为O(n),空间复杂度O(1),使用交换,而且一次只能交换两个数.(华为) #include int main() { int a[] = {10,6,9,5,2,8,4,7,1,3}; int len = sizeof(a) / sizeof(int); int temp; for(int i = 0; i < len; ) { temp = a[a[i] - 1]; a[a[i] - 1] = a[i]; a[i] = temp; if ( a[i] == i + 1) i++; } for (int j = 0; j < len; j++) coutnext; while(q!=NULL) { r=q->next; q->next=p; p=q; q=r; } head->next=NULL; head=p; return head; } 2 写出程序删除链表中的所有接点 void del_all(node *head) { node *p; while(head!=NULL) { p=head->next; free(head); head=p; } cout= 4 ) { 298 *d++ = *s++; 299 *d++ = *s++; 300 *d++ = *s++; 301 *d++ = *s++; 302 len -= 4; 303 } 304 while ( len-- ) { 305 *d++ = *s++; 306 } 307 } 308 return dst; 309 } 公司考试这种题目主要考你编写的代码是否考虑到各种情况,是否安全(不会溢出) 各种情况包括: 1、参数是指针,检查指针是否有效 2、检查复制的源目标和目的地是否为同一个,若为同一个,则直接跳出 3、读写权限检查 4、安全检查,是否会溢出 memcpy拷贝一块内存,内存的大小你告诉它 strcpy是字符串拷贝,遇到'/0'结束 /* memcpy ─── 拷贝不重叠的内存块 */ void memcpy(void* pvTo, void* pvFrom, size_t size) { void* pbTo = (byte*)pvTo; void* pbFrom = (byte*)pvFrom; ASSERT(pvTo != NULL && pvFrom != NULL); //检查输入指针的有效性 ASSERT(pbTo>=pbFrom+size || pbFrom>=pbTo+size);//检查两个指针指向的内存是否重叠 while(size-->0) *pbTo++ == *pbFrom++; return(pvTo); } 华为面试题:怎么判断链表中是否有环? bool CircleInList(Link* pHead) { if(pHead = = NULL || pHead->next = = NULL)//无节点或只有一个节点并且无自环 return (false); if(pHead->next = = pHead)//自环 return (true); Link *pTemp1 = pHead;//step 1 Link *pTemp = pHead->next;//step 2 while(pTemp != pTemp1 && pTemp != NULL && pTemp->next != NULL) { pTemp1 = pTemp1->next; pTemp = pTemp->next->next; } if(pTemp = = pTemp1) return (true); return (false); } 两个字符串,s,t;把t字符串插入到s字符串中,s字符串有足够的空间存放t字符串 void insert(char *s, char *t, int i) { memcpy(&s[strlen(t)+i],&s[i],strlen(s)-i); memcpy(&s[i],t,strlen(t)); s[strlen(s)+strlen(t)]='/0'; } 1。编写一个 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; } return cpDest; } 2。请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值。 int search(char *cpSource, int n, char ch) { int i; for(i=0; ireturn i; } 一个单向链表,不知道头节点,一个指针指向其中的一个节点,问如何删除这个指针指向的节点? 将这个指针指向的next节点值copy到本节点,将next指向next->next,并随后删除原next指向的节点。 #include void foo(int m, int n) { printf("m=%d, n=%d/n", m, n); } int main() { int b = 3; foo(b+=3, ++b); printf("b=%d/n", b); return 0; } 输出:m=7,n=4,b=7(VC6.0) 这种方式和编译器中得函数调用关系相关即先后入栈顺序。不过不同 编译器得处理不同。也是因为C标准中对这种方式说明为未定义,所以 各个编译器厂商都有自己得理解,所以最后产生得结果完全不同。 因为这样,所以遇见这种函数,我们首先要考虑我们得编译器会如何处理 这样得函数,其次看函数得调用方式,不同得调用方式,可能产生不同得 结果。最后是看编译器优化。 2.写一函数,实现删除字符串str1中含有的字符串str2. 第二个就是利用一个KMP匹配算法找到str2然后删除(用链表实现的话,便捷于数组) /*雅虎笔试题(字符串操作) 给定字符串A和B,输出A和B中的最大公共子串。 比如A="aocdfe" B="pmcdfa" 则输出"cdf" */ //Author: azhen #include #include #include char *commanstring(char shortstring[], char longstring[]) { int i, j; char *substring=malloc(256); if(strstr(longstring, shortstring)!=NULL) //如果……,那么返回shortstring return shortstring; for(i=strlen(shortstring)-1;i>0; i--) //否则,开始循环计算 { for(j=0; jstrlen(str2)) //将短的字符串放前面 comman=commanstring(str2, str1); else comman=commanstring(str1, str2); printf("the longest comman string is: %s/n", comman); } 11.写一个函数比较两个字符串str1和str2的大小,若相等返回0,若str1大于 str2返回1,若str1小于str2返回-1 int strcmp ( const char * src,const char * dst) { int ret = 0 ; while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst) { ++src; ++dst; } if ( ret < 0 ) ret = -1 ; else if ( ret > 0 ) ret = 1 ; return( ret ); } 3,求1000!的未尾有几个0(用素数相乘的方法来做,如72=2*2*2*3*3); 求出1->1000里,能被5整除的数的个数n1,能被25整除的数的个数n2,能被125整除的数的个数n3, 能被625整除的数的个数n4. 1000!末尾的零的个数=n1+n2+n3+n4; #include #define NUM 1000 int find5(int num){ int ret=0; while(num%5==0){ num/=5; ret++; } return ret; } int main(){ int result=0; int i; for(i=5;idata == Value) { if (pNode->front == NULL) { pHeader = pNode->next; pHeader->front = NULL; } else { if (pNode->next != NULL) { pNode->next->front = pNode->front; } pNode->front->next = pNode->next; } Node *pNextNode = pNode->next; delete pNode; pNode = pNextNode; bRet = TRUE; //不要break或return, 删除所有 } else { pNode = pNode->next; } } return bRet; } void DE(Node *pHeadA, Node *pHeadB) { if (pHeadA == NULL || pHeadB == NULL) { return; } Node *pNode = pHeadA; while (pNode != NULL) { if (DeteleNode(pHeadB, pNode->data)) { if (pNode->front == NULL) { pHeadA = pNode->next; pHeadA->front = NULL; } else { pNode->front->next = pNode->next; if (pNode->next != NULL) { pNode->next->front = pNode->front; } } Node *pNextNode = pNode->next; delete pNode; pNode = pNextNode; } else { pNode = pNode->next; } } } 2. 编程实现:找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad" int GetCommon(char *s1, char *s2, char **r1, char **r2) { int len1 = strlen(s1); int len2 = strlen(s2); int maxlen = 0; for(int i = 0; i < len1; i++) { for(int 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; } } } } 3. 编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列库函数 char* test3(long num) { char* buffer = (char*)malloc(11); buffer[0] = '0'; buffer[1] = 'x'; buffer[10] = '/0'; char* temp = buffer + 2; for (int i=0; i < 8; i++) { temp[i] = (char)(num28); temp[i] = temp[i] >= 0 ? temp[i] : temp[i] + 16; temp[i] = temp[i] < 10 ? temp[i] + 48 : temp[i] + 55; } return buffer; } 输入N, 打印 N*N 矩阵 比如 N = 3,打印: 1 2 3 8 9 4 7 6 5 N = 4,打印: 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 解答: 1 #define N 15 int s[N][N]; void main() { int k = 0, i = 0, j = 0; int a = 1; for( ; k < (N+1)/2; k++ ) { while( j < N-k ) s[i][j++] = a++; i++; j--; while( i < N-k ) s[i++][j] = a++; i--; j--; while( j > k-1 ) s[i][j--] = a++; i--; j++; while( i > k ) s[i--][j] = a++; i++; j++; } for( i = 0; i < N; i++ ) { for( j = 0; j < N; j++ ) cout next = head; for (i = 1; tail != head; ++i) { for (j = 1; j < m; ++j) { tail = head; head = head->next; } tail->next = head->next; printf("第%4d个出局的人是:%4d号/n", i, head->index); free(head); head = tail->next; } i = head->index; free(head); return i; } int main() { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!/n", Josephu(n, m)); system("pause"); return 0; } 已知strcpy函数的原型是: char * strcpy(char * strDest,const char * strSrc); 1.不调用库函数,实现strcpy函数。 2.解释为什么要返回char *。 解说: 1.strcpy的实现代码 char * strcpy(char * strDest,const char * strSrc) { if ((strDest==NULL)||(strSrc==NULL)) file://[/1] throw "Invalid argument(s)"; //[2] char * strDestCopy=strDest; file://[/3] while ((*strDest++=*strSrc++)!='/0'); file://[/4] return strDestCopy; } 错误的做法: [1] (A)不检查指针的有效性,说明答题者不注重代码的健壮性。 (B) 检查指针的有效性时使用((!strDest)||(!strSrc))或(!(strDest&&strSrc)),说明答题者对C语言中类型的隐式转换没有深刻认识。在本例中char *转换为bool即是类型隐式转换,这种功能虽然灵活,但更多的是导致出错概率增大和维护成本升高。所以C++专门增加了bool、true、false 三个关键字以提供更安全的条件表达式。 (C)检查指针的有效性时使用((strDest==0)||(strSrc==0)),说明答题者不知道使用常量的好处。直接使用字面常量(如本例中的0)会减少程序的可维护性。0虽然简单,但程序中可能出现很多处对指针的检查,万一出现笔误,编译器不能发现,生成的程序内含逻辑错误,很难排除。而使用NULL代替0,如果出现拼写错误,编译器就会检查出来。 [2] (A)return new string("Invalid argument(s)");,说明答题者根本不知道返回值的用途,并且他对内存泄漏也没有警惕心。从函数中返回函数体内分配的内存是十分危险的做法,他把释放内存的义务抛给不知情的调用者,绝大多数情况下,调用者不会释放内存,这导致内存泄漏。 (B)return 0;,说明答题者没有掌握异常机制。调用者有可能忘记检查返回值,调用者还可能无法检查返回值(见后面的链式表达式)。妄想让返回值肩负返回正确值和异常值的双重功能,其结果往往是两种功能都失效。应该以抛出异常来代替返回值,这样可以减轻调用者的负担、使错误不会被忽略、增强程序的可维护性。 [3] (A)忘记保存原始的strDest值,说明答题者逻辑思维不严密。 [4] (A)循环写成while (*strDest++=*strSrc++);,同[1](B)。 (B)循环写成while (*strSrc!='/0') *strDest++=*strSrc++;,说明答题者对边界条件的检查不力。循环体结束后,strDest字符串的末尾没有正确地加上'/0'。

from 凡得人间

关注
打赏
1659628745
查看更多评论
立即登录/注册

微信扫码登录

2.4369s