using namespace std;int main(){ cout<<"To iterate is human, to recurse di_降价提醒机器人c语言">
当前位置:   article > 正文

2021年cccc天梯赛题解_降价提醒机器人c语言

降价提醒机器人c语言

L1-1 人与神 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652352
题目大意:输出一行"To iterate is human, to recurse divine."

#include<iostream>
using namespace std;
int main(){
	cout<<"To iterate is human, to recurse divine.";
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
print("To iterate is human, to recurse divine.")
  • 1

L1-2 两小时学完C语言 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652353
题目大意:输出还剩下多少页数没有读完,总体减去已读即可
已读为读的天数乘以每天读的页数

#include<iostream>
using namespace std;
int main(){
    int a,b,c;
    cin>>a>>b>>c;
    cout<<a-b*c;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

L1-3 强迫症 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652354
题目大意:将时间格式化
首先不能影响正确的时间
其次对22年开始划分

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
    int n,y;
    cin>>n;
    y=n%100;
    n/=100;
    if(n<22)n+=2000;
    else if(n<100)n+=1900;
    printf("%02d-%02d",n,y);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

L1-4 降价提醒机器人 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652355
题目大意:一旦有降价便输出一行

#include<iostream>
#include<cstdio>
using namespace std;
int main(){
    int n;
    cin>>n;
    double a,x;
    cin>>a;
    while(n--){
        cin>>x;
        if(x<a)printf("On Sale! %.1lf\n",x);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

L1-5 大笨钟的心情 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652356
题目大意:将每天的心情存入,然后每次读取当时的心情,如果大于50便输出

#include<iostream>
using namespace std;
int p[25];
int main(){
    for(int i=0;i<24;i++)
        cin>>p[i];
    int x;
    while(cin>>x)
        if(x<24&&x>=0)
            if(p[x]>50)
                cout<<p[x]<<" Yes\n";
            else cout<<p[x]<<" No\n";
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

L1-6 吉老师的回归 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652357
题目大意:如果题目中带有简单和签到便跳过,反之记录,如果写的题目超过了他的过题数,他便ak了,反之输出记录的过题数

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
    int n,m;
    string s;
    vector<string> p;
    cin>>n>>m;
    getchar();
    while(n--){
        getline(cin,s);
        if(s.find("easy")!=-1)continue;
        if(s.find("qiandao")!=-1)continue;
        p.push_back(s);
    }
    if(p.size()<=m)cout<<"Wo AK le";
    else cout<<p[m];
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[510];
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    while(n--){
        getchar();
        scanf("%[^\n]",s+1);
        int len=strlen(s+1);
        for(int i=1;i<len;i++)
            if(s[i]=='e'&&s[i+1]=='a'&&s[i+2]=='s'&&s[i+3]=='y')
                break;
            else if(s[i]=='q'&&s[i+1]=='i'&&s[i+2]=='a'&&s[i+3]=='n'&&s[i+4]=='d'&&s[i+5]=='a'&&s[i+6]=='o')
                break;
            else if(i==len-1){
                m--;
                break;
            }
        if(m<0){
            printf("%s",s+1);
            return 0;
        }
    }
    printf("Wo AK le");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

L1-7 天梯赛的善良 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652358
题目大意:每次有最大最小值便更新数目和值

#include<iostream>
const int INF=0x3f3f3f3f;
int mmin=INF,cntmmin=0,mmax=-INF,cntmmax=0;
using namespace std;
int main(){
    int x,n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>x;
        if(x>mmax){
            cntmmax=1;
            mmax=x;
        }
        else if(x==mmax)cntmmax++;
        if(x<mmin){
            cntmmin=1;
            mmin=x;
        }
        else if(x==mmin)cntmmin++;
    }
    cout<<mmin<<" "<<cntmmin<<endl;
    cout<<mmax<<" "<<cntmmax<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

L1-8 乘法口诀数列 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652359
题目大意:输出乘法表

#include<iostream>
using namespace std;
const int N=2e3+7;
int s[N];
int main(){
    int n;
    cin>>s[0]>>s[1]>>n;
    for(int i=0,j=2;i<=n;i++){
        int a=s[i]*s[i+1];
        if(a<10)s[j++]=a;
        else s[j++]=a/10,s[j++]=a%10;
    }
    for(int i=0;i<n;i++)
        cout<<s[i]<<" ";
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

L2-1 包装机 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652360
题目大意:用一个栈保存框,传送带可以用队列存也可以用栈存,然后模拟即可

#include<iostream>
#include<stack>
using namespace std;
const int N=1e2+7;
stack<char> p[N],st;
char str[100010];
int main(){
    int n,m,k;
    cin>>n>>m>>k;
    string s;
    for(int i=1;i<=n;i++){
        cin>>s;
        for(int j=m;j;j--)
            p[i].push(s[j-1]);
    }
    int x,cnt=0;
    while(cin>>x,x!=-1)
        if(!x){
            if(st.size())
                str[cnt++]=st.top(),st.pop();
        }
        else{
            if(p[x].size()){
                if(st.size()==k)
                    str[cnt++]=st.top(),st.pop();
                st.push(p[x].top());
                p[x].pop();
            }
        }
    str[cnt]='\0';
    cout<<str;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

L2-2 病毒溯源 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652361
题目大意:因为没有重复感染,所以成了树的深度搜索,用拓扑排序减少重复搜索

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N=1e4+7;
vector<int> t,p,s[N];
int b[N];
int dfs(int i){
    b[i]=1;
    if(!s[i].size()&&p.size()<t.size())p=t;
    for(int j=0;j<s[i].size();j++){
        t.push_back(s[i][j]);
        dfs(s[i][j]);
        t.pop_back();
    }
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int m,x;
        cin>>m;
        for(int j=0;j<m;j++){
            cin>>x;
            s[i].push_back(x);
        }
        sort(s[i].begin(),s[i].end());
    }
    for(int i=0;i<n;i++){
        t.push_back(i);
        if(!b[i])dfs(i);
        t.pop_back();
    }
    cout<<p.size()<<"\n";
    for(int i:p)
        cout<<i<<" ";
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

L2-3 清点代码库 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652362
题目大意:将一个数组记录一个hash值,然后保存,有利于快速比对是否重复,这里我用map缩短代码

#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
map<vector<int>,int> m;
const int N=1e4+7;
vector<pair<int,vector<int>>> s;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n,k,x;
    cin>>n>>k;
    while(n--){
        vector<int> t;
        for(int j=0;j<k;j++){
            cin>>x;
            t.push_back(x);
        }
        m[t]++;
    }
    for(auto [a,b]:m)s.push_back({-b,a});
    sort(s.begin(),s.end());
    cout<<s.size()<<"\n";
    for(int i=0;i<s.size();i++){
        cout<<-s[i].first;
        for(int j=0;j<s[i].second.size();j++)
            cout<<" "<<s[i].second[j];
        cout<<"\n";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

L2-4 哲哲打游戏 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652363
题目大意:很简单,用一个数组模拟即可。

#include<iostream>
#include<vector>
using namespace std;
const int N=1e5+7;
vector<int> s[N];
int p[N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        int k,x;
        cin>>k;
        for(int j=0;j<k;j++){
            cin>>x;
            s[i].push_back(x);
        }
    }
    int v=1;
    while(m--){
        int x,j;
        cin>>x>>j;
        if(!x)v=s[v][j-1];
        else if(x==1)p[j]=v,cout<<v<<"\n";
        else v=p[j];
    }
    cout<<v;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

L3-1 森森旅游 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652364
题目大意:由于现金只能兑换一次,并且要一次性将现金全部兑换成旅游币,将该图分为一个两个点的单源最短路,然后通过堆来寻找每一次更改汇率的最小现金额。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<set>

#define endl "\n"

using namespace std;
typedef long long ll;

struct xy{
    ll x,y;
    inline bool operator<(const xy &b)const{
        return x>b.x;
    }
};

const int N=1e5+7;
const int M=2e5+7;
const ll INF=0x3f3f3f3f3f3f3f3fll;

ll dis1[N],dis2[N];

ll h1[M],h2[M],ne1[M],ne2[M],e1[M],e2[M],w1[M],w2[M],idx;

bool st[M];

int ra[N];

int n,m,k;

void add(int a,int b,int c,int d){
    e1[++idx]=b;
    w1[idx]=c;
    ne1[idx]=h1[a];
    h1[a]=idx;
    e2[idx]=a;
    w2[idx]=d;
    ne2[idx]=h2[b];
    h2[b]=idx;
}

void dij(int i,ll *dist,ll *w,ll *h,ll *e,ll *ne){
    memset(dist,0x3f,sizeof dis1);
    memset(st,0,sizeof st);
    dist[i]=0;
    priority_queue<xy> heap;
    heap.push({0,i});
    while(heap.size()){
        xy k=heap.top();
        heap.pop();
        ll var=k.y,len=k.x;
        if(st[var])continue;
        st[var]=true;
        for(int j=h[var];j;j=ne[j]){
            int en=e[j];
            if(dist[en]>len+w[j]){
                dist[en]=len+w[j];
                heap.push({dist[en],en});
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>m>>k;
    for(int i=0;i<m;i++){
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        add(a,b,c,d);
    }
    dij(1,dis1,w1,h1,e1,ne1);dij(n,dis2,w2,h2,e2,ne2);
    for(int i=1;i<=n;i++)cin>>ra[i];
    multiset<ll> S;
    for(int i=1;i<=n;i++)
        if(dis1[i]!=INF&&dis2[i]!=INF){
            S.insert(dis1[i]+(dis2[i]+ra[i]-1)/ra[i]);
        }
    while(k--){
        int a,b;
        cin>>a>>b;
        if(dis1[a]!=INF&&dis2[a]!=INF){
            S.erase(S.find(dis1[a]+(dis2[a]+ra[a]-1)/ra[a]));
            ra[a]=b;
            S.insert(dis1[a]+(dis2[a]+ra[a]-1)/ra[a]);
        }
        cout<<*S.begin()<<endl;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

L3-2 还原文件 (5 分)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/1386335159927652365
题目大意:dfs加剪枝,也可以使用kmp来写,切记每次对比的长度要减1因为每一个尾结点要重复配对,剪枝。

#include<iostream>
#include<vector>
using namespace std;

const int N=1e5+7;

vector<int> s[N];

int t[N],cnt=0;

int n,m,x;

bool st[N],flag=true;

bool p(int i,int len){
    for(int k=0;k<s[i].size();k++)
        if(s[i][k]!=s[0][len+k])
            return false;
    return true;
}

void dfs(int len){
    if(len==s[0].size()-1){flag=false;return ;}
    for(int i=1;flag&&i<=n;i++)
        if(!st[i]&&p(i,len)){
            st[i]=1;
            t[cnt++]=i;
            dfs(len+s[i].size()-1);
            if(!flag)return ;
            cnt--;
            st[i]=0;
        }
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>x;
        s[0].push_back(x);
    }
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>m;
        for(int j=0;j<m;j++){
            cin>>x;
            s[i].push_back(x);
        }
    }
    dfs(0);
    for(int i=0;i<cnt;i++)
        cout<<t[i]<<(i==cnt-1?"\n":" ");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/232648
推荐阅读
相关标签
  

闽ICP备14008679号