返回

C语言笔记2

目录

记忆化存储

c-style

注意memo数组在 memo[49]={-1}后其他元素为0.
如果想使所有元素为**-1**则要写函数遍历。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
long long allIN(int num,long long *memo){
    if(memo[num-1]!=0)return memo[num-1];
    memo[num-1]= num*allIN(num-1,memo);
    return memo[num-1];
}
int main(){
    long long memo[999]={-1};
    memo[0]=1;
    int n;
    scanf("%d",&n);

    long long tmp=0;
    for(int i=n;i>0;i--){
        tmp+=allIN(i,memo);
    }


    printf("%lld",tmp);
}

cpp-style

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>

// 计算阶乘的递归函数,使用记忆化存储
long long factorial(int num, std::vector<long long> &memo) {
    if (num == 0 || num == 1) return 1; 
    if (memo[num] != -1) return memo[num]; 
    memo[num] = num * factorial(num - 1, memo); 
    return memo[num];
}

int main() {
    int n;
    std::cout << "请输入一个正整数 (1 <= n <= 50): ";
    std::cin >> n;

    if (n < 1 || n > 50) {
        std::cout << "输入错误,请输入 1 到 50 之间的整数。\n";
        return 1;
    }

    // 初始化记忆化存储的 vector
    std::vector<long long> memo(n + 1, -1);
    memo[0] = 1; // 0! = 1

    long long sum = 0;
    for (int i = 1; i <= n; i++) {
        sum += factorial(i, memo);
    }

    std::cout << "结果: " << sum << std::endl;

    return 0;
}

高精度加法

例题: [NOIP1998 普及组] 阶乘之和

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>

int main(){
    int a[101]={0},b[101]={0};
    int num;

    a[0]=1;b[0]=1;
    scanf("%d",&num);
    
    for(int i=2;i<=num;i++){
        for(int j=0;j<100;j++){
            a[j]*=i;                //每位乘n
        }
        for(int j=0;j<100;j++){     //进位
            if(a[j]>9){
                a[j+1]+=a[j]/10;
                a[j]%=10;
            }
        }
        for(int j=0;j<100;j++){
            b[j]+=a[j];
            if(b[j]>9){
                b[j+1]+=b[j]/10;
                b[j]%=10;
            }
        }
    }
    int i;
    for (i=100;i>=0&&b[i]==0;i--);
    for(int j=i;j>=0;j--) printf("%d",b[j]);
    return 0;
}

计算器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int com(string s,int size){
    char oper='0';
    int a=0,b=0;
    for(int i=0;i<size;i++){
        if(s[i]==' ') continue;
        if(s[i]<'0'||s[i]>'9'){
            oper = s[i];
            continue;
        }
        if(oper=='0'){
            a*=10;
            a+=(s[i]-'0');
        }else{
            b*=10;
            b+=(s[i]-'0');
        }
    }
    switch(oper){
    case('+'):
        return a+b;
    case('-'):
        return a-b;
    case('*'):
        return a*b;
    case('/'):
        return a*1.0/b;
    case('%'):
        return a%b;
    }
}

stdargs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdarg.h>

static void va_print(const char* file, const long line, const char *tag, const char *format, ...) {
    va_list args;
    va_start(args, format);
    printf("[%s](%s:%ld): ",tag, file, line);
    vprintf(format, args);
    va_end(args);
    printf("\n");
}

#define TEST_DEBUG(...) va_print(__FILE__, __LINE__, __VA_ARGS__)

int main() {
    TEST_DEBUG("Tag1", "Hello, %s!", "World");
    TEST_DEBUG("Tag2", "The answer is %d.", 42);
    TEST_DEBUG("Tag3", "No extra args");
    return 0;
}


// 应用
#ifdef SFUD_DEBUG_MODE
#ifndef SFUD_DEBUG
#define SFUD_DEBUG(...) sfud_log_debug(__FILE__, __LINE__, __VA_ARGS__)
#endif /* SFUD_DEBUG */
#else
#define SFUD_DEBUG(...)
#endif /* SFUD_DEBUG_MODE */

#ifndef SFUD_INFO
#define SFUD_INFO(...)  sfud_log_info(__VA_ARGS__)
#endif

一种断点方式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#ifdef SFUD_DEBUG_MODE
#define SFUD_ASSERT(EXPR)                                                      \
if (!(EXPR))                                                                   \
{                                                                              \
    SFUD_DEBUG("(%s) has assert failed at %s.", #EXPR, __FUNCTION__);          \
    while (1);                                                                 \
}
#else
#define SFUD_ASSERT(EXPR)
#endif

一种类JSON化的结构体定义方式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 定义一个作用域为本文件的枚举变量
enum sfud_write_mode {
    SFUD_WM_PAGE_256B = 1 << 0,                            /**< write 1 to 256 bytes per page */
    SFUD_WM_BYTE = 1 << 1,                                 /**< byte write */
    SFUD_WM_AAI = 1 << 2,                                  /**< auto address increment */
    SFUD_WM_DUAL_BUFFER = 1 << 3,                          /**< dual-buffer write, like AT45DB series */
};

// 定义一个列表的表示方法
typedef struct {
    char *name;
    uint8_t id;
} sfud_mf;

// 用于清晰显示变量的定义
#define SFUD_MF_ID_CYPRESS                             0x01
#define SFUD_MF_ID_FUJITSU                             0x04
...


// 重点
#ifdef SFUD_USING_FLASH_INFO_TABLE
/* SFUD supported flash chip information table. If the flash not support JEDEC JESD216 standard,
 * then the SFUD will find the flash chip information by this table. You can add other flash to here then
 *  notice me for update it. The configuration information name and index reference the sfud_flash_chip structure.
 * | name | mf_id | type_id | capacity_id | capacity | write_mode | erase_gran | erase_gran_cmd |
 */
#define SFUD_FLASH_CHIP_TABLE                                                                                       \
{                                                                                                                   \
    {"AT45DB161E", SFUD_MF_ID_ATMEL, 0x26, 0x00, 2L*1024L*1024L, SFUD_WM_BYTE|SFUD_WM_DUAL_BUFFER, 512, 0x81},      \
    {"W25Q40BV", SFUD_MF_ID_WINBOND, 0x40, 0x13, 512L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20},                        \
    {"W25X40CL", SFUD_MF_ID_WINBOND, 0x30, 0x13, 512L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20},                        \
    {"W25X16AV", SFUD_MF_ID_WINBOND, 0x30, 0x15, 2L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20},                    \
    {"W25Q16BV", SFUD_MF_ID_WINBOND, 0x40, 0x15, 2L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20},                    \
    {"W25Q32BV", SFUD_MF_ID_WINBOND, 0x40, 0x16, 4L*1024L*1024L, SFUD_WM_PAGE_256B, 4096, 0x20},                    
}
#endif /* SFUD_USING_FLASH_INFO_TABLE */

// How to Use
sfud_mf test[20] = SFUD_MF_TABLE;

定义一个函数指针

1
2
3
4
typedef sfud_err (*spi_write_read_func)(const uint8_t *write_buf, size_t write_size, uint8_t *read_buf, size_t read_size);

// Usage
spi_write_read_func my_spi_func;
Licensed under CC BY-NC-SA 4.0