solution.cpp 1.2 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
#include <vector>
using namespace std;

int a[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int b[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 闰年
int days = 0;                                                 //共多少天
int rows = 0;                                                 //共多少行
bool f(int n)                                                 // n是不是闰年
{
    if ((n % 4 == 0 && n % 100 != 0) || n % 100 == 0)
        return true;
    else
        return false;
}

void print(int t[]) //打印那一年
{
    for (int i = 0; i < 12; i++)
    {
        for (int j = 1; j <= t[i]; j++)
        {
            cout << j << "  ";
            days++;
            if (days % 7 == 0) //  7天一礼拜
            {
                cout << endl;
                rows++;
            }
        }
    }
}
int main()
{
    int start = 1901;
    int end = 2000;
    for (int i = start; i <= end; i++)
    {
        if (f(i) == true) //是闰年,按b数组打印
            print(b);
        else //不是,按a数组打印
            print(a);
    }
    cout << "rows is " << endl;
    cout << rows << endl;
    return 0;
}