1007

作者: 峡迩 | 来源:发表于2017-07-16 14:47 被阅读0次
    #include<iostream>
    #include<string>
    #include<vector>
    #include<cmath>
    
    using namespace std;
    
    bool judge_zhishu(unsigned num)
    {
        for (unsigned i = 2; i <= sqrt(num); ++i)
        {
            if (0 == num%i)
                return false;
        }
        return true;
    }
    
    vector<int> zhishu(unsigned n)
    {
        vector<int> zhishu;
        if (n < 2)
        {
            return zhishu;
        }
        else
        {
            if (n == 2)
            {
                zhishu.push_back(2);
                return zhishu;
            }
            else
            {
                for (unsigned i = 2; i <= n; ++i)
                {
                    if (judge_zhishu(i))
                        zhishu.push_back(i);
                }
            }
        }
        return zhishu;
    }
    
    
    int main()
    {
        unsigned  n=0;
        cin >> n;
        vector<int> zhishu_numbers = zhishu(n);
    
        auto beg = zhishu_numbers.cbegin();
        auto next = beg + 1;
        unsigned count = 0;
        for (; next != (zhishu_numbers.cend()); ++beg,++next)
        {
            if ((*next - *beg) == 2)
                ++count;
        }
        cout << count;
    
    
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:1007

          本文链接:https://www.haomeiwen.com/subject/jgwtkxtx.html