c++常用代码集(OJ篇)

先贴一下别人总结的代码集:

ACM、PAT、CSP、OJ题目常用代码模板

ACM在线模版-f-zyj

1.字符串分割

void SplitString(const std::string& s, std::vector& v, const std::string& c) {
    std::string::size_type pos1 = 0, pos2 = s.find(c);
    while (std::string::npos != pos2)
    {
        v.push_back(s.substr(pos1, pos2 - pos1));
        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if (pos1 != s.length()) v.push_back(s.substr(pos1));
}

2.判断质数(素数)

bool IsPrime(int n) {
    if (n == 1 || n == 0)return 0;
    for (int i = 2; i*i <= n; i++)
        if (n%i == 0) return 0;
    return 1;
}

你可能感兴趣的:(数据结构与算法)