java - codesprint2 programming contest answer

Posted by arya on Stack Overflow See other posts from Stack Overflow or by arya
Published on 2012-01-10T05:42:35Z Indexed on 2012/06/19 9:17 UTC
Read the original article Hit count: 325

Filed under:

I recently took part in Codesprint2. I was unable to submit the solution to the following question. http://www.spoj.pl/problems/COINTOSS/ ( I have posted the link to the problem statement at spoj because the codesprint link requires login )

I checked out one of the successful solutions, ( url : http://pastebin.com/uQhNh9Rc ) and the logic used is exactly the same as mine, yet I am still getting "Wrong Answer"

I would be very thankful if someone could please tell me what error I have made, as I am unable to find it. Thank you.

My code :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.StringTokenizer;


public class Solution
{

static double solve( int n, int m )
{
    if( m==n )
        return 0;

    if( m==0 )
        return ( Math.pow( 2, n+1 ) - 2 );

    else
    {
        double res = 1 + ( Double )(  solve( n, m+1 ) + solve( n, 0 ))/2;
        return res;
    }
    }


public static void main( String[] args ) throws IOException
{

BufferedReader br = new BufferedReader( new InputStreamReader( System.in ));
int n, m;

int t = Integer.parseInt( br.readLine() );
StringTokenizer tok;
String s;

for( int T=0; T<t; T++ )
{
    s = br.readLine();
    tok = new StringTokenizer( s );

    n = Integer.parseInt( tok.nextToken() );
    m = Integer.parseInt( tok.nextToken() );

    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);

    System.out.println( df.format ( solve( n, m ) ));
}


}

}

© Stack Overflow or respective owner

Related posts about java