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. 牛客多校第二场A run(基础DP)

    链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言2621 ...

  2. Linux内核分析:期中总结

    第一章:计算机是如何工作的 计算机大部分都是用冯诺依曼体系结构,即存储程序计算机. 两个层面: 1.硬件: cpu(IP寄存器:指针,指向内存的某块区域)——总线——内存(代码与数据) 2.程序员: ...

  3. [2017BUAA软工]第零次博客作业

    第一部分:结缘计算机 1. 你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢?(必答) 当年高考前在专业这件事上纠结了好久,因为我对于大学各个专业具体学什么都不甚了解,于是就迟迟没有明确的目 ...

  4. [2017BUAA软工]个人项目:数独

    一.项目地址 https://github.com/Slontia/Sudoku 附加作业(GUI):https://github.com/Slontia/SudokuGUI 二.开发时间 PSP2. ...

  5. SVN上传下载项目

    1 本机安装svn服务器 1.1    创建新库 创建分支, Trunk:主干, Tags:分支节点 Branches:分支 比如说我们现在tomcat出了6.0以后已经定版了,但是还得继续开发呀,开 ...

  6. Oracle 和 SQLSERVER 重新获取统计信息的方法

    1. Oracle 重新获取统计信息的命令 exec dbms_stats.gather_schema_stats(ownname =>) # 需要修改 ownername options 指定 ...

  7. Docker镜像加速设置

    地址:https://www.daocloud.io/mirror#accelerator-doc 配置 Docker 加速器 Linux MacOS Windows curl -sSL https: ...

  8. auto_increment 自增键的一些说明

    导致auto_increment变小的几种情况: 1. alter table xx auto_increment = yy; 2. truncate table 3. restart mysql 第 ...

  9. BZOJ4385[POI2015]Wilcze doły——单调队列+双指针

    题目描述 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段连续区间,使得该区间内所有数字之和不超过p. 输入 第一行包含三个整数n,p ...

  10. 自学Python5.2-类和对象概念

    自学Python之路 自学Python5.2-类和对象概念 面向对象编程的2个非常重要的概念:类和对象 对象是面向对象编程的核心: 在使用对象的过程中,为了将具有共同特征和行为的一组对象抽象定义,提出 ...