将秒转换为天、分钟和秒

发布于 2024-08-24 05:50:55 字数 682 浏览 2 评论 0原文

我已经设置了将秒转换为天、分钟和秒格式的“挑战”。

例如:31600000 = 365 天 46 分钟 40 秒。

using namespace std;
const int hours_in_day = 24;
const int mins_in_hour = 60;
const int secs_to_min = 60;

long input_seconds;
cin >> input_seconds;

long seconds = input_seconds % secs_to_min;
long minutes = input_seconds / secs_to_min % mins_in_hour;
long days = input_seconds / secs_to_min / mins_in_hour / hours_in_day;

cout << input_seconds << " seconds = "
     << days << " days, "
     << minutes << " minutes, "
     << seconds << " seconds ";

return 0;

它有效并给出了正确的答案,但完成后我查看了其他人是如何解决这个问题的,他们的答案有所不同。我想知道我是否错过了什么。

I've been set the 'challenge' of converting seconds to format as the Days,Minutes and Seconds.

For example: 31600000 = 365 days, 46 minutes, 40 seconds.

using namespace std;
const int hours_in_day = 24;
const int mins_in_hour = 60;
const int secs_to_min = 60;

long input_seconds;
cin >> input_seconds;

long seconds = input_seconds % secs_to_min;
long minutes = input_seconds / secs_to_min % mins_in_hour;
long days = input_seconds / secs_to_min / mins_in_hour / hours_in_day;

cout << input_seconds << " seconds = "
     << days << " days, "
     << minutes << " minutes, "
     << seconds << " seconds ";

return 0;

It works and comes up with the correct answer but after completing it I looked at how other people had tackled it and theirs was different. I'm wondering If I'm missing something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梅倚清风 2024-08-31 05:50:55

在我看来,这是将秒转换为 DD/hh/mm/ss 的最简单方法:

#include <time.h>
#include <iostream>
using namespace std;    

time_t seconds(1641); // you have to convert your input_seconds into time_t
tm *p = gmtime(&seconds); // convert to broken down time

cout << "days = " << p->tm_yday << endl;
cout << "hours = " << p->tm_hour << endl;
cout << "minutes = " << p->tm_min  << endl;
cout << "seconds = " << p->tm_sec << endl;

this seems to me to be the easiest way to convert seconds into DD/hh/mm/ss:

#include <time.h>
#include <iostream>
using namespace std;    

time_t seconds(1641); // you have to convert your input_seconds into time_t
tm *p = gmtime(&seconds); // convert to broken down time

cout << "days = " << p->tm_yday << endl;
cout << "hours = " << p->tm_hour << endl;
cout << "minutes = " << p->tm_min  << endl;
cout << "seconds = " << p->tm_sec << endl;
妥活 2024-08-31 05:50:55

我认为这是斯蒂芬·普拉塔(Stephen Prata)书中的挑战。我是这样做的:

#include <iostream>

using namespace std;

int main()
{
    long input_seconds = 31600000;

    const int cseconds_in_day = 86400;
    const int cseconds_in_hour = 3600;
    const int cseconds_in_minute = 60;
    const int cseconds = 1;

    long long days = input_seconds / cseconds_in_day;
    long hours = (input_seconds % cseconds_in_day) / cseconds_in_hour;
    long minutes = ((input_seconds % cseconds_in_day) % cseconds_in_hour) / cseconds_in_minute;
    long seconds = (((input_seconds % cseconds_in_day) % cseconds_in_hour) % cseconds_in_minute) / cseconds;
    cout << input_seconds << " seconds is " << days << " days, " << hours << " hours, " << minutes << " minutes, and " << seconds << " seconds.";

    cin.get();
    return 0;
}

I think is the challenge from Stephen Prata's book. I did it as follows:

#include <iostream>

using namespace std;

int main()
{
    long input_seconds = 31600000;

    const int cseconds_in_day = 86400;
    const int cseconds_in_hour = 3600;
    const int cseconds_in_minute = 60;
    const int cseconds = 1;

    long long days = input_seconds / cseconds_in_day;
    long hours = (input_seconds % cseconds_in_day) / cseconds_in_hour;
    long minutes = ((input_seconds % cseconds_in_day) % cseconds_in_hour) / cseconds_in_minute;
    long seconds = (((input_seconds % cseconds_in_day) % cseconds_in_hour) % cseconds_in_minute) / cseconds;
    cout << input_seconds << " seconds is " << days << " days, " << hours << " hours, " << minutes << " minutes, and " << seconds << " seconds.";

    cin.get();
    return 0;
}
倾城月光淡如水﹏ 2024-08-31 05:50:55

