刷题笔记



一、字符串

1.1 字符串最后一个单词的长度

1.2 计算某字母出现次数

1.3 明明的随机数

1.4 进制转换

    #include <stdlib.h>

    // 字符格式,非字符格式都能解析 
    const char* str = "1723 111 0xff";

    // 用于接收解析一段后的位置
    char* endptr;

    // 解析第一个
    strtol(str,&endptr,16);

    // 循环解析
    while ( strlen(endptr) != 0 )
    {
        cout << "fuck: "<<strtol(endptr,&endptr,16) << endl;
    }
    #include <string>
    string st = "  ff 0xff1";
    size_t p;
    cout << stoi(st,&p,16) << endl;
    cout << stoi(st.substr(p),&p,16) << endl;

1.5 句子逆序

    // 输入: ab b c d
    string str;
    while(cin >> str) {
        
    }

1.6 密码验证合格程序

    #include <regex>

    regex checkPattern("([\\w]{3}).*\\1");

二、数学

2.1 质数因子

    // 待分解的数
    int value;
    int temp = value;

    // 需要搜寻的最大质数就到 sqrt(value)
    for(int i=2; i <= temp && i <= sqrt(value);i++){
        // 同一个数,重复拆
        while(temp % i == 0){
            cout << "质因数:" << i;
            temp /= i;
        }
    }

    // 最后一次是否被分解
    if(temp != 1){
        cout << "质因数:" << temp;
    }

2.2 近似值

    #include <math.h>
    // 近似值
    double round(double x);