修改密码

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

成品课程

陈远龙老师主讲 & 答疑

课程题单 - T1000

未购买 · 可先试学25节课

课程目录展开/折叠

第33课 数组(刷题课)

播放快捷键

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

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

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

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

本节课讲解到的源代码

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

1. P1118-出现次数最多的字符-1
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e6 + 5;
char c[MAXN]; // 存储字符的数组 
int n;

int cnt[128]; // 统计数组,统计每个字符出现的次数 

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

    // 枚举遍历 'A' - 'Z' 'a' - 'z' 
    // 'A'(65) - 'z'(122)
    for (int i = 'A'; i <= 'z'; i ++)
    {
        for (int j = 0; j < n; j ++)
        {
            if (i == c[j]) cnt[i] ++;
        }
    }

    // 找出现最多的次数,在统计数组查找 
    int mx = 0;
    for (int i = 0; i < 128; i ++)
    {
        // if (cnt[i] > mx) mx = cnt[i];
        mx = max(cnt[i], mx);
    }

    // 找到出现次数最多的那些字符
    for (int i = 0; i < 128; i ++)
    {
        if (cnt[i] == mx)
        {
            cout << char(i) << " - " << cnt[i] << endl;
        }
    } 

    return 0;
}
2. P1118-出现次数最多的字符-2
#include <bits/stdc++.h>
using namespace std;

int n;
int cnt[128]; // 统计数组,统计每个字符出现的次数 

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

    // 找最大值
    int mx = 0;
    for (int i = 0; i < 128; i ++)
    {
        if (cnt[i] > mx) mx = cnt[i];
    }

    // 输出
    for (int i = 0; i < 128; i ++)
    {
        if (cnt[i] == mx)
        {
            cout << char(i) << " - " << cnt[i] << endl;
        }
    } 

    return 0;
}
3. P1111-杨辉三角-1
#include <bits/stdc++.h>
using namespace std;

int a[35][35]; // 杨辉三角二维数组 

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

    for (int i = 1; i <= n; i ++)
    {
        // 规律1:第1列和对角线元素都是1
        a[i][1] = 1;
        a[i][i] = 1;
        // 规律2:其他元素 = 正上元素 + 左上元素
        for (int j = 2; j < i; j ++)
        {
            a[i][j] = a[i - 1][j] + a[i - 1][j - 1];    
        } 
    }

    // 打印杨辉三角
    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= i; j ++) // i行打印i列 
        {
            cout << a[i][j] << ' ';
        }
        cout << endl;
    } 

    return 0;
}
4. P1111-杨辉三角-2
#include <bits/stdc++.h>
using namespace std;

int a[35][35]; // 杨辉三角二维数组 

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

    for (int i = 1; i <= n; i ++)
    {
        // 规律1:第1列和对角线元素都是1
        a[i][1] = 1;
        a[i][i] = 1;
        cout << a[i][1] << ' ';

        // 规律2:其他元素 = 正上元素 + 左上元素
        for (int j = 2; j < i; j ++)
        {
            a[i][j] = a[i - 1][j] + a[i - 1][j - 1]; 
            cout << a[i][j] << ' ';   
        } 
        if (i != 1) cout << a[i][i];

        cout << endl;
    }

    return 0;
}
5. P1111-杨辉三角-3
#include <bits/stdc++.h>
using namespace std;

int a[35][35];

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

    a[0][0] = 1; // 每个元素遵循统一的规则:当前元素 = 正上元素 + 左上元素,可以设置一个初始状态
    // 递推实现
    for (int i = 1; i <= n; i ++)
    {
        for (int j = 1; j <= i; j ++)
        {
            a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
            cout << a[i][j] << ' ';
        }
        cout << endl;
    }

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

int a[105][105];

int main()
{
    int x, y;
    // ...
    int cnt = 1;
    while (cnt < n * n ?)
    {
        // 向下走
        while (x + 1 <= n && a[x + 1][y] == 0)
        {
            x++;
            cnt++;
            a[x][y] = cnt;

            // a[++x][y] = ++cnt;
        }

        // 向左走
        while ()
        {
        }

        // 向上走
        while ()
        {

        }

        // 向右走 
        while ()
        {

        } 

    }

    return 0;
}

本节课答疑

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

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

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

目录