2025寒假天梯赛训练1

寒假第一场训练
目录在右边————>>>
这场天梯赛全部都是原题,实现方法虽然有些遗忘,但是大部分题都可以保证一次通过。有个题可以用弗洛伊德最短路冲过去,但是赛时没有实现。归结为能力问题。

https://pintia.cn/problem-sets/1880234312583233536/exam/overview?

7-1 求心理阴影面积

分割三角形求面积,面积等于右下角三角形面积减去两个三角形和一个正方形面积。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int __int128
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod1=998244353;
const int mod2=1e9+7;
#define  x first
#define  y second
//二分暴龍龍
void miaojiachun()
{
    int x,y;
    cin>>x>>y;
    cout<<5000-x*y/2-(100-x)*y-(100-x)*(100-y)/2<<endl;
}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-2 人与神

cout<<"";

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int __int128
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod1=998244353;
const int mod2=1e9+7;
#define  x first
#define  y second
//二分暴龍龍
void miaojiachun()
{
    cout<<"To iterate is human, to recurse divine."<<endl;
}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-3 通讯录的录入与显示

设置一个map记录编号并输出就好了。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int __int128
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod1=998244353;
const int mod2=1e9+7;
#define  x first
#define  y second
//二分暴龍龍
struct ren{
    string name;
    string birth;
    string sex;
    string gph;
    string ph; 
};
void miaojiachun()
{
    int n;
    cin>>n;
    vector<ren>a(n);
    map<int,ren>u;
    map<int,int>v;
    for(int i=0;i<n;i++){
        cin>>a[i].name>>a[i].birth>>a[i].sex>>a[i].gph>>a[i].ph;
        u[i]=a[i];
        v[i]++;
    }

    int m;
    cin>>m;
    for(int i=1;i<=m;i++){
        int k;
        cin>>k;
        if(v[k]==0){
            cout<<"Not Found"<<endl;
            continue;
        }
        cout<<u[k].name<<" "<<u[k].gph<<" "<<u[k].ph<<" "<<u[k].sex<<" "<<u[k].birth<<endl;
    }
}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-4 算术入门之加减乘除

按照题目要求输出一下就好了。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int __int128
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod1=998244353;
const int mod2=1e9+7;
#define  x first
#define  y second
//二分暴龍龍

void miaojiachun()
{
    int a,b;
    cin>>a>>b;
    printf("%d + %d = %d\n",a,b,a+b);
    printf("%d - %d = %d\n",a,b,a-b);
    printf("%d * %d = %d\n",a,b,a*b);
    if(a%b==0){
        printf("%d / %d = %d\n",a,b,a/b);
    }else{
        printf("%d / %d = %.2f\n",a,b,(1.0*a/(1.0*b)));
    }
}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-5 出生年

从题目给出的输出年份往后进行遍历,如果有满足题目要求的年份就进行输出,输出到了这个年份时,这个人几岁,和这个年份。

使用map记录每个年份有几个不同的数字。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int __int128
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod1=998244353;
const int mod2=1e9+7;
#define  x first
#define  y second
//二分暴龍龍

void miaojiachun()
{
    int x,y;
    cin>>x>>y; 
    for(int i=x;;i++){
        map<int,int>u;
        if(i<1000){
            u[0]++;
        }
        int ii=i;
        while(ii!=0){
            u[ii%10]++;
            ii/=10;
        }
        if(u.size()==y){
            cout<<i-x<<" ";
            string s=to_string(i);
            while(s.size()<4){
                s='0'+s;
            }
            cout<<s<<endl;
            break;
        }
    }
}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-6 九宫格输入法

模拟一下就好了,先用map存一下九宫格。再string进行储存。注意模数的存在。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int __int128
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod1=998244353;
const int mod2=1e9+7;
#define  x first
#define  y second
//二分暴龍龍

void miaojiachun()
{
    map<int,string>u;
    u[1]="1,.?!";
    u[2]="2ABC";
    u[3]="3DEF";
    u[4]="4GHI";
    u[5]="5JKL";
    u[6]="6MNO";
    u[7]="7PQRS";
    u[8]="8TUV";
    u[9]="9WXYZ";
    u[0]="0 ";
    string s;
    getline(cin,s);
    s+=" ";
    int num=0;
    for(int i=0;i<s.size();i++){
        if(s[i]==' '){
            int k=s[i-1]-'0';
            int kk=num%(u[k].size());
            if(kk==0){
                kk=u[k].size();
            }
            char d=u[k][kk-1];
            cout<<d;
            num=0;
            continue;
        }
        num++;
    }
}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-7 螺旋方阵

