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. 12.11 Daily Scrum

      Today's Task Tomorrow's Task 丁辛 实现和菜谱相关的餐厅列表. 实现和菜谱相关的餐厅列表.             邓亚梅             美化搜索框UI. 美 ...

  2. Python学习笔记 -- 第六章 文件操作

    I/O编程 在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这 ...

  3. 第一个Sprint

    项目名字:四则运算APP 开发环境:java 团队名称:会飞的小鸟 团队成员:陈志棚  李炫宗   刘芮熔  徐侃  罗伟业 一.经过宿舍世纪讨论后我们剔除了一些不合理的设计,比如网站管理员这一部分在 ...

  4. tensorflow在windows下的安装

    1.python 的安装 这里我选择的是Anaconda4.2,附上下载链接https://www.continuum.io/downloads 2.测试python安装是否成功 在cmd中输入pyt ...

  5. IP工具类

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletReques ...

  6. [cnbeta]iPhone 2018年全球出货2.25亿部:中国区下滑两成

    iPhone 2018年全球出货2.25亿部:中国区下滑两成 2019年01月22日 20:12 501 次阅读 稿源:快科技 0 条评论   https://www.cnbeta.com/artic ...

  7. python 安装influxdb-python

    一.Linux下安装 1.yum install -y git 2.安装pip,参考:https://app.yinxiang.com/shard/s41/sh/0338ba85-5443-453f- ...

  8. ionic3.x开发小坑记录(一)

    自定义font的时候,在assets中创建的文件夹名字别用fonts,会与ionic默认样式冲突,在浏览器中调试是正常的,到手机上就出问题了. 在html中写img的src直接如图  assets前面 ...

  9. linux ubuntu nethogs安装与介绍

    安装nethogs: apt-get -y install ncurses* apt-get -y install libpcap-dev libncurses5-dev wget -c https: ...

  10. 研究VCL源码的原因和起点

    ---恢复内容开始--- 研究VCL源码的原因和起点 根本原因:当然是希望自己成为Delphi高手,因为这么多年过去,觉得自己始终不得要领,修改一个控件都无从下手,一直都只是个会拖控件的白痴.而我却拥 ...