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);
}
} }

(JAVA保留小数问题,基础)Probability hdu2131的更多相关文章

  1. java 保留小数点后N位数(若干位),几种实现的方式总结

    import java.math.BigDecimal;import java.text.DecimalFormat;import java.text.NumberFormat;/** * java ...

  2. java保留小数-抄网上的

    摘抄别人的JAVA中保留小数点后若干位数的几种方法  2009-12-17 11:46:18|  分类: 编程小发现 |  标签: |举报 |字号大中小 订阅 第一种:java.text.Decima ...

  3. java实验三——求平均数,数组排序(有关java保留小数位数,由于编译器版本未到1.5导致的报错format函数第二个参数不对,要求是Object[])

    package hello; import java.util.Arrays; public class 实验三更正版 { public static void main(String[] args) ...

  4. 实验04——java保留小数的两种方法、字符串转数值

    package cn.tedu.demo; import java.text.DecimalFormat; /** * @author 赵瑞鑫 E-mail:1922250303@qq.com * @ ...

  5. java 保留小数点后指定位数四种方法

    1 package com.itheima_01; 2 3 import java.math.BigDecimal; 4 import java.text.DecimalFormat; 5 impor ...

  6. java保留小数后两位的四种写法

    package com.btzh.mis.house.utils; import java.math.BigDecimal;import java.math.RoundingMode;import j ...

  7. java保留小数点后位数以及输出反转数字

    //方法一double b = 8.0/3.0; //与C语言不同,此处8.0和8有所区分 String format = String.format("%.2f,b"); //表 ...

  8. 实用---java保留小数点后位数以及输出反转数字

    //方法一double b = 8.0/3.0; //与C语言不同,此处8.0和8有所区分 String format = String.format("%.2f,b"); //表 ...

  9. java保留小数位数

    System.out.println(String.format("%.5f",new Main().minRadius(n,m)));

随机推荐

  1. linux第三次读书笔记

    第七章:链接 一.编译器驱动程序 编译系统提供的调用预处理器.编译器.汇编器和链接器来构造目标文件的程序. 二.静态链接 三.目标文件 三种形式: 1.可重定位目标文件: 2.可执行目标文件: 3.共 ...

  2. AAPT2 error: check logs for details 问题的终究修复

    AAPT2 error: check logs for details Process 'command '***\build-tools\27.0.3\aapt.exe'' finished wit ...

  3. BUG管理工具——Mantis安装配置

    配置环境: CentOS6.5(所有操作在root用户下面操作) 1. 关闭防火墙, service iptables stop(防止防火墙捣乱,或者还得手动添加端口号的麻烦) 2. Disable ...

  4. 求两个整数的最大公约数GCM

    思路分析: (1)求差判定法:  如果两个数相差不大,可以用大数减去小数,所得的差与小数的最大公约数就是原来两个数的最大公约数.例如:求78和60的最大公约数.78-60=18,18和60的最大公约数 ...

  5. VSCODE安装以及使用Python运行调试代码的简单记录

    1. VScode安装 官网下载VSCODE https://code.visualstudio.com/ 下载呢windows的x64安装包,安装stable的版本 当前日期 2018.01.15 ...

  6. python之pygal:掷一个骰子统计次数并以直方图形式显示

    源码如下: # pygal包:生成可缩放的矢量图形文件,可自适应不同尺寸的屏幕显示 # 安装:python -m pip intall pygal-2.4.0-py2.py3-none-any.whl ...

  7. Python——socketsever模块

    1.作用:同时与多个客户端通信 import socketserver class MyServer(skcketserver.BaseRequestHandler): def handle(self ...

  8. fopen

    转自http://blog.sina.com.cn/s/blog_4b986f1a0101349k.html matlab中fopen函数在指定文件打开的实例如下: *1)“fopen”打开文件,赋予 ...

  9. Qt ------ 覆盖eventFilter(),捕获组件事件,事件处理

    在Qt中,当一个事件发生时(例如鼠标点击或某个键盘上的按键按下),其传递顺序如图所示.从这个图可以看出,事件过滤器首先获得事件,其次才是部件的 event 函数,最后是部件的事件处理函数 事件过滤器由 ...

  10. HDU-1686-KMP-水题

    纯KMP #include <cstdio> #include <algorithm> #include <cstring> #include <ctype. ...