百炼OJ1004
来源:互联网 发布:模拟退火算法应用实例 编辑:程序博客网 时间:2024/04/30 04:23
Larry今年毕业并找到了一份工作。他赚很多钱,但似乎总是不够。Larry认为他需要控制他的投资以解决自己的财务问题。Larry拿到了自己的银行账户详单,想看看自己有多少钱。请帮助Larry写一个程序,通过过去12个月中每月的月末结余,计算平均结余。
100.00489.1212454.121234.10823.05109.205.271542.25839.1883.991295.011.75
$1581.42这个破题卡了我半天,一直是WrongAnswer。网上找了几个java的提交上去也都是WrongAnswer。看了oj的提交记录,用java的也多会出现WrongAnswer。
首先来看看我最初的代码:
- import java.text.DecimalFormat;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- float r = 0;
- int i = 0;
- while (i < 12) {
- r = r + in.nextFloat();
- i++;
- }
- r = r / 12;
- DecimalFormat df = new DecimalFormat(".00");
- String s = df.format(r);
- System.out.println("$" + s);
- }
- }
那么错误在哪里呢?我想到了边界测试。有如下两个测试用例
case 1:
input:
10000.00
10000.00
10000.00
10000.00
10000.00
10000.00
10000.00
10000.00
10000.00
10000.00
10000.00
10000.00 output:
$10000.00
case 2:
input:
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
output:
$.01
显然case2出现了问题。问题在于数据格式。当整数部分为0时会被舍去(注意红色部分)。为此修改如下:
- import java.text.DecimalFormat;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- float r = 0;
- int i = 0;
- while (i < 12) {
- r = r + in.nextFloat();
- i++;
- }
- r = r / 12;
- DecimalFormat df = new DecimalFormat("0.00");
- System.out.println("$" + df.format(r));
- }
- }
0 0
- 百炼OJ1004
- oj1004
- 东秦oj1004
- 北大OJ1004
- 百炼
- 九度oj1004
- [Java]杭电oj1004
- 九度OJ1004 Median
- 九度oj1004
- 九度oj1004题
- 杭电oj1004
- 百炼采药问题....
- 百炼 1833:排列
- 百炼-2726:采药
- poj百炼 2788
- 【LIS】百炼-2757
- 百炼OJ1001
- 百炼OJ1002
- 我的第一个struts 例子
- 批量配置SSH 免密钥登录脚本
- MyBatis动态SQL之 set 和 trim标记的使用
- Android进程/线程通信方式
- JPush推送
- 百炼OJ1004
- 淘宝CDN架构全解析
- IOS 用 xcode 设置 开机启动页
- Qt程序移植到ARM板上去掉鼠标样子
- java设计模式之单例篇
- elasticsearch的IK分词插件安装
- Linux使用shell脚本调用sendmail发送带附件的邮件
- Concurrency之Introduce
- Android菜鸟进阶之路二-抽屉式导航