2.8

· · 个人记录

T1

这道题的唯一的难点就是知道每个月第一天是星期几,我们用两个数组分别存对应月的天数、对应月的第一天是星期几。然后特殊先输出不完整的一行空格--第一行,然后即可正常遍历输出。

#include<bits/stdc++.h>
using namespace std;
int dde[] = {0,3,6,6,2,4,7,2, 5,1,3,6,1};//第i个月第一天
int mm[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};//天数
int main()
{
    int m;
    cin >> m;
    cout << "MON TUE WED THU FRI SAT SUN" << endl;//必要
    int kaishiw = dde[m];//题目月的相关信息
    int kaishid = mm[m];   
    for (int i = 1; i < kaishiw; i++)//读入开头一段空格。 
    {
        cout <<"    ";
    }
    int cnt = kaishiw - 1;//接下来要正常输入的
    for (int i = 1; i <= kaishid; i++) 
    {
        cout << setw(3) << i << " ";
        cnt++;
        if (cnt == 7) {
            cout << endl;
            cnt = 0;//换行
        }
    }

    return 0;
}

T2

首先明确的点是肯定每次模拟一圈时要判断圈数的奇偶性,所以如何判断圈数呢?我们发现,每次一圈四周都会向内缩一圈从而到达第 i+1 圈,所以得出:圈数=min({i,j,n-i+1,n-j+1})也就是距离四周的最小值。

接着肯定是要找规律,看图:

1 (1,2) 2 (1,2) 3 (1,3) 4 (1,4)
5 (2,1) 6 (2,2) 7 (2,3) 8 (2,4)
9 (3,1) 10 (3,2) 11 (3,3) 12 (3,4)
13 (4,1) 14 (4,2) 15 (4,3) 16 (4,4)

如果以顺时针 90 度的方法旋转,那么 a[x][y]=a[n-y+1][x],如果以逆时针 90 度的方法旋转,a[x][y]=a[y][n-x+1]。接下来就可以遍历整个数组,判断 a[i][j] 在偶数圈还是奇数圈然后存入答案即可。

#include<bits/stdc++.h>
using namespace std;
int a[5000][5000];
int b[5000][5000];
int main()
{
    int n;
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            cin>>a[i][j];
        }
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            int minn=min({i,j,n-i+1,n-j+1});//圈数
            if(minn%2==1)//奇数,顺时针
            {
                b[i][j]=a[n-j+1][i];
            }
            else
            {
                b[i][j]=a[j][n-i+1];//逆时针
            }
        }
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            cout<<b[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
} 

T3

很明显,不可能完完整整把数组中的每一个数都填出来,所以只能针对题目给的 x,y。最基本肯定也是判断目标是从外到内在第几层,此处要判断在上下左右的哪条边上。

判断点在上下左右的哪条边上,我们先假设在第 x 层,不难想到判断条件如下。

$if(y<res)$ 为上边 $if(n-y+1 < res)$ 为在下边 默认为右面 然后就就可以计算答案了。

看注释~

if(op==0)  // 右边第res层的右侧列
{
    ans+=y-res+1;  // 偏移=当前列-层数+1
}
else if(op==1)  // 下边第res层的底行
{
    ans+=x-res+n-(res-1)*2;  // 偏移=行偏移 + 上边/右边的长
}
else if(op==2)  // 左边第res层的左侧列
{
    ans+=2*(n-(res-1)*2)-2+(n-res+1)-y+1;  // 偏移=上边+右边+下边的长度 + 列偏移
}
else  // op==3 上边第res层的顶部行
{
    ans+=3*(n-(res-1)*2)-2+n-res-x+1;  // 偏移=上边+右边+左边的长 + 行偏移
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
    int ans=0;
    int n,x,y;
    cin>>n>>x>>y;
    int op=0,res=x;
    if(n-x+1<res)
    {
        op=2;
        res=n-x+1;  
    }
    if(y<res)
    {
        op=3;
        res=y;
    }
    if(n-y+1<res)
    {
        op=1;
        res=n-y+1;
    }

    for(int i = 1, j=n;i<res;i++,j-=2)
    {
        ans+=2*(j+j-2);
    }
    if(op==0)
    {
        ans+=y-res+1;
    }
    else if(op==1)
    {
        ans+=x-res+n-(res-1)*2;
    }
    else if(op==2)
    {
        ans+=2*(n-(res-1)*2)-2+(n-res+1)-y+1;
    }
    else
    {
        ans+=3*(n-(res-1)*2)-2+n-res-x+1;
    }
    cout<<ans;
    return 0;
}