【持续更新】实用算法小点总结(怕忘QAQ)

没格式就没格式了吧(。ì _ í。)
#include <iostream>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
using namespace std;
加速输入输出:
cin.tie(nullptr);//输入!
cout.tie(nullptr);
std::ios::sync_with_stdio(false);//加速cin读入

全局变量的数组最多开:int a[2e4]2e4]
读入优化:
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
输出优化:
inline void write(int x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
        write(x/10);
    putchar(x%10+'0');//对应的char值
}
__int128的读入和输出
inline __int128 read()
{
    __int128 x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
 
inline void write(__int128 x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}
 
int main()
{
    __int128 a = read();
    write(a);
    return 0;
}



int main() {
    EOF不是char类型的;

    2月平年28天,闰年29天。闰年:(能被4整除,但不能被100整除) or (能被400整除)

    1s可以运行10 ^ 7
    ~10 ^ 8次

    int 稍大于2e
    +9;
    long long
    9e+18;
    unsigned long long
    1e+19;

    const double pi = acos(-1.0);//真正的pi值

    floor(sqrt(x) + 0.5);//四舍五入向下取整

    double a = 1.2323232;
    int x = 4;
    printf("%.*f", x, a);//控制输出的小数位数

    int a[20][20], b[20][20];
    memcpy(a, b, sizeof(b));//把b数组复制到a中
    long long  a[10],b[10];
    memcpy(a,b,sizeof(long long)*10);

    int abc = 123, de = 45, x = 6, y = 8, z = 9;
    char buf[20];
    sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);//buf的内容:12345689 长度为8

    string s;
    getline(cin, s);
    int h, m, s, d = 0;
    sscanf(s.c_str(), "%d:%d:%d (%d)", &h, &m, &s, &d);//蓝桥时差那道题的读入,要从字符数组读入

    if (strchr(buf, '1') != NULL)//在字符数组buf中查找单个字符

        int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    reverse(a, a + 10);//此时a[]={9,8,7,6,5,4,3,2,1,0};
    int b =[10];
    reverse_copy(a, a + 10, b);//把逆转后的结果放入b数组中

    //<ctype.h>头文件下的
    if (isalpha(int c));//是字母
    if (isdigit(int c));//是数字
    if (islower(int c));//是小写字母
    if (isupper(int c));//是大写字母
    toupper(int
    c);//转成大写
    tolower(int
    c);//转成小写

    string s = "ABSCD";
    transform(s.begin(), s.end(), s.begin(), ::tolower);//注意前面是三个参数,两个s.begin(),后面的tolower没有括号

    printf("%d %o %x", 10, 10, 10);//十进制10、八进制12、十六进制a

    精度问题时eps = 1e-6;

    特殊读入的四种常用方法:
    1.while(!cin.eof())
    2.string s;
    while (getline(cin, s))
        3.string
    s1, s2, ss;
    while (getline(cin, s1)) {
        stringstream ss(s1);//速度很慢
        while (s1 >> s2)
        ....
    }
    4.while(getchar())

    struct node//结构体内的模版化操作
    {
        int x, y;

        node(int x = 0, int y = 0) : x(x), y(y) {}

        friend bool operator<(node a, node b)

        return a.x<b.
        x;
    };

    int a[5] = {1, 2, 4, 6, 7};
    //二分实现,所以要求数组要有序!!,不存在则返回end(),也可用于vetcor、map等容器中
    lower_bound(a, a + 5, 6) - a;//返回第一个大于等6的数的下标
    upper_bound(a, a + 5, 3) - a;//返回第一个大于3的数的下标
    //拓展
    lower_bound(a, a + 5, 5, greater<int>()) - a;//第一个小于等于5
    upper_bound(a, a + 5, 5, greater<int>()) - a;//第一个小于5

    int a[5] = {3, 2, 5, 1, 6};
    sort(a, a + 5, greater<int>());//降序
    set<int, greater<int> > a;//默认降序排列
    priority_queue<int, vector<int>, greater<int> > q;
    priority_queue<int, vector<int>, cmp> q;//再单独写个函数

    int a[10],b[10][10];
    fill(a, a + 10, 233);
    fill(b[0],b[0]+10*10,233);

    vector<int> a(10);//指定变长数组的大小
    a.resize(11);//改变vector容器的大小

    map<key, value> m;
    m.count(key);//key出现的次数,有时相当于在m中查找key是否出现过

    //并集,去重,结果存到vector中,且需提前指定容器的大小
    int a[5] = {1, 2, 3, 4, 5};
    int b[5] = {4, 5, 6, 7, 8};
    vector<int> c(10);//一定要指定大小,否则会无法运行
    vector<int>::iterator it;
    it = set_union(a, a + 5, b, b + 5, c.begin());//set_union返回地址
    cout << it - c.begin() << endl;//元素个数
    //交集
    set<int> a, b, c;
    a.insert(1);
    a.insert(4);
    a.insert(3);
    b.insert(6);
    b.insert(3);
    set_intersection(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()));
    //最后一个参数不可用c.begin(),否则会无法运行,也不可令it=set_union(。。。)
    cout << "c中元素个数:" << c.size() << endl;


    string s;
    int k = s.find('@');//返回地址
    string ss = s.substr(0, k), sss = s.substr(k + 1);//截取子串

    int a[5] = {1, 2, 2, 4, 5};
    int m = unique(a, a + 5) - a;//返回不重复序列的最后一个下标,
    //附原理(把不重复的往前面移)
       iterator My_Unique (iterator first, iterator last)
        {
          if (first==last) return last;
 
           iterator result = first;
           while (++first != last)
           {
              if (!(*result == *first)) 
              *(++result)=*first;
              }
          return ++result;
        }


    deque<int> qr;//双端队列
    qr.push_back(x);
    qr.push_front(x);

    int p[5] = {1, 2, 3, 4, 5};
    do {

    } while (next_permutation(p, p + 5));//全排列
    //流读入,多次的时候记得清空流!!
    stringstream ss;
    int first, second;
    ss << "456";//将“456”字符串收入流
    ss >> first;
    cout << first << endl;
    ss.clear();//清除,定义的stringstream的名称.clear();
    ss << true;//bool值
    ss >> second;
    cout << second << endl;

    struct node{
        int a;
        int b;
    }
    vector<node> v;
    v.push_back({1,2});
    
    string s1, s2;
    cin >> s1 >> s2;
    //string.find()返回类型为string::size_type 查找不到时返回string::npos,一个很大的数,不是-1!!!
    if(s1.find(s2) != string::npos) cout << "yes" <<endl;//平均时间复杂度O(m+n)慎用
 
    long double 的输出格式:%Lf
    长度大于16的long long 转化为 double 会出错, 可以自己写个pow,或者用powl(long double a,long double b)
    95个可打印字符,32-126左右
    高效位运算函数:
   (1)int n = 15; //二进制为1111
     cout<<__builtin_popcount(n)<<endl;//输出4,判断有多少个1
   (2)int n = 15;//二进制为1111
    int m = 7;//111
    cout<<__builtin_parity(n)<<endl;//偶数个,输出0
    cout<<__builtin_parity(m)<<endl;//奇数个,输出1,判断1数目的奇偶性
   (3)int n = 1;//1
    int m = 8;//1000
    cout<<__builtin_ffs(n)<<endl;//输出1
    cout<<__builtin_ffs(m)<<endl;//输出4,末尾最后一个1的位置
   (4)int n = 1;//1
    int m = 8;//1000
    cout<<__builtin_ctz(m)<<endl;//输出3,末尾0的个数
    (5) __builtin_clzll(nll) 返回前导0的个数,ll表示unsigned Long long 
    return 0;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值