算法技巧

Kelvin

提速技巧

  • inline
    将反复调用的函数添加inline修饰符
    1
    2
    3
    inline int add(int x, int y) {
    return x + y;
    }
  • register
    将多次使用的值添加register修饰符,作用是将该字段存入cpu而非内存中
    1
    2
    3
    for(register int i = 0; i < n; i++) {
    ...
    }
  • 数字读取
    1
    2
    3
    4
    5
    6
    7
    int read(){
    int x;
    char ch = getchar();
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = (x<<1)+(x<<3)+ch-'0', ch=getchar();
    return x;
    }
    先读取前面非数字部分,再依次读取数字,(x<<1)+(x<<3)=x*10

typedef和#define

1
2
typeof long long LL;
define re register
  • Title: 算法技巧
  • Author: Kelvin
  • Created at: 2023-02-27 00:00:00
  • Updated at: 2023-05-11 21:41:07
  • Link: https://yanwc.com/2023/02/27/2023-03-09-skill/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
算法技巧