linux c/c++ 面试题目整理(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/e21105834/article/details/70237265

1、求下面函数的返回值

int func(x)
{
    int countx = 0;
    while(x)
    {
        countx++;
        x = x&(x-1);
    }
    return countx;
}

问:假定x是9999,那么返回多少?
答:返回的是8,解题思路是将x转化为二进制,看含有多少个1,则就返回多少。

2、文件中有一组整数,要求排序后输出到另一个文件中

       如果数不多的情况下,可以直接将文件中所有整数读到set中,set类型会自动排好序,读完后再将所有整数写到另一个文件中去。
       而整数特别多内存又不够的情况下,可以分批读,每次读一部分排好序写到一个小文件中去,直到所有整数都读取完成,这时每个小文件就都是排好序的了,然后将所有小文件中第一个数读出来进行比较,选出最小的放到目标文件中去,然后将该最小的数所在文件的下一个数读取出来进行比较,这样所有小文件都比完之后,目标文件中就是排好序的所有整数了。
       思路:这里采用的是大而化小,化整为零的手段。

3、使用递归将两个有序链表合并为一个有序链表

       思路:理解递归,并定义好结束的条件

Node * Merge(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 = Merge(head1->next, head2);
    }
        else
    {
        head = head2;
        head->next = Merge(head1, head2->next);
    }
    return head;
}

4、写一个函数找出整数数组中第二大的数

const int minnumber = -32767;
int find_sec_max(int data[], int count)
{
    int maxnumber = data[0];
    int sec_max = minnumber;
    for( int i=1;I < count; i++)
    {
        if (data[i] > maxnumber)
        {
            sec_max = maxnumber;
            maxnumber = data[i];
        }
        else
        {
            if (data[i] > sec_max)
                sec_max = data[i];
        }
    }
    return sec_max;
}

思路:定义两个变量一直保存最大的两个整数

5、如何判断一个单链表是否有环

struct node
{
    char val;
    node* next;
}
bool check(const node* head)
{
    if (head == NULL)
        return false;
    node* low = head,*fast = head->next;
    while(fast != NULL && fast->next != NULL)
    {
        low = low->next;
        fast = fast->next->next;
        if (low == fast)
        {
            return true;
        }
    }
    return false;
}

思路:第一个节点每次走一步,第二个节点每次走两步,即第一个节点走n步的时候,第二个节点走了2n步,如果是有环的链表,n和2n处必定是同一个节点。

6、一个标准的strcpy函数

//strcpy的实现:
char * strcpy(char* strDest, const char* strSrc)
{
    assert( (strDest != NULL) && (strSrc != NULL));
    char *address = strDest;
    while( (*strDest++ = *strSrc++) != ‘\0’);
    return address;
}

返回char*的原因:
返回strDest的原始值使得函数能够支持链式表达式,增加了函数的附加值。同样功能的函数,如果能合理地提高函数的可用性,自然就更加的理想。

7、嵌入式系统可能用到这样的场景,即要求设置一绝对地址为0x67a9的整型变量的值为0xaa66,怎么设置?

int *ptr;
ptr = (int*)0x67a9;
*ptr = 0xaa66;

8、下面的代码输出是什么?为什么?

void foo(void)
{
    unsigned int a = 6;
    int b = -20;
    (a+b) > 6?puts(“>6”):puts(“<=6”);
}

结果是”>6”,原因是当表达式存在有符号类型和无符号类型时所有的操作对象都自动转换为无符号类型,因此-20变成一个非常大的整数与6相加,所以该表达式计算出的结果大于6.

9、strncpy和snprintf的正确用法

//strncpy的正确用法:
strncpy(dest,src,sizeof(dest)-1);
dest[strlen(dest)] = ‘\0’;
//snprintf的正确用法:
snprintf(dest, sizeof(dest)-1, “%s”, src);

总则:
1)snprintf的使用比strncpy简洁
2)snprintf可以获取被拷贝的字节数
3)二者都有性能问题,如果src长度远大于dest,用strncpy,否则用snprintf。

10、printf中%5.3s,这样的怎么看?

int main(void)
{
    printf(“%s, %5.3s\n”, “computer”, “computer”);
    return 0;
}

输出:computer,com
       %5.3s 或者是 %5.3d 中小数点后面表示输出的最大宽度,小数点前面的表示输出的最小宽度;
       %5.3f则表示输出场宽为9的浮点数, 其中小数位为3, 整数位为1, 小数点占一位, 不够9位右对齐;
       默认都是右对齐, 若是%-7s则输出左对齐

猜你喜欢

转载自blog.csdn.net/e21105834/article/details/70237265