跳到正文

个人记录

26.8.16

luluowo约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年8月16日
收录
2026年8月17日
更新
2026年11月8日
篇幅
约 12 分钟
获赞
1

版本

  1. v1当前

声明

代码仅供参考,请勿抄袭!

题目信息

题目名称难度思路(没有/有一点/有)分数
T806457 T2入门有0
T806459 T4普及−有0
T806462 T6入门有80
T806463 T7普及−有0

题目详细

T2

  • 题意

    输入一个 5*5 的矩阵,每行只有一个最大值,每列只有一个最小值,要求寻找这个矩阵的鞍点,鞍点:矩阵中的一个元素,它是所在行的最大值,并且是所在列的最小值。输出这个鞍点的行,列和值,如果没有,输出“not found”。

  • 思路

    输入5*5的矩阵,得到每行的最大值和每列的最小值,判断最大值是否等于最小值,如果是,则输出、return 0;否则继续循环,最后输出“not found”。

  • 错误代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[6][6];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    for (int i = 1; i <= 5; i++){
        for (int j = 1; j <= 5; j++){
            cin >> a[i][j];
        }
    }
    int n = 0, p =0, c = 0;
    int q = 0, w = 0, e = 0;
    for (int i = 1; i <= 5; i++){
        int maxn = 0;            
        int minn = 100000;
        for (int j = 1; j <= 5; j++){
            if (a[0][j] <= minn){
                minn = a[i][j];
                n = i;
                p = j;
                c = a[0][j];
            }
            if (a[i][j] >= maxn){
                minn = a[i][j];
                q = i;
                w = j;
                e = a[0][j];
            }
        }
    }
    if (q == n&&p == w){
        cout << q << " " << p << " " << e;
    }
    else{
        cout << "not found";
    }
	return 0;
}
  • AC代码(记得看注释)
C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[6][6];//输入的二维数组
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(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 = INT_MAX;//本行的最小值
        for (int i = 1; i <= 5; i++){
            if (a[i][lie] <= minn){
                minn = a[i][lie];//更新最小值
            }
        }
        if (minn == maxx){
            cout << h << " " << lie << " " << maxx;//如果是“鞍点”,则输出,结束
            return 0;
        }
    }
    cout << "not found";//最后输出
	return 0;
}
  • 错因

    一开始看到题目想到用6个变量,但其实不用,只用三个就行

  • 备注

    最后要记得输出“not found”哦

T4

  • 题意

    输入n个整数,要求找出出现了奇数次的数,输出这个数。

  • 思路

    输入n,每次输入a,a出现的次数+1,最后for循环遍历map,判断(如果a出现的次数%2==1),如果满足,则输出a。

  • 错误代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e8+5;
int a[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 m;
        cin >> m;
        a[m]++;
    }
    for (int i = 1; i <= 2e8; i++){
        if (a[i] % 2 == 1){
            cout << i;
            return 0;
        }
    }
	return 0;
}
  • AC代码(记得看注释)
C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<int, int>tong;//用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++){
        int a;
        cin >> a;//输入
        tong[a]++;//将桶的第a个加一
    }
    for (auto i:tong){//for循环遍历map,记住要用auto
        if (i.second % 2 == 1){//判断次数是否为奇数
            cout << i.first;//如果是,则输出它的值
            return 0;//直接结束程序
        }
    }
	return 0;
}
  • 错因

    用数组计数,导致RE

  • 备注

    记得直接return 0,因为题目中说只有一个目标数值。

T6

  • 题意

    有n名观众作为评委,第i位评委可以为他喜欢的歌手打一次分a[i],最后将分数从低到高排序,输出对应的歌手名。

  • 思路

    输入n和所有的a[i],存入map,第一个map(map1)存数据,第二个map(map2)存歌手的名字

  • 错误代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
struct st{
    string s;
    int t = 0;
}a[100005];
int cmp(st a1, st b1){
    return a1.t < b1.t;
}
map<string, int>q;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n;
    cin >> n;
    int p;
    for (int i = 1; i <= n; i++){
        cin >> a[i].s >> p;
        a[i].t += p;
        q[a[i].s]++;
    }
    sort(a+1, a+n+1, cmp);
    for (int i = 1; i <= n; i++){
        if (q[a[i].s] == 1){
            cout << a[i].s << " ";
        }
        else{
            q[a[i].s]--;
        }
    }
	return 0;
}
  • AC代码(记得看注释)
C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<string, int>m1;//存数据,当桶
map<int, string>m2;//存歌手名字
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 b;
        string a;
        cin >> a >> b;
        m1[a] += b;//将桶的第a位歌手加上分数b
    }
    for (auto i : m1){//for循环遍历map1
        m2[i.second] = i.first;//将map2的这个值设为map1的下标(歌手名)
    }
    for (auto i : m2){//for循环遍历map2
        cout << i.second << " ";//输出歌手名
    }
	return 0;
}


  • 错因

    当初没想到可以用map,想用结构体

  • 备注

    题目储备量不够,要多刷题

T7

  • 题意

    输入正整数C,N列以及一串正整数,要求计算出所有满足A − B = C的数对的个数。

  • 思路

    for循环遍历,输入a[i],将map[a[i]-c]++,定义最终答案ans,for循环到n,每次ans加上map[a[i]]。

  • 错误代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[10000005];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    int n;
    cin >> n;
    int b;
    cin >> b;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    int sum = 0;
    for (int i = 1; i <= n; i++){
        if (a[i] - a[i - 1] == b){
            sum++;
        } 
    }
    cout << sum;
	return 0;
}
  • AC代码(记得看注释)
C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int a[10000005];
map<int, int>ma;//拿map当桶
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,用来存a[i]-c
        ma[b]++;//将map的第b个加一,表示这一个出现了一次
    }
    int ans = 0;//最终答案
    for(int i = 1; i <= n; i++){
        ans += ma[a[i]];//最终答案加上map[a[i]](a)
    }
    cout << ans;//输出
	return 0;
}
  • 错因

    题目储备量不够,要多刷题

  • 备注

    a:记得加上的是map[a[i]], 不是map[i],不然测试点会哭(WA)

原文
存档评论1 条抓取于