比较恶心的题,对我来说。
就是按照标题描述,按螺旋方向对数组进行存储,主要就是循环边界的确定,按照从左到右,从上到下,从右到左,从下到上,从外到里的顺序进行存储。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int __int128
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod1=998244353;
const int mod2=1e9+7;
#define  x first
#define  y second
//二分暴龍龍
int a[15][15];
void miaojiachun()
{
    int n;
    cin>>n;
    int num=1;
    for(int i=1;i<=n/2;i++){
        for(int j=i;j<=n-i+1;j++){
            a[i][j]=num;
            // cout<<a[i][j]<<endl;
            num++;
        }
        for(int j=i+1;j<=n-i+1;j++){
            a[j][n-i+1]=num;
            num++;
        }
        for(int j=n-i;j>=i;j--){
            a[n-i+1][j]=num;
            num++;
        }
        for(int j=n-i;j>i;j--){
            a[j][i]=num;
            num++;
        }
    }
    if(n%2==1){
        a[n/2+1][n/2+1]=n*n;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            printf("%3d",a[i][j]);
        }
        cout<<endl;
    }
}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-8 抓老鼠啊~亏了还是赚了?

遇到的第一个大模拟。但是我还是略胜一筹。
对输入的字符串进行遍历,模拟不同状态,设立两个标记,兴奋🤩和伤心(不高兴)😟。来确定每天的状态,如果是兴奋,那不管情况如何,第二天和第三天一定会派出老鼠。如果兴奋状态没了,那么就按照伤心和不高兴来。每天进行伤心和兴奋状态的更新,以及钱数的更新。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int __int128
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod1=998244353;
const int mod2=1e9+7;
#define  x first
#define  y second
//二分暴龍龍
void miaojiachun()
{
    string s;
    cin>>s;
    //T捕鼠夹,C奶酪3元,X什么也不放(everyday)
    //X不高兴期1天,T不高兴期2天,
    //C2天兴奋期,不管高不高兴一定派老鼠。
    //一只老鼠10块钱;
    s=" "+s;
    int n=s.size();
    int xing=0,ju=0;
    string ans="";
    int sum=0;
    for(int i=1;i<=n;i++){
        if(xing!=0){
            xing--;
        }if(ju!=0){
            ju--;
        }
        if(s[i]=='$'){
            break;
        }
        if(s[i]=='T'){
            if(ju==0 and xing==0){
                ans+="D";
                sum+=10;
                sum-=3;
                ju=3;
            }else if(ju==0 and xing!=0){
                ans+="D";
                sum+=10;
                sum-=3;
                ju=3;
            }else if(ju!=0 and xing!=0){
                ans+="D";
                sum+=7;
                ju=3;
            }else if(ju!=0 and xing==0){
                ans+="-";
            }
        }else if(s[i]=='X'){
            if(ju!=0 and xing==0){
                ans+="-";
            }if(ju!=0 and xing!=0){
                ans+="U";
                ju=2;
            }if(ju==0 and xing==0){
                ans+="U";
                ju=2;
            }if(ju==0 and xing!=0){
                ans+='U';
                ju=2;
            }
        }else if(s[i]=='C'){
            if(ju!=0 and xing==0){
                ans+="-";
            }else{
                xing=3;
                ans+="!";
                sum-=3;
            }
        }
        // cout<<i<<" "<<ju<<" "<<xing<<endl;
    }
    cout<<ans<<endl<<sum<<endl;
}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-9 Windows消息队列

优先队列的考察。设立cmp函数,自定义优先队列。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int __int128
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod1=998244353;
const int mod2=1e9+7;
#define  x first
#define  y second
//二分暴龍龍
struct node{
    string x;
    int y;
    node(string x,int y):x(x),y(y){}
};
struct cmp{
    bool operator()(node a,node b){
        return a.y>b.y;
    }
};
priority_queue<node,vector<node>,cmp>q;
void miaojiachun()
{
    int n;
    cin>>n;
    while(n--){
        string a1;
        cin>>a1;
        if(a1=="PUT"){
            string s;
            cin>>s;
            int pro;
            cin>>pro;
            q.push({s,pro});
        }else{
            if(q.empty()){
                cout<<"EMPTY QUEUE!"<<endl;
                continue;
            }
            node x=q.top();
            q.pop();
            cout<<x.x<<endl;
        }
    }
    
}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

自定义优先队列
https://blog.csdn.net/AAMahone/article/details/82787184

