简体   繁体   English

精度损失-Java

[英]Loss of precision - Java

Problem Statement: 问题陈述:

Write a method whatTime, which takes an int, seconds, representing the number of seconds since midnight on some day, and returns a String formatted as "::". 编写方法whatTime,该方法需要一个整数秒(代表某天午夜以来的秒数),并返回一个格式为“ ::”的String。 Here, represents the number of complete hours since midnight, represents the number of complete minutes since the last complete hour ended, and represents the number of seconds since the last complete minute ended. 此处,代表自午夜以来的完整小时数,代表自上一个完整小时结束以来的完整分钟数,并代表自上一个完整分钟结束以来的秒数。 Each of , , and should be an integer, with no extra leading 0's. ,,和中的每个都应为整数,且没有多余的前导0。 Thus, if seconds is 0, you should return "0:0:0", while if seconds is 3661, you should return "1:1:1" 因此,如果秒为0,则应返回“ 0:0:0”,而如果秒为3661,则应返回“ 1:1:1”

My Algorithm: 我的算法:

Here is how my algorithm is supposed to work for the input 3661: 这是我的算法应该对输入3661起作用的方式:

  1. 3661/3600 = 1.016944 -> This means the number of hours is 1 3661/3600 = 1.016944->这意味着小时数是1
  2. Subtract the total number of hours elapsed ie 1.016944-1=0.016944 减去经过的总小时数,即1.016944-1 = 0.016944
  3. Multiply this with 60 ie 0.016944*60=1.016666 -> The number of minutes elapsed is equal to 1 将此乘以60即0.016944 * 60 = 1.016666->经过的分钟数等于1
  4. Subtract the total number of minutes elapsed ie 1.01666-1=0.01666. 减去经过的总分钟数,即1.01666-1 = 0.01666。 Multiply this with 60. This would yield the number of seconds elapsed. 将此乘以60。这将产生经过的秒数。

The output produced however is 1:1:0. 但是,产生的输出是1:1:0。 I tried to use a print statement and it appears that the value of 'answer3' variable is 0.999 and that is why prints the integer part (0). 我尝试使用打印语句,看来'answer3'变量的值为0.999,这就是为什么打印整数部分(0)的原因。 I tried to use the Math.ceil() function to round up the value and it produces a correct output. 我试图使用Math.ceil()函数将值取整,并产生正确的输出。 However I can only score about 60/250 points when I submit my code (TopCoder SRM 144 Div2) . 但是,提交代码(TopCoder SRM 144 Div2)时,我只能得分约60/250分。 Any insight for improving the algorithm will be helpful. 对于改进算法的任何见解都会有所帮助。

public class Time
{
public String whatTime(int seconds)
    {
        double answer1,answer2,answer3; int H;

        answer1=(double)seconds/3600;

        H=(int)answer1;

        answer2=(double)((answer1-H)*60);

        int M=(int)answer2;

        answer3=answer2-M;

        answer3=(double)answer3*60;
        int S=(int)answer3;

        String answer=Integer.toString(H);

        answer=Integer.toString(H)+":"+Integer.toString(M)+":"+Integer.toString(S);

        return answer;

    }
}

避免使用浮点值,并完全使用int或long。

public String whatTime(int seconds) {

    int secondVal = seconds % 60;
    int minutes = seconds / 60;
    int minuteVal = minutes % 60;
    int hours = minutes / 60;
    int hourVal = hours % 24;
    int daysVal = hours / 24;

    String answer = "" + daysVal + ":" + hourVal + ":" + minuteVal + ":" + secondVal;

    return answer;
}

Could do the formatting more elegantly, but that's the basic idea. 可以更优雅地进行格式化,但这是基本思想。

You could solve this by working with ints : 您可以通过使用ints来解决此问题:

  1. 3661/3600 = 1.016944 -> This means the number of hours is 1 3661/3600 = 1.016944->这意味着小时数是1
  2. Subtract the number of hours * 3600 - ie 3661-(1*3600) = 61 减去小时数* 3600-即3661-(1 * 3600)= 61
  3. 61/60 = 1.0166666 -> The number of minutes elapsed is equal to 1 61/60 = 1.0166666->经过的分钟数等于1
  4. Subtract the number of minutes * 60 ie 61-(1*60)=1. 减去分钟数* 60,即61-(1 * 60)= 1。 This yields the number of seconds elapsed. 这样得出经过的秒数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM