首页 > 试题广场 >

编写一个程序,要求用户以整数方式输入 秒数(使用long 或

[问答题]
编写一个程序,要求用户以整数方式输入 秒数(使用long 或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:
Enter the number of seconds: 3160000
3160000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds

#include<iostream>
using namespace std;
const int DaytoHour(24);
const int HourtoMinute(60);
const int MinutetoSecond(60);
int main()
{
 long long second;
 int D,H,M,S;
 cout<<"Enter the number of seconds:";
 cin>>second;
 S=second;
 M=S/MinutetoSecond;
 H=M/HourtoMinute;
 D=H/DaytoHour;
 H=H%DaytoHour;
 M=M%HourtoMinute;
 S=S%MinutetoSecond;
 cout<<second<<" seconds = "<<D<<" days, "<<H<<" hours, "<<M<<" minutes, "<<S<<" seconds";
 return 0;
}

编辑于 2019-01-24 13:34:48 回复(1)
#include <iostream>
using namespace std;

int main() {
    long s;
    long days,hours,mins,seconds;
    cout <<"Enter then number of seconds :";
    cin>>s;

    days=s/86400;
    hours=(s-days*86400)/3600;
    mins=(s-days*86400-hours*60*60)/60;
    seconds=s%60;
    cout<<s<<" second ="<<days<<"days ,"<<hours<<"hours ,"<<mins<<"minutes ," <<seconds<<"seconds";
}
发表于 2023-01-11 21:55:34 回复(0)
#include<iostream>
const int SecToMin(60);
const int SecToHour(60*60);
const int SecToDay(60*60*24);
int main()
{
    using namespace std;
    long sec, minu, hour, day;
    cout << "Enter the number of seconds: ";
    cin >> sec;
    day = sec/SecToDay;
    hour = sec%SecToDay/SecToHour;
    minu = sec%SecToDay%SecToHour/SecToMin;
    cout <<sec<<" seconds = ";
    sec = sec%SecToDay%SecToHour%SecToMin;
    cout <<day<<" days, "<<hour<<" hours, "<<minu<<" minutes, "<<sec<<" seconds";
    return 0;
}

发表于 2021-01-24 00:23:36 回复(0)
#include <iostream>
using namespace std;
const int day_hour(24);
const int hour_min(60);
const int min_sec(60);
int main() {
    cout << "Enter the number of seconds:";
    long long seconds;
    cin >> seconds;
    int d, h, m, s;
    float a, b;
    a = min_sec * hour_min * day_hour;
    b = hour_min * day_hour;
    d = seconds / a;
    h = (seconds - d * a) / b;
    m = (seconds - d * a - h * b) / min_sec;
    s = seconds - d * a - h * b - m * min_sec;
    cout<<seconds<<" seconds = " << d<<" days, "<<h<<" hours, "<<m<<" minutes, "<<s<<" seconds"<<endl;
    return 0;
}

发表于 2019-07-11 14:53:02 回复(1)