简体   繁体   中英

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 "::". 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. Thus, if seconds is 0, you should return "0:0:0", while if seconds is 3661, you should return "1:1:1"

My Algorithm:

Here is how my algorithm is supposed to work for the input 3661:

  1. 3661/3600 = 1.016944 -> This means the number of hours is 1
  2. Subtract the total number of hours elapsed ie 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
  4. Subtract the total number of minutes elapsed ie 1.01666-1=0.01666. Multiply this with 60. This would yield the number of seconds elapsed.

The output produced however is 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). I tried to use the Math.ceil() function to round up the value and it produces a correct output. However I can only score about 60/250 points when I submit my code (TopCoder SRM 144 Div2) . 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 :

  1. 3661/3600 = 1.016944 -> This means the number of hours is 1
  2. Subtract the number of hours * 3600 - ie 3661-(1*3600) = 61
  3. 61/60 = 1.0166666 -> The number of minutes elapsed is equal to 1
  4. Subtract the number of minutes * 60 ie 61-(1*60)=1. This yields the number of seconds elapsed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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