(JAVA保留小數問題,基礎)Probability hdu2131
阿新 • • 發佈:2018-09-14
-s his lan ins after rec accep rst new
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
註:
此題是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