在 C++ 中转换秒为天、时、分和秒
在本教程中,我们将讨论一个将秒转换为天、时、分和秒的程序。
为此,我们将提供一个随机秒数。我们的任务是将其分别转换为适当的天数、小时数、分钟数和秒数。
示例
#include <bits/stdc++.h> using namespace std; //converting into proper format void convert_decimal(int n) { int day = n / (24 * 3600); n = n % (24 * 3600); int hour = n / 3600; n %= 3600; int minutes = n / 60 ; n %= 60; int seconds = n; cout << day << " " << "days " << hour << " " << "hours " << minutes << " " << "minutes " << seconds << " " << "seconds " << endl; } int main(){ int n = 126700; convert_decimal(n); return 0; }
输出
1 days 11 hours 11 minutes 40 seconds
广告