c++ 实用面试题+自己写的答案

基础题

1.解释面向对象的语言三大特性:封装、继承、多态。并举例使用场景和方法。

2.简单介绍下堆 栈 静态区,存储哪些对象?内存管理都由谁管理。在多线程中,主线程是否可以访问子线程堆中的对象?子线程是否可以访问主线程堆中的对象?两个子线程是否可以相互访问对方堆中的对象?为什么?

3. 以下输出为?

int x =1, x2 = 3;
int& rx = x;
rx = 2;
cout << x << endl;  
cout << rx << endl; 
cout << x2 << endl; 
rx = x2;
cout << x << endl;  
cout << rx << endl; 
cout << x2 << endl; 

提升题 - 算法

4. 字符串反转

5. 链表反转

6. 有序数组合并

7. Hash算法,查找字符串中第一个只出现一次的字符

8. 求无序数组当中的中位数

9. 快速排序

10. 冒泡排序

11. 选择排序

12. 二分法查找(不用递归)

13. 二分法查找(使用递归)

加分题

14."1234567891011121314151617181920212223242526"是一个无限长的数字串0-9,由按升序写入的正整数组成,各个数字之间没有任何分隔符。该函数应返回计数序列中位置n的整数,本题提示:应该观察到此无限序列的结构非常简单(该序列以9个个位数开头,然后是90个两位数开头,然后是900个三位数,依此类推),因此可以跳过此系列的前缀,以指数级的形式跨越,直到到达位置n并找出永远在那儿的数字。

n 期望答案
0 1
100 5
10000 7
10**100 6

答案

1.

1. 封装:就是把变量和方法封装到一个类中。
2. 继承:就是如果一个子类继承一个父类,那么子类就可以直接用父类的变量和方法,大大减少了代码的书写量并且提高了代码的可维护性(只要在父类中修改其中的变量和方法,子类继承过来的变量和方法也会随着改变,必须要一一修改)
3. 多态:从字面上理解就是多种形态。从多态的定义来说:
    用一个父类的指针指向子类的对象,在函数(方法)调用的时候可以调用到正确版本的函数(方法)。 
使用多态的条件:
    3.1.子类必须重写父类的方法
    3.2.父类指针指向子类对象
多态的应用场景:
    用一个父类的指针指向子类的对象

2.

可编程内存在基本上分为这样的几大部分:静态存储区、堆区和栈区。他们的功能不同,对他们使用方式也就不同。

静态存储区(全局存储区):内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。它主要存放静态数据、全局数据和常量。

栈区:内存管理由系统控制,存储的为非静态的局部变量,例如:函数参数,在函数中生命的对象的指针等。当系统的栈区大小不够分配时, 系统会提示栈溢出。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。

堆区:亦称动态内存分配。内存管理由程序控制,存储的为malloc , new ,alloc出来的对象。 如果程序没有控制释放,那么在程序结束时,由系统释放。但在程序运行过程中,会出现内存泄露、内存溢出问题。 分配方式 类似于链表。

3.

2
2
3
3
3
3

4.字符串反转


@interface CharReverse : NSObject
 void char_reverse(char* cha);
@end

@implementation CharReverse
void char_reverse(char* cha){
    char * begin = cha;
    char * end = cha + strlen(cha) -1;
    while (begin < end) {
        char temp = *begin;
        *(begin++) = *end;
        *(end--) = temp;
    }
}


 char cha[] = "hello,world";
    char_reverse(cha);
    NSLog(@"%s",cha);

5. 链表反转

struct Node {
    int data;
    struct Node* next;
};
@interface ReverseList : NSObject

struct Node * reverseList( struct Node* head);
struct Node* constructList(void);
void printList(struct Node* head);

//创建链表
struct Node* constructList(void){
    struct Node* head = NULL;
    struct Node* cur = NULL;
    
    for (int i =1; i<10; i++) {
        struct Node* node = malloc(sizeof(struct Node));
        node->next = NULL;
        node->data = i;
        if (head == NULL) {
            head = node;
        }else{
            cur->next = node;
        }
        cur = node;
        
    }
    
    return head;
}

//反转链表
struct Node * reverseList( struct Node* head){
    struct Node* p = head;
    struct Node* newH = NULL;
    while (p != NULL) {
        //记录下一个节点
        struct Node* temp = p->next;
        //当前节点的next指向新链表头部
        p->next = newH;
        //更改新链表头部为当前结点
        newH = p;
        //移动p指针
        p = temp;
    }
    return newH;
}
//打印链表
void printList(struct Node* head){
    struct Node* node = head;
    while (node != NULL) {
        printf("%d \n",node->data);
        node = node->next;
    }
}

6. 有序数组的合并

