524 - Prime Ring Problem
Moderator: Board moderators
524 - PE
Hi, who can tell me why this code has PE , please ??? [code]
void ImprimeArrar(int * secuencia)
{
if (!tachado[secuencia[cantElementos-1]+1])
{
for (int i = 0 ; i < cantElementos -1 ; i++)
printf("%d ",secuencia[i]);
printf("%d\n",secuencia[cantElementos-1]);
}
}
[/code]
void ImprimeArrar(int * secuencia)
{
if (!tachado[secuencia[cantElementos-1]+1])
{
for (int i = 0 ; i < cantElementos -1 ; i++)
printf("%d ",secuencia[i]);
printf("%d\n",secuencia[cantElementos-1]);
}
}
[/code]
Last edited by aabiague on Tue Oct 31, 2006 4:05 am, edited 1 time in total.
you have to search first..
check this out..
http://online-judge.uva.es/board/viewtopic.php?t=4351
please delete your code..
check this out..
http://online-judge.uva.es/board/viewtopic.php?t=4351
please delete your code..
you may need a blank line after each case except for the last case..aabiague wrote:ok, I see the solution before, but I can't find my error, someone can tell me what happen why my code ????? Any sugestions ???
example
Code: Select all
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4
Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
-
- New poster
- Posts: 47
- Joined: Sun Nov 27, 2005 12:43 pm
compile error --- is the judge alrigth .....
both of problems 524 & 539 are just workin fine g++ compiler of mine
after a while i m solving in acm .... its changed now i guess....
here's my code for 524 .. plz tell me guys why compile error....
after a while i m solving in acm .... its changed now i guess....
here's my code for 524 .. plz tell me guys why compile error....
Code: Select all
# include <iostream>
# include <vector>
# include <string>
# include <cstring>
# include <algorithm>
using namespace std;
int prime[36]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0};
vector<int> v;
bool edges[35][35];
int count;
void backtrack(int i,int n)
{
extern int count;
if(v.size()==n-1)
{
if(prime[i+1]!=1)
{ return ;}
else if (prime[i+1]==1)
{
for( int j=0;j<v.size();j++)
cout<<v[j]<<" ";
cout<<i<<"\n";
//v.pop_back();
return ;
}
}
for(int j=2;j<=n;j++)
{
if(edges[i][j]==false && prime[i+j]==1)
{
count++;
edges[i][j]=true;
for(int k=2;k<=n;k++)edges[k][i]=true;
edges[j][i]=true;
v.push_back(i);
backtrack(j,n);
edges[i][j]=false;
edges[j][i]=false;
for(int k=2;k<=n;k++)edges[k][i]=false;
v.pop_back();
}
}
count--;
}
int main()
{ int n;
int count=0;
while(cin>>n)
{ cout<<"Case "<<(++count)<<":\n";memset(edges,false,sizeof(edges));backtrack(1,n);v.clear();}
return 0;
}
-
- Experienced poster
- Posts: 136
- Joined: Fri Apr 15, 2005 3:47 pm
- Location: Singapore
- Contact:
-
- Learning poster
- Posts: 63
- Joined: Tue Mar 07, 2006 6:51 pm
- Location: india
hi everybody i tried to solve this problem but getting WA ...plz help me ..here is code . My algorithm is
Start generating permutation and check the condition of primality and if condition voilate then prune it . here is my code kindly check it .
Start generating permutation and check the condition of primality and if condition voilate then prune it . here is my code kindly check it .
Code: Select all
#include<cstdio>
#include<iostream>
using namespace std;
int a[20];
int prime(int n)
{
if(n==1)
return 0;
if(n==2)
return 1;
if(n%2==0)
return 0;
for(int i=3;i*i<=n;i++)
if(n%i==0)
return 0;
return 1;
}
void perm(int k,int n,int* count)
{
int temp;
if(k==n && prime(a[0]+a[n-1]))
{
(*count)++;
for(int i=0;i<n-1;i++)
printf("%d ",a[i]);
printf("%d\n",a[n-1]);
}
else
{
for(int i=k;i<n;i++)
if(prime(a[k-1]+a[i]))
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
perm(k+1,n,count);
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}
int main()
{
int n,k=0,count;
while(scanf("%d",&n)==1)
{
count=0;
if(k!=0)
printf("\n");
k=k+1;
printf("Case %d:\n",k);
for(int i=0;i<20;i++)
a[i]=i+1;
perm(1,n,&count);
//printf("count=%d\n",count);
}
}
For input 10,
1st Part of Output:
Hope it helps.
1st Part of Output:
Code: Select all
Case 1:
1 2 3 4 7 6 5 8 9 10
1 2 3 4 7 10 9 8 5 6
1 2 3 4 9 8 5 6 7 10
1 2 3 8 5 6 7 4 9 10
1 2 3 8 5 6 7 10 9 4
1 2 3 10 7 4 9 8 5 6
1 2 3 10 7 6 5 8 9 4
...
Ami ekhono shopno dekhi...
HomePage
HomePage
-
- Learning poster
- Posts: 63
- Joined: Tue Mar 07, 2006 6:51 pm
- Location: india