将秒转换为天,分和秒

编程入门 行业动态 更新时间:2024-10-10 02:19:00
本文介绍了将秒转换为天,分和秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嘿大家。我继续学习C ++,我已经设置了将秒转换为格式为天,分和秒的挑战。

例如:31600000 =

使用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,<<分钟< minutes,<<秒< seconds; return 0;

它的工作原理和正确答案,但完成后我看看如何其他人解决它和他们是不同的。

解决方案

div>

编程的一个原因是,从不只是一种方法来做某事。事实上,如果我把我的想法,我可能能够提出十多种完全不同的方式来完成这一点。

对于你的娱乐,这里有一种方法来格式化小时:分钟:秒Windows下( elapsed 是一个double&表示从...开始经过的秒数)。

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)

Hey everyone. I've continuing to learn C++ and 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.

Thanks, Dan.

解决方案

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));

更多推荐

将秒转换为天,分和秒

本文发布于:2023-11-28 19:25:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分和   为天

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!