void mergeSortList(int a[],int aLen, int b[],int bLen, int result[]){
    int p = 0;
    int q = 0;
    int i = 0;
    while (p < aLen && q < bLen) {
        if (a[p] <= b[q]) {
            result[i] = a[p++];
        }else{
            result[i] = b[q++];
        }
        i++;
    }
    
    while (p < aLen) {
        result[i++] = a[p++];
    }
    
    while (q < bLen) {
        result[i++] = b[q++];
    }
}


-(void) mergeSortList{
    int a[5] = {1,3,6,7,9};
    int b[8] = {2,4,6,7,10,11,12,13};
    int result[13];
    mergeSortList(a, 5, b, 8, result);
    for (int i=0; i<13; i++) {
        printf("%d ",result[i]);
    }
}

7. 哈希查找.查找字符串中第一个只出现一次的字符

char findFirstOnceChar(char * cha){
    char result = '\0';
    
    int arr[256];
    for (int i =0; i<256; i++) {
        arr[i] = 0;
    }
    
    char *p = cha;
    while (*p != '\0') {
        arr[*(p++)]++;
    }
    p = cha;
    while (*p != '\0') {
        if (arr[*p] == 1) {
            result = *p;
            break;
        }
        p++;
    }
    
    return result;
}

-(void) findFirstOneTimeChar{
    char cha[] = "ababscskjaglkjagkajklaf";
    char result = findFirstOnceChar(cha);
    printf("the result is %c \n",result);
}

8. 查找无序数组中位数

int findMedian(int a[],int aLen){

    int low = 0;
    int high = aLen -1;
    int mid = (aLen -1)/2;
    int div = partSort(a,low,high);
    
    while (div != mid) {
        if (mid < div) {
            div = partSort(a,low,div-1);
        }else{
            div = partSort(a,div+1,high);
        }
    }
    return a[mid];
}

int partSort(int a[],int start,int end){
    int low = start;
    int high = end;
    int key = a[end];
    while (low < high) {
        
        while (low < high && a[low] <= key) {
            low++;
        }
        
        while (low < high && a[high] >= key) {
            high--;
        }
        
        if(low < high) {
            int temp = a[low];
            a[low] = a[high];
            a[high] = temp;
        }
    }

    int temp = a[high];
    a[high] = a[end];
    a[end] = temp;
    return low;
}

-(void) findMedian{
    int a[9] = {12,3,10,8,6,7,11,13,9};
    // int a[5] = {3,4,6,2,5};
    int res = findMedian(a, 9);
    printf("result is %d",res);
}

9. 快速排序 c语言

void quickSort(int a[],int left,int right){
    if (left >= right){
        return;
    }
    int i = left;
    int j = right;
    
    int key = a[left];
    while (i < j) {
        while (i < j && a[j] >= key) {
            j--;
        }
        a[i] = a[j];
        
        while (i < j && a[i] <= key) {
            i++;
        }
        a[j] = a[i];
    }
    a[i] = key;
//    for (int i=0; i< 9; i++) {
//        printf("%d ",a[i]);
//    }
//    printf("\n-------------\n");

    quickSort(a,left,i-1);
    quickSort(a,i+1,right);
}

10.冒泡排序

for (NSInteger i = 0; i < arr.count -1; i++) {
        for (NSInteger j = 0; j < arr.count -1 -i; j++) {
            if ([arr[j] integerValue] < [arr[j+1] integerValue]) {
                NSInteger tmp = [arr[j] integerValue];
                arr[j] = arr[j+1];
                arr[j+1] = @(tmp);
            }
        }
    }

11.选择排序

for (NSInteger i = 0; i < arr.count -1; i++) {
        for (NSInteger j = i +1; j < arr.count; j++) {
            if ([arr[j] integerValue] < [arr[i] integerValue]) {
                NSNumber * temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

12.二分法查找(不用递归)

-(NSInteger)binarySearchWithArr:(NSMutableArray *)arr low:(NSInteger)low High:(NSInteger)high Key:(NSNumber *)key{
    
    if (low > high) {
        return -1;
    }
    while (low <= high) {
        NSInteger mid = (low + high)/2;

        if (arr[mid] == key) {
            return mid;
        }else if (arr[mid] > key){
            high = mid -1;
        }else{
            low = mid +1;
        }
    }
    
    return -1;
}

13. 二分法查找(使用递归)

-(NSInteger)binarySearchRecursionWithArr:(NSMutableArray *)arr low:(NSInteger)low high:(NSInteger)high key:(NSNumber *)key{
    if (low > high) {
        return -1;
    }
    
    NSInteger mid = (low + high)/2;
    if (arr[mid] == key) {
        return mid;
    }else if(arr[mid] > key){
        return [self binarySearchRecursionWithArr:arr low:low high:mid -1 key:key];
    }else{
        return [self binarySearchRecursionWithArr:arr low:mid + 1 high:high key:key];
    }
    
    return -1;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Army_Ma

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值