跳到正文

个人记录

8.16错题总结

LJY15387562778约 10 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

T806456 T1

知识点:循环+数位分离

题目分析:如果正整数K的所有数位中恰好包含3个2,则称K为”美丽数”,求出两个数L和R之间美丽数的数量。

思路:用循环遍历L到R,在循环内把当前的数i进行数位分离,再判断是否恰好拥有3个2,最后输出。

AC代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    //输入L和R
	int l,r;
    cin>>l>>r;
    //答案
    int d=0;
    // 1.循环遍历
    for(int i=l;i<=r;i++){
        int ans=0;
        int k=i;
        // 2.数位分离
        while(k>0){
            if(k%10==2) ans++;
            k/=10;
        }
        // 3.判断
        if(ans==3) d++;
    }
    // 4.输出
    cout<<d;
	return 0;
}

错误代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int l,r;
    cin>>l>>r;
    int d=0;
    for(int i=l;i<=r;i++){
        int ans=0;
        int k=i;
        while(k){
            if(ans%10==2) ans++;
            k/=10;
        }
        if(ans==3) d++;
    }
    cout<<d;
	return 0;
}

错因分析:

C++
if(ans%10==2) ans++;
k/=10;

中if(ans%10==2)的ans应改成k。

T806458 T3

题目分析:给出一个数列,然后有m次操作,每次给出一个正整数k,输出k是第几次输入的。

思路:使用map,把队列里的每个数存进map中,输入k时就输出map[k]。

AC代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
// 定义map
map<int,int>q;
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;
        q[x]=i;
    }
    int m;
    cin>>m;
    //输出map中的第k个数
    while(m--){
        int k;
        cin>>k;
        cout<<q[k]<<endl;
    }
	return 0;
}

错误代码:

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1e8+5;
int a[100005];
struct st{
    int sl,bh;
} 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++){
        cin>>a[i];
        tong[a[i]].sl++;
        tong[a[i]].bh=i;
    }
    int m;
    cin>>m;
    int x;
    for(int i=1;i<=m;i++){
        cin>>x;
        if(tong[x].sl==0) cout<<0<<endl;
        else{
            cout<<tong[x].bh<<endl;
        }
    }
	return 0;
}

错因分析:N=1e8+5太大了,会MLE,但题目数据最大为1e9,又小了,会RE。

T806459 T4

题目分析:给出n个数,输出其中出现奇数次的数(保证只有一个)。

思路:不能用数组,要用map,因为map用一个开一个,而数组开的太大,不能用,其余方法与数组相同。

AC代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<int,int>q;
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;
        // 统计各数出现个数
        q[x]++;
    }
    // 循环遍历
    for(auto i:q){
        if(i.second%2==1){
            // 找到答案,输出
            cout<<i.first;
            // 结束程序
            return 0;
        }
    }
}

错误代码:

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

错因分析:数据太大,数组开不到1e9,所以RE。

T806461 T5

题目分析:要把给出的n个字符串去重,然后统计,输出最大值。

思路:先输入,然后把去重后的字符串存进另一个字符串,再用map统计,最后输出。

AC代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<string,int>q;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
    // 输入
    int n;
    cin>>n;
    // 循环输入
    while(n--){
        string s;
        cin>>s;
        // 去重,并存进字符串中
        string ans="";
        for(int i=0;i<s.size();i++){
            if(s[i]!=s[i+1]){
                ans+=s[i];
            }
        }
        // 统计数量
        q[ans]++;
    }
    string ans;
    int maxx=0;
    // 循环遍历
    for(auto i:q){
        if(i.second>maxx){
            maxx=i.second;
            ans=i.first;
        }
    }
    // 输出
    cout<<ans;
	return 0;
}

T806463 T7

题目分析:把与A相减得C所需要的数统计,再把已有的数加起来,得到结果。

思路:用数组输入,用map把所需的数统计,然后用循环把tong[a[i]]加起来,就能得到结果,最后输出。

AC代码

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
map<int,int>tong;
const int N=2e5+5;
int a[N];
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];
        // 统计
        tong[a[i]-c]++;
    }
    int ans=0;
    // 用循环得到答案
    for(int i=1;i<=n;i++){
        ans+=tong[a[i]];
    }
    // 输出
    cout<<ans;
	return 0;
}

错误代码:

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;
    int x;
    int ans=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        for(int j=1;j<=i;j++){
            int k=abs(a[i]-a[j]);
            if(k==c) ans++;
        }
    }
    cout<<ans;
	return 0;
}

错因分析:因为循环嵌套,且题目数据较大,所以有三个样例TLE。

o( ^ v ^ )o 感谢观看 o( ^ v ^ )o

原文