十三号星期五

 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
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    int a[7] = {0};//记录星期出现次数
    int week = 1;
    for (int y = 1900; y < 1900 + n ; y++)
    {
        for (int m = 1; m <= 12; m++)
        {
            int d;
            if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)//根据年与月算该月有多少天
            {
                d = 31;
            }
            else if (m == 2)
            {
                if ((y % 100 != 0 && y % 4 == 0) || (y % 400 == 0))
                    d = 29;
                else d = 28;
            }
            else
                d = 30;
            for (int i = 1; i <= d; i++)
            {
                week++;
                if (i == 13)//如果是 13 号,对应的星期出现次数加 1 
                    a[week % 7] ++;

            }
        }
    }
    for (int i = 0; i < 7; i++) cout << a[i] << " ";
    cin >> n;
}

蛇形矩阵

 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
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int maxn = 150;
int a[maxn][maxn];

int main() 
{
    int n, m;
    cin >> n >> m;

    memset(a, 0, sizeof(a));
    int x = 0, y = 0;       //初始坐标坐标,(0,0) 
    int cnt = 1;            //初始化第一个数 
    a[x][y] = cnt;

    while (cnt < n * m ) 
    {   
        //用下一笔的位置来判断
        //向右, 符合条件,则填入下一笔。____提前预判  
        while (y + 1 < m && !a[x][y + 1]) a[x][ ++ y] = ++ cnt;
        //向下 
        while (x + 1 < n && !a[x + 1][y]) a[ ++ x][y] = ++ cnt;
        //向左
        while (y - 1 >= 0 && !a[x][y - 1]) a[x][ -- y] = ++ cnt;
        //向上
        while (x - 1 >= 0 && !a[x - 1][y]) a[ -- x][y] = ++ cnt;    
    }
    for (int i = 0; i < n; i ++ ) 
    {
        for (int j = 0; j < m; j ++ )
        {
            cout << a[i][j] << " ";
            // printf("%-5d", a[i][j]);
        }   
        cout << endl;
    } 

    return 0;
}