团体程序设计天梯赛--部分25分题

L2-003 月饼

#include
using namespace std;
struct xx{
    double c,a,b;
}s[1005];
bool cmp(xx x,xx y){
    return x.c>y.c;
}
int main(){
        int n,i,k;double x=0;
    cin>>n>>k;
    for(i=0;i>s[i].a;
    for(i=0;i>s[i].b;s[i].c=s[i].b*1.0/s[i].a;}
    sort(s,s+n,cmp);
    
         for(i=0;is[i].a){k-=s[i].a;x+=s[i].b;}
             else {x+=s[i].b*1.0*k/s[i].a;k=0;break;}
         }
     printf("%.2f",x);
    return 0;
}

L2-005 集合相似度

#include
using namespace std;
int main(){
    int n,m,k,i,x,y,b,c;
    seta[55];
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>m;
         while(m--){
             cin>>x;
             a[i].insert(x);
         }
    }
    cin>>k;
    while(k--){
        b=0,c=0;
        cin>>x>>y;
        b=a[x].size()+a[y].size();
        for(auto j:a[x]){
            if(a[y].find(j)!=a[y].end())
                c++;
        }
        printf("%.2f%%\n",c*100.0/(b-c));
    }
    return 0;
}

L2-008 最长对称子串

#include
using namespace std;
int main(){
    int i,j,k,x;string s;
    getline(cin,s);
    for(i=0;i=0&&i+jx)x=(j-1)*2+1;
        while(i-k>=0&&i+k+1x)x=2*k;
    }cout<

L2-009 抢红包

#include
using namespace std;
struct xx{
    int a,b=0,c=0,d=0;
}s[10005];
bool cmp(xx x,xx y){
    if(x.c==y.c)return x.b>y.b;
    if(x.c==y.c&&x.b==y.b)return x.ay.c;
}
int main(){
    int n,i,k,p,x;
    cin>>n;
    for(i=1;i<=n;i++){
        s[i].a=i;
        cin>>k;
        while(k--){
            cin>>x>>p;
            s[x].c+=p;
            s[x].b++;
            s[i].d+=p;
        }
    }
     for(i=1;i<=n;i++)s[i].c=s[i].c-s[i].d;
    sort(s+1,s+n+1,cmp);
    for(i=1;i<=n;i++){
        printf("%d %.2f\n",s[i].a,s[i].c/100.0);
    }
    return 0;
}

L2-010 排座位

