|
|
back to boardWhy wrong answer? Java Posted by Arderun 19 Apr 2016 19:42 Sample numbers have different value of math.sqrt() in last digit. Why? This task is not so big level for hidden problem. sample result: 2297.0716 936297014.1164 0.0000 37.7757 my result: 2297.0715 936297024.0000 0.0000 37.7757 import java.io.PrintWriter; import java.util.Scanner; import java.util.*; public class AlgorithmTAsk { public static void main(String[] arg){ Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); ArrayList<Float> f = new ArrayList<Float>(); while (in.hasNextLong()) { f.add((float) Math.sqrt(in.nextLong())); } for(int i = f.size()-1;i>=0;i--){ out.printf("%8.6f\n",f.get(i)); } out.flush(); } } Edited by author 19.04.2016 19:45 Re: Why wrong answer? Java Try double instead of float Help me out here import java.util.Scanner; import java.text.DecimalFormat; import java.lang.StringBuffer; import java.lang.Math; public class Timus2{ public static void main(String[] args){ Scanner S = new Scanner(System.in); DecimalFormat df = new DecimalFormat("#.0000"); StringBuffer result = new StringBuffer(); while(S.hasNext()){ result.append(df.format(Math.sqrt(S.nextDouble())) + "\n"); } S.close(); System.out.println(result); } } Re: Help me out here 1) Any advantages of using "StringBuffer result"? Is it really faster then just print result line by line? 2) Show here expected output, your program output, compare. |
|
|