ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1001. Reverse Root

JAVA PROBLEM - "Time Limit Exceeded"
Posted by Qambar Raza 31 Aug 2008 00:58
import java.io.*;
import java.util.*;

public class Main
{
       public static void main(String[] args) throws IOException
       {
          new Main().run();
       }

       StreamTokenizer in;
       PrintWriter out;

       void run() throws IOException
       {
          in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
          out = new PrintWriter(new OutputStreamWriter(System.out));
          solve();
          out.flush();
       }

       void solve() throws IOException
       {
          LinkedList<Double> myArr = new LinkedList<Double>();
          while(in.nextToken() != StreamTokenizer.TT_EOF)
          {
            myArr.add(Math.sqrt((double)(in.nval)));

          }
          myArr.add(0.0);
          ListIterator iterator = myArr.listIterator(myArr.size() - 1);
          while (iterator.hasPrevious())
      {
                out.printf("%.4f\n", iterator.previous());
      }
       }

}

I read the FAQS and other post but still cannot figure out whats the problem its working fine on my test cases which are as follows:

Description: First 4 are input numbers rest is output.

Test Case # 1:

2
5
3
5
2.2361
1.7321
2.2361
1.4142

Test Case # 2:

1427
0
876652098643267843
5276538
2297.0716
936297014.1164
0.0000
37.7757

Test Case # 3:

1 2 3 4
2.0000
1.7321
1.4142
1.0000

Test Case # 4:

2
            4
64
                                   49
7.0000
8.0000
2.0000
1.4142

Why am i getting the time limit exceed error ?
Re: JAVA PROBLEM - "Time Limit Exceeded"
Posted by Vladimir Yakovlev (USU) 1 Sep 2008 02:14
You can solve this problem on Java in two ways:

1. Use NumberFormat:

NumberFormat nf = NumberFormat.getInstance(Locale.US);
nf.setMaximumFractionDigits(6);
nf.setGroupingUsed(false);
out.println(nf.format(x));


You can get AC in 1.14 sec in such way

2. Use BigDecimal:

out.println(BigDecimal.valueOf(x).setScale(6, RoundingMode.HALF_UP));

You can get AC in 1.375 sec in such way