USACO "bead" problem
This is "USACO" bead problem, why doesn't it print the number?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int n,i,out=0,w=0,pre,k;
cin>>n;
char a[n];
for(int q=0;q<n;q++)
{
cin>>a[q];
}
while(w<n)
{
pre=0;
for(int r=w;r>=0;r++)
{
if(r==n)r=0;
if(a[w]='r')
{
if(a[r]=='b')break;
}
if(a[w]=='b')
{
if(a[r]=='r')break;
}
pre++;
k=r+1;
}
for(int e=k;e>=0;e++)
{
if(e==n)e=0;
if(a[w]='r')
{
if(a[e]=='r')break;
}
if(a[w]=='b')
{
if(a[e]=='b')break;
}
pre++;
}
if(pre>out)out=pre;
w++;
}
cout<<out<<endl;
cin>>i;
return 0;
}
|
Last edited on
PLEASE HELP MEEEEE . . .
It's EMERGENCY :(
If you never execute the break
statements, you're never going to exit the loop since r>=0
will always be true.
Thank you but I understood my answer .
If you want to know the correct answer of it (that accepted from USACO)you can see it here :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
/*
ID:
PROG: beads
LANG: C++
*/
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream fout ("beads.out");
ifstream fin ("beads.in");
int n,i,out=0,w=0,pre,k;
fin>>n;
char a[n],num1,num2;
for(int q=0;q<n;q++)
{
fin>>a[q];
}
for(int l=0;l<n;l++)
{
w=l;
pre=0;
num1=a[w];
for(int r=w;r>=0;r++)
{
if(r==n)r=0;
num2=a[r];
if(num1=='r')
{
if(num2=='b') break;
}
if(num1=='b')
{
if(num2=='r') break;
}
pre++;
if(num1=='w')num1=num2;
if(pre==n)
{
break;
}
}
k=pre+w;
if(k>=n)k=k%n;
num1=a[k];
if(n>pre)
{
for(int e=k;e>=0;e++)
{
num2=a[e];
if(e==n-1)e=-1;
if(num1=='r')
{
if(num2=='b')break;
}
if(num1=='b')
{
if(num2=='r')break;
}
if(num1=='w')num1=num2;
pre++;
}
}
if(pre==n)
{
out=pre;
break;
}
else if(pre>out)
{
out=pre;
}
}
fout<<out<<endl;
return 0;
}
|
if you used it please tell me ,I will be happy if I find out that my program would be helpful:)
Last edited on
Topic archived. No new replies allowed.