Timus Online Judge 1001. Reverse Root

本文介绍了一种使用Java处理大数并计算其平方根的方法。通过读取一系列整数,利用BigDecimal进行精确计算,并按指定格式输出结果。

Input

The input stream contains a set of integer numbers Ai (0 ≤ Ai ≤ 1018). The numbers are separated by any number of spaces and line breaks. A size of the input stream does not exceed 256 KB.

Output

For each number Ai from the last one till the first one you should output its square root. Each square root should be printed in a separate line with at least four digits after decimal point.

Sample

inputoutput
 1427  0   

   876652098643267843 
5276538
  
   
2297.0716
936297014.1164
0.0000
37.7757

译文:

输入:

输入一个整数数组,这个数组中每个整数的取值范围为0<= Ai <=10的18次方。这些数字被一些空格和换行符分隔。输入的内存限制为256KB。

输出:

倒序输出数组的平方根。每个平方根结果保留最后四位小数。

==========================

第一次code:

 1 import java.math.BigDecimal;
 2 import java.math.RoundingMode;
 3 import java.util.Scanner;
 4 
 5 public class Main
 6 { 
 7     public static void main(String[] args) 
 8     { 
 9         Scanner input = new Scanner(System.in);
10         long[]a = new long[1000000000];
11         int i;
12         for (i=0; input.hasNext(); i++)  
13         {
14              a[i] = input.nextLong();
15         } 
16         for(i--;i>-1;i--)
17         {
18                         /**
19                         *   Math.sqrt()  :  对数据求平方
20                         *   BigDecimal适用于大型数据计算,精确度高
21                         */
22             BigDecimal   b   =   new   BigDecimal(Math.sqrt(a[i]));  
23             System.out.println(b.setScale(4,RoundingMode.HALF_UP).toString());
24         }
25     }
26 }
27     

 -------------------------------------我是万恶的分割线---------------------------------------------------------------------------------------------------------

/*

题目稍微修改一下,首先输入一个数,定义数组大小,然后倒序输出该数组的平方根。

*/

 1 import java.math.BigDecimal;
 2 import java.math.RoundingMode;
 3 import java.util.Scanner;
 4 
 5 public class Main
 6 { 
 7     public static void main(String[] args) 
 8     { 
 9         Scanner input = new Scanner(System.in);
10         int n =input.nextInt();
11         run();
12     }
13     public static void run()
14     {
15         Scanner input = new Scanner(System.in);
16         long[]a = new long[n];
17         int i;
18         for (i=0; i<n; i++)  
19         {
20              a[i] = input.nextLong();
21         } 
22         for(i--;i>-1;i--)
23         {
24             BigDecimal   b   =   new   BigDecimal(Math.sqrt(a[i]));  
25             System.out.println(b.setScale(4,RoundingMode.HALF_UP).toString());
26         }
27     }
28 }

 

posted on 2016-08-29 11:20 niithub 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/niithub/p/5817304.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值