zoukankan      html  css  js  c++  java
  • C/C++

    下列程序输出:67

    #include<stdio.h>
    
    int cnt = 0;
    
    int fib(int n)
    {
        cnt++;
        if(n == 0)
            return 1;
        else if(n == 1)
            return 2;
        else
            return fib(n-1) + fib(n-2);    
    }    
    
    void main()
    {
        fib(8);
        printf("%d", cnt);
    }

    解析:

    解法1:

    f(n)=0, cnt自加一次,f(n)=1, cnt自加一次,即:

    n=0    cnt = 1;

    n=1    cnt = 1;

    n=2    cnt = f(1) + f(0) = 1+1+1 = 3;

    n=3    cnt = f(2) + f(1) = 3+1+1 = 5;

    n=4    cnt = f(3) + f(2) = 5+3+1 = 9;

    n=5    cnt = f(4) + f(3) = 9+4+1 = 15;

    n=6    cnt = f(5) + f(4) = 15+9+1 = 25;

    n=7    cnt = f(6) + f(5) = 15+25+1 = 41;

    n=8    cnt = f(7) + f(6) = 41+25+1 = 67;

    所以最后输出:67

    解法2:

    观察题目fib(n) = fib(n-1) + fib(n-2),类似于斐波那契数列,所以也可直接写出答案:

    数列为:1 , 1 , (1+1+1) , (3+1+1) , (5+3+1) , (9+5+1) , (15+9+1) , (25+15+1) , (41+25+1) = 67;

    std::vector::at和std::vector::[]的区别在于前者会进行边界检查(推荐使用),后者不进行边界检查。

    Windows中进程间通信方式:File、管道(pipe)、命名管道(name pipe)、信号(signal)、消息队列(message queue)、共享内存(sharred memory)、内存映射(memory mapped file)、信号量(semphore)、套接字(socket)、命名事件。临界区事实上是由信号量保证的。

    virtual函数是动态绑定的,而缺省参数值却是静态绑定。(在调用一个定义于派生类内的virtual函数的同时,却使用基类为它所指定的缺省参数值)

    C/C++中函数的参数是从右到左入栈的。

    %d格式输出的是4字节大小。

    BSS(block started by symbol):通常是指用来存放程序中未初始化的全局变量和静态变量的一块内存区域。特点是可读写,在程序执行之前BSS会自动清零、所以未初始化的全局变量在程序执行之前已经为0了。

    数据段(data segment):通常是指用来存放程序中已经初始化的全局变量的一块内存区域。数据段属于静态内存分配。

    代码段(code segment):通常是指用来存放程序执行代码的一块内存区域。这部分区域的大小在程序运行之前就已经确定,并且内存区域通常属于只读,某些架构也允许代码段为读写。在代码段中,也可能包含一些只读的常数变量,如字符串常量。

    类成员默认为private。

    软件特性:一致性、复杂性、不可见性、可变性。

    类中的非静态函数默认为this指针,表明为该类的对象所有。静态函数不属于任何类的对象,没有this指针,由类直接调用。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        char ss[10] = "1, 2, 3, 4, 5";
        gets(ss);
        strcat(ss, "6789");
        printf("%s
    ", ss);
    
        exit(0);
    }

    输出为:ABC678
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
     int main(void)
     {   
         int n;
         char y[10] = "ntse";
         char *x = y;
         n = strlen(x);
         *x = x[n];
         x++;
         printf("x=%s
    ", x);
         printf("y=%s
    ", y);
         exit(0);
      }
    
    输出:x=tse
         y=

     在构造函数、析构函数中调用类自己的函数,虚函数的动态绑定机制均不会生效。

    静态函数不可以是虚函数。

    虚函数可以声明为inline。

    转义字符有两种形式:三位八进制(ddd)、二位十六进制(xhh)。

  • 相关阅读:
    OpenLDAP与Apache
    OpenLDAP双主
    OpenLDAP主从
    LDAP与禅道
    LDAP与jenkins
    LDAP与Samba
    LDAP与SSH
    LDAP客户端
    LDAP与migrationtools 导入系统账号
    OpenLDAP与phpldapadmin的搭建
  • 原文地址:https://www.cnblogs.com/MrRS/p/9071453.html
Copyright © 2011-2022 走看看