关于编程的事情之一是,做某事从来没有只有一种方法。事实上,如果我下定决心,我也许能够想出十几种完全不同的方法来实现这一目标。如果您的代码满足要求,您就不会遗漏任何内容。

为了您的娱乐,这里有一种在 Windows 下格式化小时:分钟:秒的方法(elapsed 是一个双精度值,表示自...某事以来经过的秒数)

sprintf_s<bufSize>(buf, "%01.0f:%02.0f:%02.2f", floor(elapsed/3600.0), floor(fmod(elapsed,3600.0)/60.0), fmod(elapsed,60.0));

One of the things about programming is that there is never just one way to do something. In fact if I were to set my mind to it, I might be able to come up with a dozen completely different ways to accomplish this. You're not missing anything if your code meets requirements.

For your amusement, here's a way to format up hours:minutes:seconds under Windows (elapsed is a double & represents number of seconds elapsed since... something)

sprintf_s<bufSize>(buf, "%01.0f:%02.0f:%02.2f", floor(elapsed/3600.0), floor(fmod(elapsed,3600.0)/60.0), fmod(elapsed,60.0));
走走停停 2024-08-31 05:50:55

例如:31600000 = 365 天 46 分 40 秒。

真的吗?

$ bc
365*24*60*60 + 46*60 + 40
31538800

365*24*60*60 + 1066*60 + 40
31600000

您的意思是“将输入转换为天、小时、分钟和秒,然后丢弃小时”还是“将输入转换为天、一天内的总分钟(即可以超过 60)和秒”?

在第二种情况下,我认为你应该将几分钟的指令替换为

long minutes = input_seconds / secs_to_min % (mins_in_hour * hours_in_day);

For example: 31600000 = 365 days, 46 minutes, 40 seconds.

Really?

$ bc
365*24*60*60 + 46*60 + 40
31538800

365*24*60*60 + 1066*60 + 40
31600000

Did you mean "convert the input into days, hours, minutes and seconds, and then discard the hours" or "convert the input into days, total minutes within a day (i.e. can be more than 60), and seconds"?

In the second case I think you should replace the instruction for minutes with

long minutes = input_seconds / secs_to_min % (mins_in_hour * hours_in_day);
[浮城] 2024-08-31 05:50:55

将秒转换为天、小时、分钟和秒;它运行完美,无需消耗太多资源:

#include <iostream>
using namespace std;

int main()
{

    const int Seconds_Per_Day = 86400;
    const short Seconds_Per_Hour = 3600;
    const short Seconds_Per_Minute = 60;
    int Input_Seconds;

    cout << "Please, enter seconds: \n"; 
    cin >> Input_Seconds;

    short Number_Of_Days = floor(Input_Seconds / Seconds_Per_Day);
    int Reminder_1 = Input_Seconds % Seconds_Per_Day;
    short Number_Of_Hours = floor(Reminder_1 / Seconds_Per_Hour);
    short Reminder_2 = Reminder_1 % Seconds_Per_Hour;
    short Number_Of_Minutes = floor(Reminder_2 / Seconds_Per_Minutes);
    short Number_Of_Seconds = Reminder_2 % Seconds_Per_Minutes;

    cout << "The number of days is " << Number_Of_Days << ", of hours is " << 
    Number_Of_Hours << ", of minutes is " << Number_Of_Minutes << ", and of 
    seconds is " << Number_Of_Seconds << endl;

    return 0;
}

Convert seconds to days, hours, minutes, and seconds; it works perfectly without consuming many resources:

#include <iostream>
using namespace std;

int main()
{

    const int Seconds_Per_Day = 86400;
    const short Seconds_Per_Hour = 3600;
    const short Seconds_Per_Minute = 60;
    int Input_Seconds;

    cout << "Please, enter seconds: \n"; 
    cin >> Input_Seconds;

    short Number_Of_Days = floor(Input_Seconds / Seconds_Per_Day);
    int Reminder_1 = Input_Seconds % Seconds_Per_Day;
    short Number_Of_Hours = floor(Reminder_1 / Seconds_Per_Hour);
    short Reminder_2 = Reminder_1 % Seconds_Per_Hour;
    short Number_Of_Minutes = floor(Reminder_2 / Seconds_Per_Minutes);
    short Number_Of_Seconds = Reminder_2 % Seconds_Per_Minutes;

    cout << "The number of days is " << Number_Of_Days << ", of hours is " << 
    Number_Of_Hours << ", of minutes is " << Number_Of_Minutes << ", and of 
    seconds is " << Number_Of_Seconds << endl;

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文