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;
}