There is just a small bug in the line:
if(req<prox_avail)
Figure it out, its a minor one and easy to spot too. This should give you AC as far as your code is concerned .
And please donβt post the whole source code here, just provide the link to your code as it makes it tough for the other people to browse through the editorial.
Hello,
i executed my code with two different ways to calculate the minimum of days he has to be attendant.
timeDuration = All Lessons he should have attended.
int x = (int) Math.ceil(((timeDuration/100.0)*75));
int y = (int) Math.ceil(timeDuration*0.75);
With way 1 i got a wrong answer, way 2 worked. i tested both ways with some numbers and 1 and 2 output allways the same.
Can you give me an example where 1 is wrong ?
neel19
July 10, 2019, 3:53pm
24
Why am I getting Wrong Answer
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t,n,present_count,count,flag,i;
float attendence;
string s;
cin >> t;
while(t--)
{
cin >> n;
cin >> s;
present_count = 0,count = 0,flag = 0;
for(i=0; i<n; i++)
{
if(s[i] == 'P')
present_count++;
}
attendence = (float)present_count/n;
if(attendence >= 0.75)
{
cout << 0 << "\n";
continue;
}
else
{
for(i=2; i<n-2; i++)
{
if(s[i] == 'A')
{
if( (s[i-1] == 'P' || s[i-2] == 'P') && (s[i+1] == 'P' || s[i+2] == 'P') )
{
s[i] = 'P';
count++; present_count++;
attendence = (float)present_count/n;
if(attendence >= 0.75)
{
flag = 1;
break;
}
}
}
}
if(flag == 0)
cout << -1 << "\n";
else
cout << count << "\n";
}
}
}
What is is wrong in this code?
#include
#include<string.h>
using namespace std;
int main()
{
int D,T,i,j,P,change=0;
char S[1000];
cin>>T;
if(T>2000)
{
exit(0);
}
for(i=0;i<T;i++)
{
cin>>D>>S;
if(D>1000)
{
exit(0);
}
for(j=0;j<D;j++)
{
if(S[j]!=βAβ&&S[j]!=βPβ)
{
exit(0);
}
}
for(j=0;j<D;j++)
{
if(S[j]==βPβ)
{
P++;
}
}
if((P*100)/D>=75)
{
cout<<0;
}
else
{
for(j=2;j<D-2;j++)
{
if(S[j]=='A')
{
if(S[j-2]=='P'||S[j-1]=='P'||S[j+2]=='P'||S[j+1]=='P')
{
change++;
P=P+1;
if(((P*100)/D)>=75)
{
cout<<change;
break;
}
}
}
}
}
}
}
for x in range(int(input())):
d = int(input())
s = input()
a = s.count(βPβ)
b = 0
if a/d >= 0.75:
print(-1)
else:
for y in range(2,d-2):
if s[y] == βAβ and (s[y-1] == βPβ or s[y-2] == βPβ) and (s[y+1] == βPβ or s[y+2] == βPβ):
a += 1
b += 1
if a/d >= 0.75:
print(b)
break
else:
print(-1)
Whatβs Wrong With My Codeβ¦
ssjgz
January 23, 2020, 8:54am
28
You have an out-of-bounds access here:
s[d]='\0';
2 Likes
https://www.codechef.com/viewsolution/29305865
Can somebody find a test case for which this solution is wrong
I have tried all the test cases provided in this discussion thread.
ssjgz:
1 13 PAPAPAPPAPAAA
Thank you
found the error
1 Like
please help me where i am wrong
#include <bits/stdc++.h>
using namespace std;
using ll=long long int;
int main() {
// your code goes here
int t;
cin>>t;
while(tβ)
{
int n,i,p=0,prox=0,np=0;
string s;
double per;
cin>>n;
cin>>s;
if(n==1)
{
if(s[0]==βAβ)
cout<<1<<endl;
else
cout<<0<<endl;
}
else if(n==2)
{
if((s[0]==βAβ) && (s[1]==βAβ))
cout<<2<<endl;
else if(((s[0]==βAβ) && (s[1]==βPβ))|| ((s[0]==βPβ) && (s[1]==βAβ)))
cout<<1<<endl;
else
cout<<0<<endl;
}
else
{
for(i=0;i<n;i++)
{
if(s[i]==βPβ)
p++;
}
per=(double)p/n;
if(per>=0.75)
cout<<0<<endl;
else
{
for(i=0;i<n;i++)
{
if(i==0)
{
if((s[i]==βAβ) && ((s[i+1]==βPβ) || (s[i+1]==βPβ)))
{
p++;
np++;
s[i]=βPβ;
}
}
else if(i==1)
{
if((s[i]==βAβ) && ((s[i+1]==βPβ) || (s[i+2]==βPβ) ||(s[i-1]==βPβ)))
{
p++;
np++;
s[i]=βPβ;
}
}
else
{
if((s[i]==βAβ) && ((s[i+1]==βPβ) || (s[i+2]==βPβ) ||(s[i-1]==βPβ) || (s[i-2]==βPβ)))
{
p++;
np++;
s[i]=βPβ;
}
}
per=(double)p/n;
if(per>=0.75)
break;
}
//cout<<βper=β<<per<<βnp=β<<np<<endl;
if(per>=0.75)
cout<<np<<endl;
else
cout<<-1<<endl;
}
}
}
return 0;
}
ssjgz
August 1, 2020, 7:48am
36
Please either format your code or (better!) link to your submission - the forum software has mangled it and it wonβt compile!
1 Like
ssjgz
August 1, 2020, 4:26pm
38
Thanks! The following test input causes an out-of-bounds access in your solution:
1
5
AAAAA
2 Likes
That is correct output,i think.
For this input i get -1 as output that is correct
https://www.codechef.com/viewsolution/37764805
Please help me out with the code.Dummy testcases have passed.If anybody can find me some test cases which are outputting wrong from my code.
Thanks in advance.