Balloon Comes! hdu(小数位数处理)

Balloon Comes!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5402 Accepted Submission(s): 1722  

Problem Description
The contest starts now! How excited it is to see balloons floating around. You, one of the best programmers in HDU, can get a very beautiful balloon if only you have solved the very very very... easy problem.
Give you an operator (+,-,*, / --denoting addition, subtraction, multiplication, division respectively) and two positive integers, your task is to output the result.
Is it very easy?
Come on, guy! PLMM will send you a beautiful Balloon right now!
Good Luck!

Input
Input contains multiple test cases. The first line of the input is a single integer T (0<T<1000) which is the number of test cases. T test cases follow. Each test case contains a char C (+,-,*, /) and two integers A and B(0<A,B<10000).Of course, we all know that A and B are operands and C is an operator. 

Output
For each case, print the operation result. The result should be rounded to 2 decimal places If and only if it is not an integer.

Sample Input
4
+ 1 2
- 1 2
* 1 2
/ 1 2 

Sample Output
3
-1
2
0.50 
package ACM1;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Scanner;

public class hdu1 {
    public static void main(String[]args)
    {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        for(int i=0;i<n;i++)
        {   

            String a = scanner.next();
            int b = scanner.nextInt();
            int c = scanner.nextInt();
            //double result = (double)b/(double)c;
            if(a.charAt(0)==‘+‘)
                System.out.println((b+c));
            else if(a.charAt(0)==‘-‘)
                System.out.println((b-c));
            else if(a.charAt(0)==‘*‘)
                System.out.println((b*c));
            else
                {
                    if(b%c==0)
                        System.out.println((b/c));
                    else
                    {
                        DecimalFormat dc = new DecimalFormat("0.00");
                        //dc.setMaximumFractionDigits(2);
                        //dc.setGroupingSize(0);
                        //dc.setRoundingMode(RoundingMode.FLOOR);
                        //System.out.println(dc.format(b/(1.0*c)));//我不明白为什么这个样子不对
                        System.out.format("%.2f",(b/(1.0*c)) ).println();//为什么这个样子对

                    }

                }

            }

        }

    }
时间: 2024-07-29 22:53:39

Balloon Comes! hdu(小数位数处理)的相关文章

在JS中,将text框中数据格式化,根据不同的小数位数,格式化成对应的XXX,XXX,XXX.XX(2位小数) 或者XXX,XXX,XXX(0位小数)

//在JS中,将text框中数据格式化,根据不同的小数位数,格式化成对应的XXX,XXX,XXX.XX(2位小数) 或者XXX,XXX,XXX(0位小数) function formatNum(num, n) {//参数说明:num 要格式化的数字 n 保留小数位 num = String(num.toFixed(n)); var re = /(-?\d+)(\d{3})/; while (re.test(num)) num = num.replace(re, "$1,$2") ret

转,Oracle中关于处理小数点位数的几个函数,取小数位数,Oracle查询函数

关于处理小数点位数的几个oracle函数() 1. 取四舍五入的几位小数 select round(1.2345, 3) from dual; 结果:1.235 2. 保留两位小数,只舍 select trunc(1.2345, 2) from dual; 结果:1.23 select trunc(1.2399, 2) from dual; 结果:1.23 3.取整数 返回大于或等于x的最大整数: SQL> select ceil(23.33) from dual; 结果: 24 返回等于或小于

Java学习-047-数值格式化及小数位数四舍五入

此小工具类主要用于数值四舍五入.数值格式化输出,很简单,若想深入研究,敬请自行查阅 BigDecimal 或 DecimalFormat 的 API,BigDecimal.setScale(位数,四舍五入法)中四舍五入法有如下 7 种: 1. ROUND_UP:远离零方向舍入.向绝对值最大的方向舍入,只要舍弃位非0即进位. 2. ROUND_DOWN:趋向零方向舍入.向绝对值最小的方向输入,所有的位都要舍弃,不存在进位情况. 3. ROUND_CEILING:向正无穷方向舍入.向正最大方向靠拢.

js数据保留小数位数

1000.12345.toFixed(2)=1000.12: 1000.12987.toFixed(2)=1000.13: 1000.1.toFixed(2)=1000.10 js数据保留小数位数,布布扣,bubuko.com

JS中保留小数位数

一.1.2.toFixed(2) 浮点数进行四舍五入设置指定小数位的数值 二.2.toFixed(2) 整数尝试,但是在JS中会被认为是对象名,以至于无法使用 三."1.2".toFixed(2) 由于toFixed方法是针对Number类型的,所以string类型的即使能够转换成数字,也不能使用这个方法 四.var a=1;a.toFixed(2); 构造整数对象,那么就可以调用了 http://www.w3school.com.cn/jsref/jsref_tofixed.asp

ADF_12c_保留小数位数问题

Jdeveloper (Version 12.1.2)默认显示三位小数,有小数精度要求时,给出如下解答. 进入该VO,找到有小数位数需求的列: 属性 UI Hint       format type:Number       format:###0.000000 如上是保留六位小数的情况,其他可类推.

三种方法保留小数位数

js代码部分为 var PI=3.1415192;var str=PI+""; //因为数字没法进行字符操作,所以需要先转换:var index=str.indexOf(".");//返回当前的字符的点的位置console.log(index); //1console.log(str.substr(0,index+3)); //3.14 //合并后的第一种写法console.log(str.substr(0,str.indexOf(".")+3)

EL表达式取整数或者取固定小数位数的简单实现

EL表达式取整数或者取固定小数位数的简单实现 例如${8/7} ,${6/7} ,${12/7 } 在页面的显示结果分别为: 1.1428571428571428 0.8571428571428571 1.7142857142857142 现在我的目的是只取前面的整数,此时我用的是EL表达式的<fmt:formatNumber />标签,例如: <fmt:formatNumber type="number" value="${8/7)}" maxF

android edittext 限制输入框小数位数

先看下XML布局文件 <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/tv_count" android:background="@null" android:hint="@

C# decimal保留指定的小数位数,不四舍五入

decimal保留指定位数小数的时候,.NET自带的方法都是四舍五入的. 项目中遇到分摊金额的情况,最后一条的金额=总金额-已经分摊金额的和. 这样可能导致最后一条分摊的时候是负数,所以自己写了一个保留指定位数小数的方法. 扩展方法的使用,使得调用起来很优雅. 1 public static class DecimalExtension 2 { 3 /// <summary> 4 /// decimal保留指定位数小数 5 /// </summary> 6 /// <para