zoj 1312 - 行者无疆 始于足下 - 行走,思考,在路上

zoj 1312

xiaohanyu posted @ Thu, 13 May 2010 08:05:51 +0800 in Algorithm with tags ZOJ , 1287 readers

coding!coding!为什么这么简单的题目愣是做不对呢?

在网上找了相应正确的代码做了测试,没有发现哪里不对。求高人指点一二。

算法是永远的痛。加油加油。

#include <iostream>
#include <vector>
using namespace std;

bool is_prime(int n)
{
    for (int i = 2; i*i <= n; ++i)
    {
        if (n % i == 0)
        {
            return false;
        }
    }

    return true;
}

int get_prime_list(int n, vector<int> &prime_list)
{
    prime_list.push_back(1);
    prime_list.push_back(2);
    prime_list.push_back(3);
    
    for (int i = 5; i <= n; i = i+2)
    {
        if (is_prime(i))
        {
            prime_list.push_back(i);
        }
    }

    return 0;
}


int main(int argc, char *argv[])
{
    int N, C;
    int cases = 0;
    
    while (cin >> N >> C)
    {
        vector<int> v;
        get_prime_list(N, v);
        cout << N << " " << C << ":";

        int center = v.size() / 2;

        if (center + C > v.size())
            for (int i = 0; i < v.size(); i++)
                cout << " " << v[i];
        else
            for(int i = center - C + (v.size() % 2 ? 1 : 0); i < center + C; i++)
                cout << " " << v[i];

        cout << endl << endl;
    }
    
    return 0;
}

Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee