2 条题解

  • 0
    @ 2019-03-31 13:23:18
    #include <iostream>
    using namespace std;
    class DateTime
    {
        int year, month, day, hour, minute, second;
    public:
        void init(int y, int mon, int d, int h, int min, int s)
        {
            year = y;
            month = mon;
            day = d;
            hour = h;
            minute = min;
            second = s;
        }
        int isLeapYear()
        {
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                return 1;
            else
                return 0;
        }
        void add(int num)
        {
            second = second + num;
            if (second >= 60)
            {
                minute += second / 60;
                second = second % 60;
            }
    
            if (minute >= 60)
            {
                hour += minute / 60;
                minute = minute % 60;
            }
            if (hour >= 24)
            {
                day += hour / 24;
                hour = hour % 24;
            }
            int Month[13] = { 0,31,28 + isLeapYear(),31,30,31,30,31,31,30,31,30,31 };
            while (day > Month[month])
            {
                day -= Month[month];
                month++;
                if (month > 12)
                {
                    month -= 12;
                    year++;
                    Month[2] = 28 + isLeapYear();
                }
            }
        }
        void print()
        {
            cout << year << " " << month << " " << day << " " << hour << " " << minute << " " << second;
        }
    };
    int main()
    {
        int y, mon, d, h, min, s;
        cin >> y >> mon >> d >> h >> min >> s;
        int num;
        cin >> num;
        DateTime t;
        t.init(y, mon, d, h, min, s);
        t.add(num);
        t.print();
        return 0;
    }
    
  • -2
    @ 2021-01-29 15:05:16
    //这个题思路和1-3一致 时分秒的进率是固定的所以没有比较特别的点
    #include<iostream>
    using namespace std;
    int main()
    {
        int month_day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//利用下标的便利之处记录每月天数上限^^b
        int year,month,day,shi,fen,miao;
        int mum;
        cin>>year>>month>>day>>shi>>fen>>miao>>mum;
        if((year%4==0&&year%100!=0)||(year%400==0))//开局先判断闰年
            month_day[2]=29;//修改二月上限
        for(int i=1;i<=mum;i++)//我一秒一秒加好做一点(  ̄︶ ̄)
        {
            miao++;//每次加一秒后判断是否要进位
            if(miao==60)
            {
                miao=0;
                fen++;
            }
            if(fen==60)
            {
                fen=0;
                shi++;
            }
            if(shi==24)
            {
                shi=0;
                day++;
            }
            if(day>month_day[month])
            { 
                day=1;
                month++;
            }
            if(month>12)
            {
                month=1;
                year++;
                if((year%4==0&&year%100!=0)||(year%400==0))//年份改变时要再判断二月上限是否改变
                    month_day[2]=29;
                else
                    month_day[2]=28;
                
            }
        }
        cout<<year<<" "<<month<<" "<<day<<" "<<shi<<" "<<fen<<" "<<miao;
        
        return 0;
    
    
  • 1

信息

ID
1007
难度
3
分类
(无)
标签
递交数
313
已通过
147
通过率
47%
被复制
4
上传者