I am getting segmentation faults when i run my code can anyone explain me why this is happening though i have tried to look for any errors but couldn’t find it.
is it that since here no. of presents is already above 75% so output must be zero
which in my case is coming out to be -1 because i didn’t consider a case for that…
now updated the code but again the problem is occuring as in the last message
updated codes source code
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int T;
cin>>T;
if (T<=200 && T>=1){
for(int k =0;k<T;k++){
int D;
cin>>D;
if (D<=1000){
char S[D];
for (int i=0; i<D; i++){
cin>>S[i];
}
int count =0;
int P =0;
for (int i=0; i<2;i++){
if (S[i]=='P')
P++;
}
for (int i=2;i<=(D-3);i++){
if (S[i]=='A') {
if((S[i-1]=='P' || S[i-2]=='P') && (S[i+1]=='P' || S[i+2]=='P')){
count++;
}
}
else {
P++;
}
}
for (int i=(D-2); i<D; i++){
if (S[i]=='P')
P++;
}
int req = ceil(0.75*D);
int proxy = req - P;
if (P>=req)
cout<<0;
else if (P< req)
cout<<proxy;
else
cout<< -1;
}
}
}
return 0;
}
i m really sorry a noob like me is eating up a lot of time of yours.
okay fixed that
now the thing is again same problem is occurring
Have tested the sample case - working fine output is 1
have tested the 26 days case working fine output is 0
have tested he 25 case and output is -1
int main() {
int T;
cin>>T;
if (T<=200 && T>=1){
for(int k =0;k<T;k++){
int D;
cin>>D;
if (D<=1000){
char S[D];
for (int i=0; i<D; i++){
cin>>S[i];
}
int count =0;
int P =0;
for (int i=0; i<2;i++){
if (S[i]=='P')
P++;
}
for (int i=2;i<=(D-3);i++){
if (S[i]=='A') {
if((S[i-1]=='P' || S[i-2]=='P') && (S[i+1]=='P' || S[i+2]=='P')){
count++;
}
}
else {
P++;
}
}
for (int i=(D-2); i<D; i++){
if (S[i]=='P')
P++;
}
int req = ceil(0.75*D);
int proxy = req - P;
if (P>=req)
cout<<0;
else if (P< req && count>= proxy)
cout<<proxy;
else
cout<< -1;
}
}
}
return 0;
}
can you specifically find out the mistake over this segment of my code :
int req = ceil(0.75*D);
int proxy = req - P;
if (P>=req)
cout<<0;
else if (P< req && count>= proxy)
cout<<proxy;
else
cout<< -1;
I strongly feel something is wrong over here…
Thnx… a lot for helping me out and guiding me to find out my mistakes on my own.
Finally this one got submitted.
But how did you find those test cases so quickly which i couldn’t ?
anyways be prepared cause i will be bugging you from next time when i fail. I hope you won’t mind
but i will try out a lot before approaching you, that’s a promise
I usually write a random testcase generator to go with my solutions, and so have 10000 or so testcases (with known correct solutions) which I can test other people’s code against
i wrote a code #include
using namespace std;
int main(){
int T;
cin>>T;
for (int k=0;k<T;k++){
int N;
cin>>N;
N=N*2;
int ar[N];
// loop 1 to take values in the array as ques 1,score,ques 2,score and goes on....
for (int i =0; i<N;i++){
cin>>ar[i];
}
int score = 0;
// loop2 begins with entire purpose of calculating the total score
for (int i=1;i<N;i=i+2){
if (ar[i-1]<=8){
score = score + ar[i];
if (i>=3){
//loop 2.1 begins here
for (int j = 0; j<i-1;j=j+2){
if (ar[j]==ar[i-1]) //chck if the ques no. has been repeated in the previous entries
{
if (ar[j+1]>ar[i]){
score = score -ar[i];
}
else {
score = score - ar[j+1];
}
}
}
} // counter a problem when only one set of entry is made in the array
} // this condition is to ensure that entire scoring is considered for scorable problems
}
cout<<score<<endl;
}
return 0;
}
but when i tried running custom inputs i got the correct output as shown in the picture
but when i tried the other run option (not with the custom inputs)
i got two things to share : First one is a segmentation fault as show in image below