跳到正文

学习·文化课

8.16训练课

xiezirui1104约 17 分钟
原文
文章信息与版本

文章信息

发布
2026年8月16日
收录
2026年8月17日
更新
2026年11月14日
篇幅
约 17 分钟

版本

  1. v1当前
题目分数
T1100
T210
T370
T420
T540
T620
T776

T2 T2

无思路

题意:

一个5∗55 * 5的矩阵,要去找一个鞍点(就是为那一行的最大值,并且为那一列的最小值),输出这个鞍点在哪一行那一列,再输出这个鞍点的数,没有所谓的鞍点就输出”not found”

思路:

先用二维数组存5∗55 * 5的矩阵,再去找每一行的最大值,它是第几列,再判断一下那一列的最小值,如果最小值与最大值一样,就输出鞍点

错误代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[30][30];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int maxx = 0;
    int minn = LLONG_MAX;
    for(int i = 1 ; i <= 5 ; i++){
        for(int j = 1 ; j <= 5 ; j++){
            cin >> a[i][j];
            minn = min(minn , a[i][j]);
            maxx = max(maxx , a[j][i]);
        }
        
    }
    int f = 0;
    for(int i = 1 ; i <= 5 ; i++){
        for(int j = 1 ; j <= 5 ; j++){
            if(a[i][j] == maxx && a[i][j] == minn){
                f = 1;
                cout << a[i][j];
            }    
        }
    }
    if(f == 0){
        cout << "not found";
    }
	return 0;
}

正确代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[10][10];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int maxx = 0;
    for(int i = 1 ; i <= 5 ; i++){
        for(int j = 1 ; j <= 5 ; j++){
            cin >> a[i][j];    
        }
    }
    for(int h = 1 ; h <= 5 ; h++){
        int maxx = 0,lie = 0;//去寻找一下列
        for(int l = 1 ; l <= 5 ; l++){
            if(a[h][l] >= maxx){//打擂台,找这一行的最大值
                maxx = a[h][l];
                lie = l;//最大值的那一个列就是
            }
        }
        int minn = 1e6;
        for(int i = 1 ; i <= 5 ; i++){
            if(a[i][lie] < minn){//判断这一列的最小值
                minn = a[i][lie];
            }
        }
        if(maxx == minn){//如果为同一个数字
            cout << h << " " << lie << " "<< maxx;
            return 0;//题目保证,只有一个鞍点
        }
    }
    cout << "not found";//没有结束代码就输出
	return 0;
}

错因:

没有分开去找最大值的那一列,方法用的也是错误的

T3 T3

有思路

题意:

有nn行瓶子,每一行都有aia^i个瓶子,有qq次发球机会,每次需要判断在哪个位置可以打到mm个瓶子

思路:

用一个mapmap,因为正常的桶遍历会超时,因为可能会有151^5次发球机会,再加上一个一个去判断aia^i,最多也有151^5次循环,就会超时,所以要在存的时候发生一些改变,类似把mapmap当桶用,虽然是超级桶罢了

错误代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[100005];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        cin >> a[i];
    }
    int m;
    cin >> m;
    for(int j = 1 ; j <= m ; j++){
        int pz,f = 0;
        cin >> pz;
        for(int i = 1 ; i <= n ; i++){
            if(a[i] == pz){
                f = 1;
                cout << i << "\n";
            }    
        }
        if(f == 0){
            cout << 0 << "\n";
        }
    }
	return 0;
}
C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<int,int>a;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        int x;
        cin >> x;
        a[x] = i;//用瓶子个数当下标,存入瓶子是第几行
    }
    int l;
    cin >> l;
    for(int i = 1 ; i <= l ; i++){
        int m;
        cin >> m;
        cout << a[m] << "\n";//直接调取
    }
	return 0;
}

错因:

没有用mapmap啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊,就差一点,mapmap就差一点改过来了

T4 [蓝桥杯青少年组省赛 2024] 出现奇数次的数

有思路

题意:

很简单,有nn个数的数列,要去判断一下哪个数字出现了奇数次

思路:

直接用桶,当然超时且报错,因为数可能是一个191^9的数字,再去遍历好像不太现实,那怎么办?当然是用高贵的mapmap啦,用多少开多少的含金量

