Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code is giving "wrong answer" on SPOJ but works perfectly on ideone (have tried hundreds of test cases).

When I use the exact logic and implement it using C language then it works perfectly on SPOJ.

ideone link : www.ideone.com/MUVrrE
Problem on spoj : www.spoj.com/problems/ONP/​

Java
import java.io.*;
class abc
{
public static void main(String[] args) throws Exception
{
try{
    String x="",y="",str="";
    String [] A= new String[20];
    String op="";
    int i=0, j=0, k=0, ctr=0, n=0;
    BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
    n=Integer.parseInt(br.readLine());
    for(i=0;i<n;i++)
        A[i] = br.readLine();
    for(k=0;k<n;k++)
        {
        str=A[k];
        for(i=0;i<str.length();i++)
            {
            if(str.charAt(i)=='(')
                {
                ctr++;
                for(j=i+1;j<str.length();j++)
                    {
                    if((str.charAt(j)=='+' || str.charAt(j)=='-' || str.charAt(j)=='*' || str.charAt(j)=='/' || str.charAt(j)=='^') && ctr==1)
                        {
                        op+=str.charAt(j);
                        x=str.substring(0,j);
                        y=str.substring(j+1);
                        str=x+y;
                        }
                    if(str.charAt(j)=='(')
                        ctr++;
                    if(str.charAt(j)==')')
                        ctr--;
                    if(ctr==0)
                        {
                        x= str.substring(0,j);
                        y= str.substring(j);
                        str= x+ op+ y;
                        op="";
                        break;
                        }
                    }
                }
                ctr=0; op="";
                }
            for(i=0;i<str.length();i++)
                {
                if(str.charAt(i)!=')' && str.charAt(i)!='(')
                    System.out.print(str.charAt(i));
                }
            if(k!=n-1)
                System.out.println();
            }
        }
    catch(Exception e)
        {
        return;
        }
    }
}
Posted
Updated 28-Aug-15 14:01pm
v3
Comments
PIEBALDconsult 28-Aug-15 19:41pm    
How are we supposed to even know what's supposed to do?

1 solution

Lets guess the problem is not in your code. The problem is linked to an external behaviour.

Solution: it's time to learn the debugger.

Run your program step by step with the debugger, pay special attention to variables.
Compare behaviour with ideome and SPOJ.

You should find pretty quickly what is wrong.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900