点击查看代码
#include<queue>
#include<vector>
#include<iostream>
using namespace std;
 
struct node
{
    int x, y;
    node(int x,int y):x(x),y(y){}
};
 
struct cmp
{
    bool operator()(node a,node b)
    {
        if(a.x == b.x)  return a.y >= b.y;
        else return a.x > b.x;
    }
};
 
int main()
{
    priority_queue<node,vector<node>,cmp> pq;    //带有三个参数的优先队列;
    for(int i = 1; i <= 5; i++)
        for(int j = 1; j <= 5; j++)
            pq.push(node(i,j));
    while(!pq.empty())
    {
        cout<<pq.top().x<<" "<<pq.top().y<<endl;
        pq.pop();
    }
    return 0;
}

7-10 名人堂与代金券

第二道模拟题,这个很简单,注意这类题有自己独特的排名方法,同分数排名一致,等到下一个分数不同的人时,将其排名设置为位置。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int __int128
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod1=998244353;
const int mod2=1e9+7;
#define  x first
#define  y second
//二分暴龍龍
struct ren{
    string zh;
    int sor;
    int p;
};
bool cmp(ren a,ren b){
    if(a.sor==b.sor){
        return a.zh<b.zh;
    }
    return a.sor>b.sor;
}
void miaojiachun()
{
    int n,g,k;
    cin>>n>>g>>k;
    int sum=0;
    vector<ren>a(n+1);
    for(int i=1;i<=n;i++){
        cin>>a[i].zh>>a[i].sor;
        if(a[i].sor>=g){
            sum+=50;
        }if(a[i].sor>=60 and a[i].sor<g){
            sum+=20;
        }
    }
    cout<<sum<<endl;
    sort(a.begin()+1,a.end(),cmp);
    int num=1;
    for(int i=1;i<=n;i++){
        if(a[i].sor!=a[i-1].sor){
            a[i].p=num;
        }else{
            a[i].p=a[i-1].p;
        }
        num++;
    }
    for(int i=1;i<=n;i++){
        if(a[i].p<=k){
            cout<<a[i].p<<" "<<a[i].zh<<" "<<a[i].sor<<endl;
        }
    }
    
}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-11 用扑克牌计算24点

大大模拟,我不会写。考虑函数的优良性质。可以简化思路障碍。手写可以得到
3个运算符,4个数字,2对括号,有5种组合方式。

  • ((a op b) op c) op d

  • (a op (b op c)) op d

  • a op ((b op c) op d)

  • a op (b op (c op d))

  • (a op b) op (c op d)

注意用浮点数运算

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
// typedef long long ll;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod9=998244353;
const int mod1=1e9+7;
#define  x first
#define  y second
//二分暴龍龍
char op[5]={'.','+','-','*','/'};
int flag=0;
float result(float x,float y,char op){
    if(op=='+'){
        return x+y;
    }else if(op=='-'){
        return x-y;
    }else if(op=='*'){
        return x*y;
    }else{
        return x/y;
    }
}
float model1(float a,float b,float c,float d,char op1,char op2,char op3){
    float x,y,z;
    x=result(a,b,op1);
    y=result(x,c,op2);
    z=result(y,d,op3);
    return z;
}
float model2(float a,float b,float c,float d,char op1,char op2,char op3){
    float x,y,z;
    x=result(b,c,op2);
    y=result(a,x,op1);
    z=result(y,d,op3);    
    return z;
}
float model3(float a,float b,float c,float d,char op1,char op2,char op3){
    float x,y,z;
    x=result(c,d,op3);
    y=result(b,x,op2);
    z=result(a,y,op1);
    return z;
}
float model4(float a,float b,float c,float d,char op1,char op2,char op3){
    float x,y,z;
    x=result(b,c,op2);
    y=result(x,d,op3);
    z=result(a,y,op1);
    return z;
}
float model5(float a,float b,float c,float d,char op1,char op2,char op3){
    float x,y,z;
    x=result(a,b,op1);
    y=result(c,d,op3);
    z=result(x,y,op2);
    return z;
}
int calo(int a,int b,int c,int d){
    for(int i=1;i<=4;i++){
        for(int j=1;j<=4;j++){
            for(int k=1;k<=4;k++){
                char op1=op[i],op2=op[j],op3=op[k];
                if(model1(a,b,c,d,op1,op2,op3)==24){
                    printf("((%d%c%d)%c%d)%c%d",a,op1,b,op2,c,op3,d);
                    flag=1;
                    return 0;
                }
                if(model2(a,b,c,d,op1,op2,op3)==24) {
                    printf("(%d%c(%d%c%d))%c%d",a,op1,b,op2,c,op3,d);
                    flag=1;
                    return 0;
                }
                if(model3(a,b,c,d,op1,op2,op3)==24) {
                    printf("%d%c(%d%c(%d%c%d))",a,op1,b,op2,c,op3,d);
                    flag=1;
                    return 0;
                }
                if(model4(a,b,c,d,op1,op2,op3)==24) {
                    printf("%d%c((%d%c%d)%c%d)",a,op1,b,op2,c,op3,d);
                    flag=1;
                    return 0;
                }
                if(model5(a,b,c,d,op1,op2,op3)==24){
                    printf("(%d%c%d)%c(%d%c%d)",a,op1,b,op2,c,op3,d);
                    flag=1;
                    return 0;
                }
            }
        }
    }
    return 0;
    // cout<<1<<endl;
}

