FZU 1856 The Troop (JAVA高精度)

本文探讨了Java中对象、类和构造函数的基本概念,并通过具体案例展示了它们在解决问题中的应用。通过实例分析,读者可以更深入地理解Java面向对象编程的核心思想。
Problem 1856 The Troop

Accept: 72    Submit: 245
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

The troop is advancing. The length of the queue is x meter. The commander is at the front of the queue, riding his horse. The crier is at the end of the queue. If something happen, the crier should talk to the commander. Now, the crier run to the commander. Then run back. We know the troop has advanced x meter. So what is the number of meter the crier run. You can assume the speed of the crier is bigger than the troop.

Input

There are multiple test case, in every case, there will be a real number x. which means the distance the troop advances. 0 < x < 10^1000

Output

For each test case you should print the answer. Use the print format as below. Print a blank line after each case. Because of the Accuracy error, If answer >= 100000,you should just output the front five digit. Else round to the four digits after the decimal point.

Sample Input

100 100000

Sample Output

Case 1 241.4214 Case 2 24142
思路:直接根据样例看出这是1+根号2.
收获:JAVA 对象,类, 构造函数。 http://blog.youkuaiyun.com/liujun13579/article/details/8155223
import java.math.*;
import java.util.*;
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        BigDecimal x, cell = BigDecimal.ONE;
        cell = cell.add(new BigDecimal(Math.sqrt(2.0)));
        String str;
        int  t = 1;
        while(in.hasNext()) {
            x = in.nextBigDecimal();
            x = x.multiply(cell);
            str = x.toString();
            System.out.println("Case "+ t);
            t++;
            if(x.compareTo(BigDecimal.valueOf(100000.0)) >= 0)
                System.out.println(str.substring(0, 5));
            else{
                x = x.divide(BigDecimal.ONE, 4, RoundingMode.HALF_UP);
                System.out.println(x);
            }
            System.out.println();
        }

    }

}

 

转载于:https://www.cnblogs.com/ZP-Better/p/4723481.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值