Posted: Tue Jan 17, 2006 10:22 pm
So now you got it?If the size of the center list exceeds the limits of the list of prime numbers between 1 and N, the list of prime numbers between 1 and N (inclusive) should be printed.
So now you got it?If the size of the center list exceeds the limits of the list of prime numbers between 1 and N, the list of prime numbers between 1 and N (inclusive) should be printed.
Code: Select all
#include<iostream.h>
const int MAX=1000;
int list[1001];
int nlist[200];
int n,num;
int i,j,k;
int count;
int len;
void initial() //Eratosthenes
{
list[0]=1;
for(i=1;i<=1000;i++)
list[i]=i;
list[1]=0;
i=2;
while(i<1000)
{
k=2*i;
while(k<=1000) // FIRST:DELETE 2N+2,N=0 1 2 ... 1000
{
list[k]=0;
k=k+i;
}
i=i+1;
while(list[i]==0&&i<1000)
i=i+1;
}
j=1;
for(i=0;i<=1000;i++)
if(list[i]!=0)
{
nlist[j]=list[i];
j=j+1;
}
len=j;
//for(i=1;i<len;i++)
// cout<<nlist[i]<<endl;
//cout<<len<<endl;
}
void solve()
{
count=0;
int pnum,pos;
for(i=1;nlist[i]<=n&&count<168;i++) //168 is the number of prime number in the 1000
count++;
if(count/2<=num)
{
pnum=count;
pos=1;
}
if(count/2>num)
{
if(count%2==0)
{
pnum=num*2;
pos=count/2+1-num;
}
else
{
pnum=num*2-1;
pos=count/2+1-(num-1);
}
}
cout<<count<<" "<<pos<<" "<<pnum<<endl;
cout<<n<<" "<<num<<": ";
for(i=pos;pnum>1;i++,pnum--)
cout<<nlist[i]<<" ";
cout<<nlist[i]<<endl;
cout<<endl;
}
void read()
{
while(cin>>n>>num)
{
solve();
}
}
int main()
{
initial();
read();
return 0;
}
Code: Select all
#include <iostream>
#include <vector>
using namespace std;
int main(){
int N,C;
while(cin >> N >> C){
vector<char> T=vector<char>(N+1,3);
// 1 -> primer
// 2 -> no primer
// 3 -> no calculat
T[1]=1;
T[2]=1;
for(int i=3;i<=N;i++){
bool primer = true;
for(int j=2;j<i;j++){
if(T[j]==1){
if(i%j==0) primer=false;
}
}
T[i]=(primer)?1:2;
}
int count=0;
for(int i=1;i<=N;i++){
if (T[i]==1){
count++;
}
}
cout << N << " " << C << ":";
int inici, fi, quans;
if(count%2==0){
quans=C*2;
inici = count/2 - (quans/2);
fi = count/2 + (quans/2)-1;
}else{
quans=C*2-1;
inici = count/2 - (quans/2);
fi = count/2 + (quans/2);
}
quans=0;
for(int i=1;i<=N;i++){
if (T[i]==1){
if(quans>=inici && quans <=fi)
cout << " " << i;
quans++;
}
}
cout << endl << endl;
}
}
Code: Select all
2 2
3 3
Code: Select all
2 2: -858993460 1 2 -858993460
3 3: -858993460 1 2 3 -858993460
Code: Select all
if(r>count) to (r>=count)... but its still a wa i dont no why plz help...
people are not rewarded for participating but for winning....aliter
If the size of the center list exceeds the limits of the list of prime numbers between 1 and N, the list of prime numbers between 1 and N (inclusive) should be printed.
Code: Select all
deleted
You are checking r >= count where r is the C you read into your program.Your program will read in a number N; determine the list of prime numbers between 1 and N; and print the C*2 prime numbers from the center of the list if there are an even number of prime numbers or (C*2)-1 prime numbers from the center of the list if there are an odd number of prime numbers in the list.
Code: Select all
/*
* 406 prime cuts
* submission WA WA WA RF WA WA
* coded at 24th march,2006 at 2:32pm
*
*/
#include <stdio.h>
int primes[170];
int k=0;
int isprime(int n) {
if(n<2) return 0;
if(n==2) return 1;
if(n%2==0) return 0;
for(int i=3;i*i<=n;i+=2) {
if(n%i==0) return 0;
}
return 1;
}
void build(){
int i;
primes[0]=1;
primes[++k]=2;
for(i=3;i<=1000;i+=2) {
if(isprime(i)) {
primes[++k]=i;
}
}
}
int search(int n) {
int i;
for(i=0;i<170;i++) {
if(primes[i]>n) break;
}
return i;
}
void print(int a,int b) {
int i;
for(i=a;i<=b;i++) {
if(primes[i]) printf(" %d",primes[i]);
}
printf("\n\n");
}
int main() {
int num;
int c;
int limit;
int start;
int end;
build();
/*
freopen("f:\\input.in","r",stdin);
freopen("f:\\output.out","w",stdout);*/
//printf("k--> %d\n",k);
//scanf("%d",&k);
while(scanf("%d %d",&num, &c)==2) {
limit=search(num);
//printf("limit --> %d\n",limit);
//printf("primes 0 --> %d\n",primes[0]);
if(limit%2==0) {
start=limit/2-c;
end=start+2*c-1;
}else {
start=limit/2-c+1;
end=start+2*c-2;
}
if(end>limit||start<0) {
printf("%d %d:",num,c);
print(0,limit-1);
}else {
printf("%d %d:",num,c);
print(start,end);
}
}
return 0;
}