1026 program running time (15 minutes)

1026 program running time (15 minutes)
To obtain the running time of a C language program, the common method is to call the header file time.h, which provides the clock () function, which can capture the time spent from the beginning of the program to the time when clock () is called. This time unit is clock tick, or "clock dot". There is also a constant CLK_TCK, which gives the number of clock ticks taken by the machine clock per second. Therefore, in order to obtain the running time of a function f, we only need to call clock () before calling f to obtain a clock count C1; After the f execution is completed, call clock() to obtain another clock count C2; The difference (C2-C1) between the clock beats obtained twice is the clock beats consumed by F operation, divided by the constant CLK_TCK, you get the running time in seconds.

Here we might as well simply assume the constant CLK_TCK is 100. Given the clock count obtained by the tested function before and after, please give the running time of the tested function.

Input format:
The input gives two integers C1 and C2 in order in one line. Note that the clock points obtained twice must be different, that is, C1 < C2, and the value is [0,10 ^ 7].

Output format:
Output the running time of the function under test in one line. The running time must be output in hh:mm:ss (i.e. 2-bit hour: minute: Second) format; Times less than 1 second are rounded to seconds.

Input example:
123 4577973
No blank lines at the end
Output example:
12:42:59
No blank lines at the end

/*
To obtain the running time of a C language program, the common method is to call the header file time.h,
The clock() function is provided to capture the time spent from the beginning of the program to the time when clock() is called. The unit is clock tick.
There is also a constant CLK_TCK, which gives the number of clock ticks taken by the machine clock per second.
In order to obtain the running time of a function f, just call clock() before calling f to obtain a clock count C1;
After the f execution is completed, call clock() to obtain another clock count C2;
The difference (C2-C1) between the clock beats obtained twice is the clock beats consumed by f operation, divided by the constant CLK_TCK,
You get the running time in seconds.

Assumed constant CLK_TCK is 100.
Given the clock count obtained by the tested function before and after, please give the running time of the tested function.

Input format: two integers C1 and C2 are given in sequence in one line. C1 < C2, and the value is [0,10 ^ 7].

Output format: the running time must be output in hh:mm:ss (i.e. 2-bit hour: minute: Second) format; Times less than 1 second are rounded to seconds.
*/

#include<stdio.h>
#include<time.h>

#define CLK_TCK 100

void main()
{
    int C1,C2;  //Clock count
    int d,C;  //d = C1 - C2
    float c;    //Used to determine rounding
    int min;    //min is the number of minutes not processed
    int ss=0,mm=0,hh=0;   //Final hours, minutes and seconds, initial value is 0
    int ss2=0;
    scanf("%d %d",&C1,&C2);
    d = C2 - C1;
    c = (float)d/CLK_TCK;
    C = d/CLK_TCK;
//    printf("c:%f\n",c);
//    printf("C:%d\n",C);
//    printf("c-C:%f\n",c-C);
    if(c-C >= 0.5)
        C = d/CLK_TCK+1;    //Consider rounding
    min = C/60;
//    printf("min:%d\n",min);
    ss = C%60;
    ss2 = C - (min * 60);
//    printf("ss:%d,ss2:%d\n",ss,ss2);
    hh = min/60;
    mm = min%60;
    if(hh<10&&mm<10&&ss<10)
        printf("0%d:0%d:0%d",hh,mm,ss);
    else if(mm<10&&ss<10)
        printf("%d:0%d:0%d",hh,mm,ss);
    else if(hh<10&&ss<10)
        printf("0%d:%d:0%d",hh,mm,ss);
    else if(hh<10&&mm<10)
        printf("0%d:0%d:%d",hh,mm,ss);
    else if(hh<10)
        printf("0%d:%d:%d",hh,mm,ss);
    else if(mm<10)
        printf("%d:0%d:%d",hh,mm,ss);
    else if(ss<10)
        printf("%d:%d:0%d",hh,mm,ss);
    else
        printf("%d:%d:%d",hh,mm,ss);
}

This problem is not difficult. Directly call the clock() function twice to obtain the difference of clock points d, C = d/CLK_TCK, minutes min = d/60, the remainder is seconds ss, to be rounded, hours hh = min/60, the remainder is minutes mm
The only thing that needs to be considered is the rounding of seconds. Here, the difference between the actual decimal point value and the direct cut-off value is compared. If the difference > = 0.5, it needs to be rounded, and the cut-off value + 1, otherwise it can be cut off directly.

Tags: C PAT

Posted on Wed, 22 Sep 2021 13:13:50 -0400 by e1seix