错误代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e8 + 5;
int tong[N];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        int x;
        cin >> x;
        tong[x]++;
        if(tong[x] % 2 != 0 && tong[i] != 1){
            cout << x;
            break;
        }
    }
	return 0;
}

正确代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<int,int>ma;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        int x;
        cin >> x;
        ma[x]++;//桶
    }
    for(auto i:ma){
        if(i.second % 2 == 1){//这个值是不是奇数
            cout << i.first;//输出下标,数字存的是下标
            return 0;
        }
    }
	return 0;
}

错因:

没有用mapmap,跟T3差不多

T5 [常州市赛 2020] 重点单词

有思路

题意:

有nn个单词,要压缩单词(有跟后面一样的就不保存这个字母),在统计一下哪个被压缩的单词出现的最多

思路:

只要这个单词不跟后一个单词一样,就保存这个字母,最后压缩过来的再用mapmap统计一下,最后看一下哪个单词出现最多

错误代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
string tong[100005];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        int ans = 0;
        string s;
        cin >> s;
        string t = "";
        for(int j = 0 ; j < s.size() ; j++){
            if(s[j] != s[j-1]){
                t += s[j];    
            }
        }
        cout << t << "\n";
    }
	return 0;
}

正确代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<string,int>ma;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n;
    string ans = "";
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        string s;
        cin >> s;
        string t = "";
        for(int j = 0 ; j < s.size() ; j++){
            if(s[j] != s[j+1]){//不跟后一个一样就保存
                t += s[j];    
            }
        }
        ma[t]++;
    }
    int maxx = 0;
     for(auto i:ma){
            if(i.second >= maxx){//打擂台
                maxx = i.second;
                ans = i.first;
            }
        }
    cout << ans;
	return 0;
}

错因:

没有用mapmap统计

T6 T6

无思路

题意:

有nn个评委,他们可以给选手打分,每次输入的是姓名和分数,最后按分数从低到高输出歌手的名字

思路:

一开始用的是结构体排序的方法,但是错了,所以用了双mapmap,输入后再用两个循环收尾

错误代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
struct st{
    string xm;
    int fs;
}a[1005];
int cmp(st s1,st s2){
    return s2.fs < s1.fs;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        cin >> a[i].xm >> a[i].fs;
    }
    sort(a + 1 , a + n + 1,cmp);
    for(int i = 1 ; i <= n ; i++){
        cout << a[i].xm << " ";                    
    }
	return 0;
}

正确代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<string,int>m1;//这个map存分数
map<int,string>m2;//这个map按分数存名字
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        string s;
        int x;
        cin >> s >> x;
        m1[s] += x;//这个名字下标的值加上分数
    }
    for(auto i:m1){
        m2[i.second]=i.first;//交换值
    }
    for(auto i:m2){
        cout << i.second << " ";
    }
	return 0;
}

错因:

没有去用双mapmap,结构体排序还不熟练,也不会去重

T7 A-B 数对

有思路

题意:

有nn个数,一个cc,要去判断数字能不能达到A−B=cA - B = c的情况

思路:

经过数学可知A−B=cA - B = c那么A−c=BA - c = B,AA就是这个数字,已知,cc也已知,只要把BB求出来再用mapmap统计一下就可以了

错误代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[200005];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n,c;
    cin >> n >> c;
    for(int i = 1 ; i <= n ; i++){
        cin >> a[i];
    }
    int cnt;
    for(int i = 1 ; i <= n ; i++){
        for(int j = i + 1 ; j <= n ; j++){
            if(a[i] - a[j] == c || a[j] - a[i] == c){
                cnt++;
            }
        }
    }
    cout << cnt;
	return 0;
}
C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<int,int>ma;
int a[200005];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n,c;
    cin >> n >> c;
    for(int i = 1 ; i <= n ; i++){
        cin >> a[i];
        int b = a[i] - c;//使用公式求出B
        ma[b]++;
    }
    int cnt = 0;
    for(int i = 1 ; i <= n ; i++){
        cnt += ma[a[i]];//假判断,如果这个没有结果,map会自动开0
    }
    cout << cnt; 
	return 0;
}

错因:

没有用mapmap,没有考虑到公式这一层面

拜拜!感谢观看!

原文