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. ABP框架用Dapper实现通过SQL访问数据库

    ABP的框架(2) - 访问数据库   为了防止不提供原网址的转载,特在这里加上原文链接:http://www.cnblogs.com/skabyy/p/7517397.html 本篇我们实现数据库的 ...

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

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

  3. logback基本入门

    1. logback的定义 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分为下面下个模块: logback-core:其它 ...

  4. PAT 1031 查验身份证

    https://pintia.cn/problem-sets/994805260223102976/problems/994805290334011392 一个合法的身份证号码由17位地区.日期编号和 ...

  5. ASP.NET MVC4学习笔记

    一.MVC简介

  6. [转帖]Marvell兵败中国4G 创始人去职未来几何

    Marvell兵败中国4G 创始人去职未来几何 (2016-04-12 09:08:30) 2016年的帖子. http://blog.sina.com.cn/s/blog_1542ef86c0102 ...

  7. ubuntu ftp服务

    apt-get install vsftpd apt-get update vi /etc/apt/sources.list vim  /etc/vsftpd.conf service vsftpd ...

  8. linux系统下find命令的使用

    1.find /* -name erlang 当前目录下,查找名为erlang的文件和目录 find /* -name rabbitmq-server 当前目录下,查找名为 rabbitmq-serv ...

  9. Omni(USDT)钱包安装(ubuntu)

    一.下载Omni Layer钱包 wget https://bintray.com/artifact/download/omni/OmniBinaries/omnicore-0.3.0-x86_64- ...

  10. MySQL基本概念以及简单操作

    一.MySQL   MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MyS ...