CaptainHammer_java数字计算,求精度提高方法

来源:互联网 发布:广州市软件行业协会 编辑:程序博客网 时间:2024/05/18 02:30

原题要求在本文后面。

要求 计算  A =  arcsin( g*D/(v*v) )/2 。没错就是初中物理,还理解错的话,还是简单的。

g是重力加速度,D是距离,v是初速度,A 是待求的角度。

谁对java数字计算熟悉的指点下,下文代码精度不够  (给的三个case能满足,就是一些3988个case的输入有些精度上达不到要求,也不知道问题在哪里)。

代码如下:

 

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.HashMap;import java.util.Map;public class CaptainHammer {private Map<Integer,String> data = new HashMap<Integer,String>();private String inputFileString="";private String outputFileString="";private Map<Integer,Double> outPutMap = new HashMap<Integer,Double>();private final Double G =(Double) 9.8;CaptainHammer(String inputFileString, String outputFileString){this.inputFileString =inputFileString;this.outputFileString =outputFileString;}private void Read() throws IOException{try{BufferedReader tBufferedReader= new BufferedReader(new FileReader(inputFileString) );String sTempOneLine=  tBufferedReader.readLine();Integer num = Integer.parseInt(sTempOneLine);for(int i=1; i<= num ;i++){sTempOneLine = new String( tBufferedReader.readLine() );data.put(i, sTempOneLine);}tBufferedReader.close();}catch(FileNotFoundException  e){e.printStackTrace();throw new FileNotFoundException();}catch (IOException io){io.printStackTrace();throw new IOException();}}private void ComPut(){for(Map.Entry<Integer, String> mapEy: data.entrySet()){Integer index = mapEy.getKey();String[] strs = mapEy.getValue().split(" ");Integer tempV=Integer.parseInt(strs[0] );Integer tempD=Integer.parseInt(strs[1] );Double tempNum = Math.asin( G* tempD /tempV /tempV ) *90 /Math.PI;outPutMap.put(index, tempNum);}}private void outPutFile() throws IOException{try{BufferedWriter bw = new BufferedWriter(new FileWriter(outputFileString));for(Map.Entry<Integer, Double> mapEy:outPutMap.entrySet()){String str = "Case #"+ String.valueOf(mapEy.getKey())+": ";String str2= String.format("%.7f", mapEy.getValue());str += str2+"\n";bw.write(str);}bw.close();}catch(IOException ioe){ioe.printStackTrace();throw new IOException(" write wrong!");}}public static void main(String[] args) throws Exception {String inputFileString = "B";StringoutputFileString = "B.out";CaptainHammer bh= new CaptainHammer(inputFileString,outputFileString);bh.Read();bh.ComPut();bh.outPutFile();System.out.println("OK!");}}


 

 

原题如下:

Problem

The Hamjet is a true marvel of aircraft engineering. It is a jet airplane with a single engine so powerful that it burns all of its fuel instantly during takeoff. The Hamjet doesn't have any wings because who needs them when the fuselage is made of a special Wonderflonium isotope that makes it impervious to harm.

Piloting the Hamjet is a not a job for your typical, meek-bodied superhero. That's why the Hamjet belongs to Captain Hammer, who is himself impervious to harm. The G-forces that the pilot endures when taking a trip in the Hamjet are legen-dary.

The Hamjet takes off at an angle of θ degrees up and a speed of V meters per second. V is a fixed value that is determined by the awesome power of the Hamjet engine and the capacity of its fuel tank. The destination is D meters away. Your job is to program the Hamjet's computer to calculate θ given V and D.

Fortunately, the Hamjet's Wondeflonium hull is impervious to air friction. Even more fortunately, the Hamjet doesn't fly too far or too high, so you can assume that the Earth is flat, and that the acceleration due to gravity is a constant 9.8 m/s2 down.

Input

The first line of the input gives the number of test cases, T. T lines follow. Each line will contain two positive integers -- V and D.

Output

For each test case, output one line containing "Case #x: θ", where x is the case number (starting from 1) and θ is in degrees up from the the horizontal. If there are several possible answers, output the smallest positive one.

An answer will be considered correct if it is within 10-6 of the exact answer, in absolute or relative error. See the FAQ for an explanation of what that means, and what formats of floating-point numbers we accept.

Limits

1 ≤ T ≤ 4500;
 1 ≤ V ≤ 300;
 1 ≤ D ≤ 10000;
 It is guaranteed that each test case will be solvable.

Sample

Input
  
Output
  
 3
 98 980
 98 490
 299 1234    Case #1: 45.0000000
 Case #2: 15.0000000
 Case #3: 3.8870928

 

原创粉丝点击