please someone tell whats wrong with this program
https://www.codechef.com/viewsolution/13944522
what is wrong with my code i couldn’t understand why i got a wrong answer…
import java.util.*;
class snckdwn {
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
int t = ob.nextInt();
while(t-- > 0)
{
int n = ob.nextInt();
char a[][] = new char[2][n];
for(int i=0;i<2;i++)
{
String s = ob.next();
for(int j=0;j<n;j++)
{
a[i][j] = s.charAt(j);
}
}
boolean upper = false;
boolean lower = false;
boolean hl = false;
boolean left = false;
boolean leftdiag = false;
boolean rightdiag = false;
int count = 0;
for(int i=0;i<n;i++)
{
char te, te1, be, be1;
te = a[0][i];
be = a[1][i];
if(hl==false)
{
if(te=='*')
upper = true;
if(be=='*')
lower = true;
}
if(upper==true&&lower==true)
{
upper = lower = false;
hl = true;
count++;
}
if(te=='*'&&be=='.')
{
if(rightdiag==true)
{
rightdiag = false;
continue;
}
if(leftdiag==true)
{
count++;
continue;
}
leftdiag = true;
if(left==true)
{
count++;
continue;
}
left = true;
}
else if(te=='.'&&be=='*')
{
if(leftdiag==true)
{
leftdiag = false;
continue;
}
if(rightdiag==true)
{
count++;
continue;
}
rightdiag = true;
if(left==true)
{
count++;
continue;
}
left = true;
}
else if(te=='*'&&be=='*')
{
if(left==false)
left = true;
else
{
count++;
left = false;
}
}
else
continue;
}
System.out.println(count);
}
}
}
@code_man Ur code gives wrong output for following test cases:
4
7
*.....*
.*****.
7
.*****.
*.....*
11
...*...*...
....***....
11
....***....
...*...*...
The correct output is :
5
5
3
3
but your code gives output :
6
6
4
4
Could anyone help me in figuring out the test cases where my solution is failing? i tried hard but could get any.!!
Link to solution is CodeChef: Practical coding for everyone .
can’t find what’s wrong in this code
#include
using namespace std;
int main(){
int t,n;
cin>>t;
while(t--){
cin>>n;
char a[2][n];
for(int i=0;i<2;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
int fences = 0;
char prev1='.', prev2='.', row1=false, row2=false;
for(int i=0;i<n;i++) {
if(a[0][i]=='*'){
row1=true;
}
if(a[1][i]=='*'){
row2=true;
}
if((row2==row1)and(row1==true)){
fences++;
break;
}
}
for(int i=0;i<n;i++){
if(((a[0][i]=='*')and(a[1][i]=='*'))and((prev1=='*')or(prev2=='*'))) {
fences++;
if(prev1=='*')
prev2='*';
else if(prev2=='*')
prev1='*';
continue;
}
if((a[0][i]=='*') and (prev1!='*')){
prev1='*';
}
else if((a[0][i]=='*') and (prev1=='*')) {
fences++;
prev2='.';
}
if((a[1][i]=='*') and (prev2!='*')){
prev2='*';
}
else if((a[1][i]=='*') and (prev2=='*')){
fences++;
prev1='.';
}
}
cout<<fences<<endl;
}
}
@dpraveen, @kingofnumbers
can we have the test cases now that test is over and we cannot even submit to check if our approach is correct.
why my code is giving wrong answer … plz help
#include
#include
using namespace std;
int main()
{
int t,n,i,counta,countb,j,k,ans;
cin>>t;
while(t–)
{
cin>>n;
char a=(char)malloc((n+1)sizeof(char)),b=(char)malloc(sizeof(char)(n+1));
cin>>a>>b;
i=0;j=0;counta=0;countb=0;ans=0;
for(k=0;k<n;k++)
{
if(a[k]==’’)
{
counta++;
i++;
}
if(b[k]==’’)
{
countb++;
j++;
}
if(i>1&&j>1)
{
ans++;
i=1;j=1;
}
else if(i>1 && j<2)
{
ans++;
i=1;
j=0;
}
else if(i<2 && j>1)
{
ans++;
j=1;i=0;
}
}
if(counta>0 && countb>0)
ans++;
cout<<ans<<endl;
}
return 0;
}
Your code raises an error empty character constant because in this condition " if(str1.at(a_i)==’’) " your are comparing a character with empty character. If I assume that your counting ‘*’ there then your code fails for
input:
5
.*.*.
*.*.*
Your Output
4
Expected :
3
Your code also fails if I assume that char with you are comparing is’.’ instead of ‘*’ as mentioned I my previous assumption. Correct me if I’m wrong.
Else you can paste your code as a answer so that other users can also take a chance to find error case
thanxx man.
thank you for trying! @sai_rathan
Can anyone provide a test case for which the code does not work?