c c++内存操作

C
void * malloc ( size_t size );

Allocate memory block
Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.

The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.

void * calloc ( size_t num, size_t size );

Allocate space for array in memory
Allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits to zero.

The effective result is the allocation of an zero-initialized memory block of (num * size) bytes.

c++

new

new操作不会初始化内存

总之 需要需要对内存初始化为0  安全第一




c/c++内存操作函数 是否初始化










strcpy  后面跟了0
strncpy 后面没跟0




















1、  有个好动的小孩,他还很小,不能象你一样一步爬很多楼梯,他最多一步爬两楼梯。他家的楼梯一共有N梯(2<=N<=100000),今天一直在那爬上爬下,有时一步一梯,有时候一步两梯。原来,他在尝试爬楼梯有多少有种爬法。噢,这个方法太笨太慢了,你能写个程序帮帮他吗?另外,如果你能让你的程序更快的计算出来那我们会对你打更高的分!

递归函数 f(n)= f(n-2)+ f(n-1)

int g_count = 0;

#define BIG_INT_SIZE    1024 * 2

typedef struct big_int_t
{
    unsigned char data[BIG_INT_SIZE];
}big_int;


void big_int_add(big_int *first, big_int *second, int first_n, int second_n);
void big_int_addadd(big_int *first, int first_n);
void big_int_equal(big_int *first, int first_n, int num);

void alg_recursion(int steps);


void main()
{

    int nStep = 0;
    printf("please enter step num :\n");
    scanf("%d", &nStep);
    if (nStep >= 2 && nStep <= 100000)
    {
//         calcStep(nStep);
        alg_recursion(nStep);
    }
    else
    {
        printf("step is error!.\n");
        printf("step must be >= 2     <= 100000!.\n");

        printf("step = %d\n", nStep);
    }
    printf("\n打印出来的是16进制数字\n");
    scanf("%d", &nStep);
}

void big_int_add(big_int *first, big_int *second, int first_n, int second_n)
{
    int i = second_n - 1;
    int j = first_n -1;

    int tmp = 0;
    int tmp2 = 0;

    big_int big_int_tmp = {0};

    for (i = second_n - 1; i >= 0; i --)
    {
        tmp = (int)first->data[i] + (int)second->data[i] + tmp2;
        big_int_tmp.data[i] = tmp;
        tmp2 = tmp >> 8;
    }
    memcpy(first, &big_int_tmp, BIG_INT_SIZE);
}

void big_int_addadd(big_int *first, int first_n)
{
    if (first->data[first_n - 1] == 0xff)
    {
        big_int_addadd(first, first_n - 1);
    }
    else
    {
        first->data[first_n - 1] ++;
    }
}

void big_int_equal(big_int *first, int first_n, int num)
{
    memset(first, 0, first_n);
    first->data[first_n - 1] = num;
    first->data[first_n - 2] = num >> 8;
    first->data[first_n - 3] = num >> 16;
    first->data[first_n - 4] = num >> 24;
}

big_int g_p1;
big_int g_p2;

big_int * calc_alg_p(int steps)
{
    //裴不拉切序列算法
    //1 2 3 5 8 13 21 34
    //1 2 3 4 5 6  7  8
    int i = 2;
    big_int_equal(&g_p1, BIG_INT_SIZE, 1);
    big_int_equal(&g_p2, BIG_INT_SIZE, 2);
    while (steps != i)
    {
        if (i % 2 == 0)
        {
            big_int_add(&g_p1, &g_p2, BIG_INT_SIZE, BIG_INT_SIZE);
        }
        else
        {
            big_int_add(&g_p2, &g_p1, BIG_INT_SIZE, BIG_INT_SIZE);
        }
        i ++;
    }
    if (steps % 2 == 0)
    {
        return &g_p2;
    }
    else
        return &g_p1;
}

void alg_recursion(int steps)
{
    //将数分解成
    //steps - 1 与 steps - 2
    //
    int i = 0;
    unsigned char byte_tmp = 0;
    big_int * p_tmp = calc_alg_p(steps);//calc_recursion(steps);

    //打印大数
    for (i = 0; i < BIG_INT_SIZE; i ++)
    {
        byte_tmp = p_tmp->data[i] >> 4;
        printf(" %x", byte_tmp);
        byte_tmp = p_tmp->data[i] & 0x0F;
        printf("%x", byte_tmp);
    }

    printf("\n");

}


c语言实现爬楼梯的算法解释
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值