-1

当我输入 36011 时,它会导致第二个输出是 36011 而不是 11,我确保编译器设置正确设置。我真的不知道还能做什么,我必须使用 long long int 来完成这项任务,当我尝试在我的代码中使用 mod 函数时,它对我不起作用。

#include <iostream>

using namespace std;

int main()
{
long long int seconds;

cin >> seconds;

long long int minutes = seconds / 60;
long long int hours = minutes / 60;
long long int days = hours / 24;

cout << "Total seconds: " << seconds << endl;

if(seconds <= 0)
{
    cout << "\nTotal seconds must be greater than zero";
}

else
{
    if(days > 0)
    {
        hours %= 24;
        cout << "\n" << days << " day(s)";
    }
    if(hours > 0)
    {
        minutes %= 60;
        cout << "\n" << hours << " hour(s)";
    }
    if(minutes > 0)
    {
        seconds %= 60;
        cout << "\n" << minutes << " minute(s)";
    }
    if(seconds > 0)
    {
        cout << "\n" << seconds << " second(s)";
    }
}
cout << endl;

return 0;}
4

1 回答 1

0

你以错误的方式计算你的价值观。尝试更多类似的东西:

#include <iostream>

using namespace std;

int main()
{
    long long int seconds;
    cin >> seconds;

    if (seconds <= 0)
    {
        cout << "Total seconds must be greater than zero" << endl;
    }
    else
    {
        cout << "Total seconds: " << seconds << endl;

        long long int minutes = seconds / 60; seconds %= 60;
        long long int hours = minutes / 60; minutes %= 60;
        long long int days = hours / 24; hours %= 24;

        /* alternatively:
        long long int days = seconds / 86400; seconds %= 86400;
        long long int hours = seconds / 3600; seconds %= 3600;
        long long int minutes = seconds / 60; seconds %= 60;
        */

        if (days > 0) {
            cout << days << " day(s)" << endl;
        }
        if (hours > 0) {
            cout << hours << " hour(s)" << endl;
        }
        if (minutes > 0) {
            cout << minutes << " minute(s)" << endl;
        }
        if (seconds > 0) {
            cout << seconds << " second(s)" << endl;
        }
    }

    return 0;
}
于 2017-10-09T00:27:41.293 回答