修改密码

【2024课程】零基础学会C++编程课程

成品课程

陈远龙老师主讲 & 答疑

课程题单 - T1000

未购买 · 可先试学25节课

课程目录展开/折叠

第34课 数组(刷题课)

播放快捷键

播放/暂停:空格(或鼠标单击)      全屏:F(或鼠标双击)      退出全屏:Esc

快进10 / 30 / 60秒:方向键→ / Ctrl + 方向键→ / Shift + 方向键→

快退10 / 30 / 60秒:方向键← / Ctrl + 方向键← / Shift + 方向键←

本节课讲解配套PPT&板书:

本节课讲解到的源代码

源代码下载:第34课 数组(刷题课)-源代码下载

1. C1055-蛇形数组-1
#include <bits/stdc++.h>
using namespace std;

int a[55][55];
int n;

// 过程 思维 
int main()
{
    cin >> n;
    int x = 1, y = n; // x行号,y列号 起始坐标
    int cnt = 1; // 计数用 
    a[x][y] = cnt;

    // cnt : 1 - n * n 
    // cnt < n * n  [cnt <= n * n 错误的] 
    while (cnt != n * n) // 什么时候停止 cnt == n * n ,继续循环 
    {
        // 1. 向下 每次尝试向下走 x + 1
        while (x + 1 <= n && a[x + 1][y] == 0)
        {
            x ++; // y不变
            cnt ++;
            a[x][y] = cnt;
        }

        // 2. 向左
        while (y - 1 >= 1 && a[x][y - 1] == 0)
        {
            y --;
            cnt ++;
            a[x][y] = cnt;
        }

        // 3. 向上 

        while (x - 1 >= 1 && a[x - 1][y] == 0)
        {
            x --;
            cnt ++;
            a[x][y] = cnt;
        }

        // 4. 向右  
        while (y + 1 <= n && a[x][y + 1] == 0)
        {
            y ++;
            cnt ++;
            a[x][y] = cnt;
        }
    }

    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
        {
            cout << setw(5) << left << a[i][j];
            // printf("%-5d", a[i][j]);
        }
        cout << endl;
    }

    return 0;
}
2. C1055-蛇形数组-2
#include <bits/stdc++.h>
using namespace std;

int a[55][55];
int n;

// 过程 思维 
int main()
{
    cin >> n;
    int x = 1, y = n; // x行号,y列号 起始坐标
    int cnt = 1; // 计数用 
    a[x][y] = cnt;

    // cnt : 1 - n * n 
    // cnt < n * n  [cnt <= n * n 错误的] 
    while (cnt != n * n) // 什么时候停止 cnt == n * n ,继续循环 
    {
        // 1. 向下 每次尝试向下走 x + 1
        while (x + 1 <= n && a[x + 1][y] == 0)
        {
            a[++x][y] = ++cnt;
            // a[x++][y] = cnt++; 错误的 
        }

        // 2. 向左
        while (y - 1 >= 1 && a[x][y - 1] == 0) a[x][--y] = ++cnt;

        // 3. 向上 
        while (x - 1 >= 1 && a[x - 1][y] == 0) a[--x][y] = ++cnt;

        // 4. 向右  
        while (y + 1 <= n && a[x][y + 1] == 0) a[x][++y] = ++cnt;
    }

    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= n; j ++)
        {
            cout << setw(5) << left << a[i][j];
            // printf("%-5d", a[i][j]);
        }
        cout << endl;
    }

    return 0;
}
3. C1069-一年中的第几天-1
#include <bits/stdc++.h>
using namespace std;

// a[1] 1月份的天数 
int a[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
    int y, m, d;
    cin >> y >> m >> d;

    int ans = 0; // answer
    // 1 - (m - 1)月份的天数累加 
    // i 枚举的是月份 
    for (int i = 1; i < m; i ++)
    {
        ans += a[i];
    }
    ans += d;

    if (m >= 3)
    {
        if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) ans ++;
    }

    cout << ans;

    return 0;
}
4. C1051-最大子段和-1
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
int a[N];
int n;

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++)
    {
        cin >> a[i];
    }

    int t = 0; // 当前遍历的点前一个点(为终点)的最大字段和 

    // 遍历数组 
    int ans = INT_MIN;
    for (int i = 0; i < n; i ++)
    {
        if (t >= 0) t = a[i] + t;
        else t = a[i];
        ans = max(ans, t);
        // cout << t << ' ';
    }

    cout << ans;

    return 0;
}

本节课答疑

建议大家有问题先通过AI答疑(比如:DeepSeek 等),AI时代需要学会使用AI辅助学习

陈远龙老师视频讲解:如何使用DeepSeek进行答疑?

通过AI未能获得满意解答的,可以联系陈远龙老师答疑

目录