AOJ0080 Third Root
問題リンク Third Root
- 解法
3乗根を求めるための漸化式が載っているのでそれを実装します。
終了判定は q * 10^-5 未満ですが、ソースではq*10^-6になってるのは自分でもよくわかりません。
- ソース
import java.util.Scanner; //Third Root public class AOJ0080 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(true){ double q = sc.nextDouble(); if(q==-1)break; double x = Math.abs(q/2); while(Math.abs(x*x*x-q)>=0.000001*q)x = x-(x*x*x-q)/(3*x*x); System.out.printf("%.6f\n", x); } } }