void miaojiachun()
{
    int x[5]={0};
    scanf("%d %d %d %d",&x[1],&x[2],&x[3],&x[4]);
    // cout<<x[1]<<x[2]<<x[3]<<x[4]<<endl;
    for(int i=1;i<=4;i++){
        for(int j=1;j<=4;j++){
            if(j==i){continue;}
            for(int k=1;k<=4;k++){
                if(i==k or j==k){continue;}
                for(int t=1;t<=4;t++){
                    if(i==t or j==t or k==t){continue;}
                    // cout<<1<<endl;
                    calo(x[i],x[j],x[k],x[t]);
                    if(flag){
                        return;
                    }
                }
            }
        }
    }
    if(!flag){
        cout<<"-1"<<endl;
        return;
    }
}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-12 玩转二叉树

数据结构题。利用递归得到树的子树,与普通的中序前序求后序(层序)不同,在递归时就反转其左右子树。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
// typedef long long ll;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX;
const int mod9=998244353;
const int mod1=1e9+7;
#define  x first
#define  y second
//二分暴龍龍
int n,pre[35],in[35];
priority_queue<PII,vector<PII>,greater<PII>>pq;
void getc(int l,int r,int root,int index){
    if(l>r) return;
    int i=l;
    while(in[i]!=pre[root]) ++i;
    pq.push({index,pre[root]});
    getc(l,i-1,root+1,index*2+2);
    getc(i+1,r,root+i-l+1,index*2+1);
}
void miaojiachun()
{
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>in[i];
    }
    for(int i=0;i<n;i++){
        cin>>pre[i];
    }
    getc(0,n-1,0,0);
    // cout<<1<<endl;
    PII p=pq.top();
    pq.pop();
    cout<<p.y;
    while(!pq.empty()){
        p=pq.top();
        pq.pop();
        cout<<" "<<p.y;
    }
    cout<<endl;
}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-13 六度空间

需要自豪的是,我只会Floyd。

利用Floyd求最短路,计算各个点两两之间的最短路径长度,然后计算百分比就好了。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
// typedef long long ll;
typedef pair<int, int> PII;
const int N =2e5+7;
const int inf=LLONG_MAX/10;
const int mod9=998244353;
const int mod1=1e9+7;
#define  x first
#define  y second
//二分暴龍龍
int a[1005][1005];
void miaojiachun()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            a[i][j]=inf;
        }
    }
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        a[x][y]=1;
        a[y][x]=1;
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(a[i][j]>a[i][k]+a[k][j]){
                    a[i][j]=a[i][k]+a[k][j];
                }  
            }
        }
    }
    // for(int i=1;i<=n;i++){
    //     for(int j=1;j<=n;j++){
    //         cout<<a[i][j]<<" ";
    //     }
    //     cout<<endl;
    // }
    vector<double>f(n+1);
    for(int i=1;i<=n;i++){
        int sum=0;
        for(int j=1;j<=n;j++){
            if(a[i][j]<=6){
                sum++;
            }
        }
        f[i]=(1.0*sum)/(1.0*n);
    }
    for(int i=1;i<=n;i++){
        printf("%d: %.2lf%%\n",i,f[i]*100);
    }

}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin>>ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

7-14 可怜的复杂度

不会写吉如一的题。有没有人教我,链接发评论区就好。谢谢。

7-15 塔防游戏

我只能照着题解写一下下,不如看一下别人的题解。

that's all.

posted @ 2025-01-25 21:01  miao-jc  阅读(25)  评论(0)    收藏  举报