#include
using namespace std;
int main(){
    int n,m,k,i,j,a[105][105]={0},b[105][105]={0};
    cin>>n>>m>>k;
    int x,y,z;
    while(m--){
        cin>>x>>y>>z;
        if(z==1)a[x][y]=a[y][x]=1;
        else b[x][y]=b[y][x]=1;
    }
    for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    for(int c=1;c<=n;c++)
         if(a[i][j]==1&&a[j][c]==1)a[i][c]=1;
    for(i=1;i<=k;i++){
                cin>>x>>y;
                if(a[x][y]&&!b[x][y])cout<<"No problem";
                if(!a[x][y]&&!b[x][y])cout<<"OK";
                if(a[x][y]&&b[x][y])cout<<"OK but...";
                if(!a[x][y]&&b[x][y])cout<<"No way";
                if(i

L2-014 列车调度

#include
using namespace std;
int main(){
    int n,x;
    sets;
    cin>>n;
    while(n--){
        cin>>x;
        set::iterator i=s.lower_bound(x);
        if(i!=s.end()){
            s.erase(i);
            s.insert(x);
        }
        else s.insert(x);
    }cout<

L2-015 互评成绩 

#include
using namespace std;
int main(){
    int n,k,m,i,x[10005][15],a,b,c;double y,s[10005];
    cin>>n>>k>>m;
    c=k-2;
     for(i=0;i>x[i][j];
        }
    }
    for(i=0;ia)a=x[i][j];
            if(x[i][j]

L2-017 人以群分

#include
using namespace std;
int main(){
    int n,i,a[200005],x=0,y=0;
    cin>>n;
    for(i=0;i>a[i];
    sort(a,a+n);
    int b;
    if(n%2==1)b=(n+1)/2;
    else b=n/2;
    for(i=0;i#include
using namespace std;
struct xx{
    string a;
    int b;
}c[10005];
int main(){
    int n,m,i,x=0,k=0,j=0;string s,y[10005];
    cin>>n;
    mapp;
    while(n--){cin>>s;p[s]=1;}
    cin>>m;
    for(i=0;i>c[i].a>>c[i].b;
        x+=c[i].b;
    }x/=m;
    for(i=0;ix&&p[c[i].a]==0){y[j++]=c[i].a;k=1;}
    }
    sort(y,y+j);
    if(k==0)cout<<"Bing Mei You";
    else {
        for(i=0;i

L2-021 点赞狂魔

#include
using namespace std;
struct xx{
    string x;
    int y=0,k;
    double z=0;
}s[105];
bool cmp(xx a,xx b){
    if(a.y==b.y)return a.z>b.z;
    return a.y>b.y;
}
int main(){
    int n,i,a;
    mapb;
    cin>>n;
    getchar();
    for(i=0;i>s[i].x>>s[i].k;
         for(int j=0;j>a;
            if(b[a]==0){s[i].y++;b[a]=1;}
        }
        s[i].z=s[i].y*1.0/s[i].k;
        b.clear();
    }
    sort(s,s+n,cmp);
    if(n>=3){
         cout<

L2-024 部落

#include
using namespace std;
int a[10005];
int find(int x){
    if(a[x]!=x)a[x]=find(a[x]);
    return a[x];
}
int main(){
    int n,k,i,j,x,y,q,b=0,c=0,m;
    stacks;
    cin>>n;
    while(n--){
        cin>>k;
        if(k--){
            cin>>i;
            if(a[i]==0){
                b++;a[i]=i;s.push(a[i]);
            }
        }
        while(k--){
            cin>>j;
            if(a[j]==0){
                b++;a[j]=j;s.push(a[j]);
            }
            a[find(i)]=find(j);
        }
    }
    while(s.size()){
        m=s.top();
        if(m==a[m])c++;
        s.pop();
    }
    cout<>q;
    while(q--){
        cin>>x>>y;
        if(find(x)==find(y))cout<<"Y\n";
        else cout<<"N\n";
    }
    return 0;
}

L2-027 名人堂与代金券

#include
using namespace std;
struct xx{
string a;
    int b;
}s[10005];
bool cmp(xx x,xx y){
    if(x.b==y.b)return x.ay.b;
}
int main(){
    int n,g,k,i,x,y=0,c=1;
    cin>>n>>g>>k;
    for(i=1;i<=n;i++){
        cin>>s[i].a>>s[i].b;
        if(s[i].b>=60&&s[i].b=g&&s[i].b<=100)y+=50;
    }
    sort(s+1,s+n+1,cmp);
    cout<k)break;
            cout<

L2-032 彩虹瓶

#include
using namespace std;
int main(){
    int n,m,k,x,j;
    cin>>n>>m>>k;
    while(k--){
        stacks;
        int z=0,j=1;
        for(int i=0;i>x;
            if(x==j){j++;
                     while(s.empty()==0){
                    int y=s.top();
                    if(y==j){j++;s.pop();continue;}
                    else break;
                }
                     continue;}
            else {
                s.push(x);
                 if(s.size()>m)z=1; 
                continue;
                 }
                    }
        if(z==0&&j==n+1)cout<<"YES\n";
        else cout<<"NO\n";
    }
    return 0;
}

L2-033 简单计算器

#include
using namespace std;
int main(){
         int n,x,x1,x2,k=0,z,sum=0;char y;stacka;stackb;
    cin>>n;
    for(int i=0;i>x;a.push(x);}
    for(int i=0;i>y;b.push(y);}
    while(a.empty()==0&&b.empty()==0){
        y=b.top();b.pop();
        x1=a.top();a.pop();
        x2=a.top();a.pop();
        if(y=='+')sum=x2+x1;
        if(y=='-')sum=x2-x1;
        if(y=='*')sum=x2*x1;
        if(y=='/'&&x1)sum=x2/x1;
        if(y=='/'&&x1==0){
            k=1;printf("ERROR: %d/0",x2);
            break;
        }
        a.push(sum);
        z=a.top();
    }if(k==0)cout<

L2-037 包装机

#include
using namespace std;
int main(){
      int n,m,k,x;stacks;char a[105][1005];
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
    }
    while(cin>>x){
        if(x==-1)break;
        if(x==0){ if(!s.empty()){cout<

L2-040 哲哲打游戏

#include
using namespace std;
int main(){
    int n,m,i,x,k,b[100005],p,q,y=1;vectora[100005];
    cin>>n>>m;
   for(i=1;i<=n;i++){
        cin>>k;
        while(k--){
            cin>>x;
            a[i].push_back(x);
        }
    }
    while(m--){
        cin>>p>>q;
        if(p==0)y=a[y][q-1];
        if(p==2)y=b[q];
        if(p==1){
            b[q]=y;
            cout<

L4-204 盲盒包装流水线

#include
using namespace std;
int main(){
    int n,s,a[100005],i,m,k,x;
    mapb;
    cin>>n>>s;
    for(i=1;i<=n;i++)cin>>a[i];
    m=n/s;
    int l=1,r=s;
     while(m--){
         for(i=r;i>=l;i--)
         {
             cin>>x;b[a[i]]=x;
         }
         l=r+1;r+=s;
     }
    cin>>k;
    while(k--){
        cin>>x;
        if(b[x]==0)cout<<"Wrong Number\n";
        else cout<

你可能感兴趣的:(天梯赛题集,算法,c++)