zoukankan      html  css  js  c++  java
  • 程序员面试宝典题目重温-P1-100

    int f(int x ,int y)
    {
        return (x&y) + ((x^y)>>1)
    } 

    f(729,271)输出是什么?

    x&y表示按位与,结果是x,y相同位的一半(除以2),

    x^y表示亦或,表示取x,y的不同位, 同时右移表示除以2,

    相同位和不同位都取了,而且都取的是一半,因此结果是(x+y)/2,结果为500 

    如何将a、b的值进行交换而不用担心超界问题

    方法1:a = a+b; b = a-b ; a = a-b; 缺点:a和b较大的时候,容易超界

    方法2:a = a^b; b = a^b; a= a^b 

    编写一个函数,实现把c/c++程序代码中的注释去掉,并返回结果


    几个swap函数,判断能否交换两个数 

    void swap1(int p,int q)
    {
        int temp;
        temp = p;
        p = q;
        q = temp;
    }

    void swap2(int *p,int *q)
    {
        int *temp;
        *temp = *p;
        *p = *q;
        *q = *temp;
    }

    void swap3(int *p,int *q)
    {
        int *temp;
        temp = p;
        p = q;
        q = temp;
    }

    void swap4(int *p,int *q)
    {
        int temp;
        temp = *p;
        *p = *q;
        *q = temp;
    }

    void swap5(int &p,int &q)
    {
        int temp;
        temp = p;
        p = q;
        q = temp;

    } 

     答案:no,no,no,yes,yes

    写出函数指针、函数返回指针、常量指针、指针常量、指向const的const指针

    从上到下依次是:


    void (*f)()

    void *f()

    const int *

    int *const

    const int *const 

    指向数组的指针和指针数组

    依次是:int (*ptr)[]

       int *ptr[] == int *(ptr[]) 

    前者表示一个指向整型数组的指针,后者是一个指针数组,ptr[]里存放的是地址,它指向位置的值是*ptr[0],*ptr[1]等,不要直接*ptr[0]=5,因为5指向的不是一个固定的地址空间。

    指针数组是指:一个数组里装着指针,对指针取值就能得到对应地址的值。

    数组指针是一个指针,代表它是一个指针,指向整个数组。 


    写出如下程序片段输出

    int a[] = {1,2,3,4,5};

    int *ptr = (int *)(&a + 1); 

    cout << *(a+1) << *(ptr-1);

     输出 2和5,(int *)(&a + 1)表示的是a数组的第六个数(尽管不存在)


  • 相关阅读:
    BZOJ1969: [Ahoi2005]LANE 航线规划
    BZOJ1800: [Ahoi2009]fly 飞行棋
    BZOJ2661: [BeiJing wc2012]连连看
    BZOJ2464: 中山市选[2009]小明的游戏
    BZOJ2440: [中山市选2011]完全平方数
    BZOJ2226: [Spoj 5971] LCMSum
    Codeforces 610B
    Codeforces 757B
    Codeforces B
    HDU 6090 Rikka with Graph
  • 原文地址:https://www.cnblogs.com/xiawen/p/3277759.html
Copyright © 2011-2022 走看看