ZOJ1312(Prime Cuts)素数
//1813904 2009-04-01 20:54:29 Accepted 1312 C++ 0 188 Xredman
#include <iostream>
using namespace std;
int prlist[1000];
int pcnt;
int n, c;
void prime()
{
int kk, i;
bool flag;
prlist[1] = 1;
prlist[2] = 2;
prlist[3] = 3;
pcnt = 4;
for(kk = 5; kk < 1000; kk += 2)
{
flag = true;
for(i = 2;i < pcnt; i++)
if(kk % prlist[i] == 0 )
{
flag = false;
break;
}
if(flag)
prlist[pcnt++] = kk;
}
}
void solve()
{
int cnt = 1,tt, i, j;
while(prlist[cnt] <= n)
cnt++;
cnt--;
if(cnt % 2)
{//odd
tt = 2 * c - 1;
if(tt >= cnt)
{
for(i = 1; i <= cnt; i++)
cout<<" "<<prlist[i];
}
else
{
for(j = 0, i = (cnt - tt) / 2 + 1; j < tt; j++, i++)
cout<<" "<<prlist[i];
}
}
else
{//even
tt = 2 *c;
if(tt >= cnt)
{
for(i = 1; i <= cnt; i++)
cout<<" "<<prlist[i];
}
else
{
for(j = 0, i = (cnt - tt) / 2 + 1; j < tt; j++,i++)
cout<<" "<<prlist[i];
}
}
}
int main()
{
prime();
prlist[pcnt] = 10000;
while(cin>>n>>c)
{
cout<<n<<" "<<c<<":";
solve();
cout<<endl<<endl;
}
return 0;
}
//21 2: 5 7 11
#include <iostream>
using namespace std;
int prlist[1000];
int pcnt;
int n, c;
void prime()
{
int kk, i;
bool flag;
prlist[1] = 1;
prlist[2] = 2;
prlist[3] = 3;
pcnt = 4;
for(kk = 5; kk < 1000; kk += 2)
{
flag = true;
for(i = 2;i < pcnt; i++)
if(kk % prlist[i] == 0 )
{
flag = false;
break;
}
if(flag)
prlist[pcnt++] = kk;
}
}
void solve()
{
int cnt = 1,tt, i, j;
while(prlist[cnt] <= n)
cnt++;
cnt--;
if(cnt % 2)
{//odd
tt = 2 * c - 1;
if(tt >= cnt)
{
for(i = 1; i <= cnt; i++)
cout<<" "<<prlist[i];
}
else
{
for(j = 0, i = (cnt - tt) / 2 + 1; j < tt; j++, i++)
cout<<" "<<prlist[i];
}
}
else
{//even
tt = 2 *c;
if(tt >= cnt)
{
for(i = 1; i <= cnt; i++)
cout<<" "<<prlist[i];
}
else
{
for(j = 0, i = (cnt - tt) / 2 + 1; j < tt; j++,i++)
cout<<" "<<prlist[i];
}
}
}
int main()
{
prime();
prlist[pcnt] = 10000;
while(cin>>n>>c)
{
cout<<n<<" "<<c<<":";
solve();
cout<<endl<<endl;
}
return 0;
}
//21 2: 5 7 11