zoukankan      html  css  js  c++  java
  • (JAVA保留小数问题,基础)Probability hdu2131

    Probability

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2131

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 8731    Accepted Submission(s): 4228

    Problem Description
    Mickey is interested in probability recently. One day , he played a game which is about probability with mini.First mickey gives a letter and a word to mini.Then mini calculate the probability that the letter appears in the word.For example,give you the letter "a" and the word "apple". the probability of this case is 0.20000.
     
    Input
    The input contains several test cases. Each test case consists of a letter and a word.The length of the word is no longer than 200.
     
    Output
    For each test case, print the probability rounded to five digits after the decimal point.
     
    Sample Input
    a apple
    c Candy
    a banana
     
    Sample Output
    0.20000
    0.20000
    0.50000

     注:

    此题是java水题,注意要int与double进行转换。

    import java.math.BigDecimal;
    import java.util.Scanner;
    
    import javax.swing.plaf.basic.BasicArrowButton;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner inScanner = new Scanner(System.in);
            while(inScanner.hasNext()) {
                String string = inScanner.next();
                String string2 = inScanner.next();
                string = string.toLowerCase();
                string2 = string2.toLowerCase();
                char c = string.charAt(0);
                int number = 0;
                BigDecimal bigDecimal = BigDecimal.valueOf(0);
                for(int i = 0;i<string2.length();i++) {
                    if(string2.charAt(i) == c) {
                        number ++;
                    }
                }
                double number1 = number/1.0;  //int和double的转换。
                double d = number1/string2.length();
                bigDecimal = bigDecimal.add(BigDecimal.valueOf(d).setScale(5,BigDecimal.ROUND_HALF_UP)); //保留5位小数。
                System.out.println(bigDecimal);
            }
        }
    
    }
  • 相关阅读:
    bzoj:1299: [LLH邀请赛]巧克力棒
    [51nod][cf468D]1558 树中的配对
    HDU5447 Good Numbers
    Timus Online Judge:ural:1006. Square Frames
    poj1830:开关问题
    bzoj:1776: [Usaco2010 Hol]cowpol 奶牛政坛
    bzoj:1725: [Usaco2006 Nov]Corn Fields牧场的安排
    bzoj:1828: [Usaco2010 Mar]balloc 农场分配
    bzoj:1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
    bzoj:1598: [Usaco2008 Mar]牛跑步
  • 原文地址:https://www.cnblogs.com/Weixu-Liu/p/9648221.html
Copyright © 2011-2022 走看看