Use of iterative method seeking positive square root. x0 = a / 2;
Input formats:
Wherein, in the input line real number a (a> 0) and eps (eps> 0).
Output formats:
Output of the square, a 4 decimal places and the number of iterations.
Sample input:
Here we are given a set of inputs. E.g:
7.8 0.01
Sample output:
Given here corresponding output. E.g:
2.7949 3
Author: Li Zhicong
Unit: Harbin Normal University
Time limit: 400 ms
Memory Limit: 64 MB
Code length limit: 16 KB
1 import java.util.Scanner; 2 public class Main { 3 public static void main(String[] args) { 4 Scanner sc=new Scanner(System.in); 5 double a=sc.nextDouble(); 6 double eps=sc.nextDouble(); 7 int sum=1; 8 double temp=a/2,item; 9 item=0.5*temp+0.5*(a/temp); 10 while(Math.abs(item-temp)>=eps) { 11 temp=item; 12 item=0.5*temp+0.5*(a/temp); 13 sum++; 14 } 15 double k; 16 k=item*0.5+temp*0.5; 17 System.out.printf("%.4f %d",k,sum); 18 } 19 }
He initially did not write it. Refer to someone else's writing (To be completed