mod函数对我不起作用

时间:2017-10-08 23:12:00

标签: c++ mod unsigned-long-long-int

当我输入36011时,它导致第二个(s)输出为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;}

1 个答案:

答案 0 :(得分: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;
}