C++面试题整理
小小哇牛
jiayou
展开
-
175-178
175void GetMemory(char *p){p = (char *)malloc(100);}void Test(void){char *str = NULL;GetMemory(str); strcpy(str, "helloworld");printf(str);}请问运行Test函数会有什么样的结果?答:程序崩溃。因为Get原创 2016-06-30 14:27:38 · 379 阅读 · 0 评论 -
174、请简述以下两个for循环的优缺点(5分)
174、请简述以下两个for循环的优缺点(5分) for (i=0; i<N; i++){ if (condition) DoSomething(); else DoOtherthing();}if (condition){ for (i=0; i<N; i++) DoSomething();}else{ for (i=0; i原创 2016-06-30 14:22:50 · 2519 阅读 · 0 评论 -
143、约瑟夫问题
143、有一个数组a[1000]存放0--1000;要求每隔二个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。以7个数为例: {0,1,2,3,4,5,6,7} 0-->1-->2(删除)-->3-->4-->5(删除)-->6-->7-->0(删除),如此循环直到最后一个数被删除。方法1数组:#define null 1000int main(原创 2016-06-30 10:34:41 · 454 阅读 · 0 评论 -
125.已知strcpy函数的原型是,不调用库函数,实现strcpy函数
125.已知strcpy函数的原型是:char * strcpy(char * strDest,const char *strSrc);不调用库函数,实现strcpy函数。其中,strSrc是原字符串,strDest是目标字符串。#include "stdlib.h"#include "stdio.h"#include "string.h"char * strcpy(char *原创 2016-06-29 11:47:53 · 1708 阅读 · 0 评论 -
126.自己编写String类
126.已知类String 的原型为:class String{public: String(const char *str = NULL); // 普通构造函数 String(const String &other); // 拷贝构造函数 ~ String(void); // 析构函数 String & operate =(const String &other);原创 2016-06-29 13:31:29 · 476 阅读 · 0 评论 -
130.逆序链表
130.一个链表的结点结构 struct Node{int data ;Node *next ;};typedef struct Node Node ;已知链表的头结点head,写一个函数把这个链表逆序 ( Intel)答案:#include "stdio.h"#include "stdlib.h"#include "string.h"#include原创 2016-06-29 14:05:08 · 605 阅读 · 0 评论 -
131. 一个链表的结点结构,合并非递归和递归
131. 一个链表的结点结构struct Node{int data ;Node *next ;};typedef struct Node Node ;已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。答案:我自己写的如下#include "stdio.h"#include "stdlib.h"#include "string.原创 2016-06-29 14:50:53 · 424 阅读 · 0 评论 -
133.分析一下这段程序的输出
133.分析一下这段程序的输出 (Autodesk)class B{public: B() { cout<<"default constructor"<<endl; } ~B() { cout<<"destructed"<<endl; } B(int i):data(i) { cout<<"constructed by parameter" << data原创 2016-06-29 15:38:43 · 1440 阅读 · 0 评论 -
136.求下面函数的返回值(微软)
13、求下面函数的返回值(微软)int main(int argc, char* argv[]){ //B temp = Play(5); Play(5); return 0;}int func(x){ int countx = 0; while(x) { countx ++; x = x&(x-1); } return countx;} 假定x原创 2016-06-29 16:01:45 · 645 阅读 · 0 评论 -
137、写出下列代码的输出内容,函数指针问题
137、写出下列代码的输出内容#includeint inc(int a){ return(++a);}int multi(int*a,int*b,int*c){ return(*c=*a**b);}typedef int(FUNC1)(int in);typedef int(FUNC2) (int*,int*,int*);void show(FUNC2 fun,原创 2016-06-29 16:13:31 · 743 阅读 · 0 评论 -
栈的学习和应用
首先栈的实现可以借助于线性表,因为栈就是特殊的线性表,只支持一端进出,先进先出,先给出线性表实现代码#ifndef _MYLINKLIST_H_#define _MYLINKLIST_H_typedef void LinkList;/*typedef struct _tag_LinkListNode LinkListNode;struct _tag_LinkListNode{原创 2016-07-01 10:01:48 · 354 阅读 · 0 评论