ACM 实验室2020.11.08天梯赛练习*5

7-1 考试座位号 (15分)

用结构体按座位顺序,存储所有人的准考证号 考试座位号,方便通过座位号查找

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ture true
struct people{
    string no;
    int a,b;
}p[1010];
int main()
{
    int n,c,d;
    string s;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s>>c>>d;
        p[c].no=s;
        p[c].a=c;
        p[c].b=d;
    }
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>c;
        cout<<p[c].no<<" "<<p[c].b<<endl;;
    }
    return 0;
}

 7-3 后天 (5分)

给出今天是星期几,求后天是星期几

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ture true
int main()
{

    int n;
    cin>>n;
    cout<<(n+1)%7+1<<endl;
    //cout<<""<<endl;
    return 0;
}

7-4 到底有多二 (15分)

一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ture true

int main()
{
   string s;
   cin>>s;
   double sum=0;
   double flag=1,k=1;
   for(int i=0;i<s.length();i++){
    if(s[i]=='2'){
        sum++;
    }
   }
   int len=s.length();
   if(s[0]=='-'){
    flag=1.5;
    len--;
   }
   if(  (s[s.length()-1]-'0')%2==0  ){
    k=2;
   }
   double b=flag*k*100*(sum/len);
   printf("%.2f",b);
   cout<<'%'<<endl;
    return 0;
}

7-5 念数字 (10分)

输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出fu字。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ture true
int main()
{

    string s[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
    string a;
    cin>>a;
    for(int i=0; i<a.length(); i++)
    {
        if(i==0)
        {
            if(a[i]=='-')
            {
                cout<<"fu";
            }
            else
            {
                cout<<s[a[i]-'0'];
            }
        }else{

        cout<<" "<<s[a[i]-'0'];
        }

    }
    return 0;
}

7-6 帅到没朋友 (20分)

当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友。本题就要求你找出那些帅到没有朋友的人。

输入格式:

输入第一行给出一个正整数N100),是已知朋友圈的个数;随后N行,每行首先给出一个正整数K1000),为朋友圈中的人数,然后列出一个朋友圈内的所有人——为方便起见,每人对应一个ID号,为5位数字(从00000到99999),ID间以空格分隔;之后给出一个正整数M10000),为待查询的人数;随后一行中列出M个待查询的ID,以空格分隔。

注意:没有朋友的人可以是根本没安装“朋友圈”,也可以是只有自己一个人在朋友圈的人。虽然有个别自恋狂会自己把自己反复加进朋友圈,但题目保证所有K超过1的朋友圈里都至少有2个不同的人。

输出格式:

按输入的顺序输出那些帅到没朋友的人。ID间用1个空格分隔,行的首尾不得有多余空格。如果没有人太帅,则输出No one is handsome

注意:同一个人可以被查询多次,但只输出一次。

因为id只有五位嘛,开一个这么大的数组,用来标注这个人有没有在朋友圈出现过

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ture true
int main()
{
    int a[100000]= {0};
    int n,m,b,k,i,j;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>m;
        for(j=0; j<m; j++)
        {
            cin>>b;
            if(m!=1)
            {
                a[b]=1;
            }
        }
    }
    cin>>m;
    int flag=0;
    for(i=0; i<m; i++)
    {
        cin>>b;
        if(flag==0)
        {
            if(a[b]!=1)
            {
                printf("%05d",b);
                a[b]=1;
                flag=1;
            }
        }

        else
        {
            if(a[b]!=1)
            {
                printf(" %05d",b);
                a[b]=1;
            }

        }
    }
    if(flag==0)
    {
        cout<<"No one is handsome"<<endl;
    }
    return 0;
}

7-7 A乘以B (5分)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ture true
int main()
{

    int a,b;
    cin>>a>>b;
    cout<<a*b<<endl;
    //cout<<""<<endl;
    return 0;
}

7-8 计算阶乘和 (10分)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ture true
int main()
{

    int n,sum=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        int sum2=1;
        for(int j=1;j<=i;j++){
            sum2*=j;
        }
        sum+=sum2;
    }
    cout<<sum<<endl;
    return 0;
}

7-9 点赞狂魔 (25分)

统计每个人点赞的不同标签的数量,找出数量最大的前3名,在一行中顺序输出他们的用户名,其间以1个空格分隔,且行末不得有多余空格。如果有并列,则输出标签出现次数平均值最小的那个,题目保证这样的用户没有并列。若不足3人,则用-补齐缺失,例如mike jenny -就表示只有2人。

将每个人的标签排序,遍历一遍找出不同标签的个数,存入结构体,最后对结构体排序

,输出前三个(输出前判断是否够三个)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ture true
struct people{
   string name;
   int maxx;
   int sum;
}p[110];
bool cmp(people a,people b){
    if(a.maxx>b.maxx)
        return true;
    else if(a.maxx<b.maxx)
        return false;
    else{
        if(a.sum<b.sum)
            return true;
        else
            return false;
    }

}
int main()
{
    int n,i,j,a;
    cin>>n;
    for(i=0;i<n;i++){
       cin>>p[i].name;
       cin>>a;
       p[i].sum=a;
       int b[1100];
        for(j=0;j<a;j++){
            cin>>b[j];
        }
        sort(b,b+a);
        int sum=1;
        for(int k=1;k<a;k++){
            if(b[k]!=b[k-1]){
               sum++;
            }
        }
        p[i].maxx=sum;
    }
    sort(p,p+n,cmp);
    if(n>=3){
        cout<<p[0].name<<" "<<p[1].name<<" "<<p[2].name<<endl;
    }
    if(n==2){
        cout<<p[0].name<<" "<<p[1].name<<" -"<<endl;
    }
    if(n==1){
        cout<<p[0].name<<" - -"<<endl;
    }
    return 0;
}

7-11 彩虹瓶 (25分)

用栈(stack)来表示货架,如果搬来的这箱小球正好是可以装填的颜色,++,处理下一个;如果不是,进栈;

判断栈顶元素是不是要用的,是,出栈

最后判断是否能全部按他给定的顺序用掉

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ture true
struct stu{
string name;
int num;
}s[10100];
int main()
{

    int n,m,k,i,b,flag=0;
    cin>>n>>m>>k;
    while(k--){
        stack<int> a;
        flag=0;
        int b[1100]={0};
        for(i=0;i<n;i++)
        {
            cin>>b[i];
        }
        i=0;
        //cout<<"****"<<endl;
        int k=1;
        while(i<=n)
        {
            if(b[i]==k)
            {
                i++;
                k++;
                if(k==n+1){
                    flag=1;
                    cout<<"YES"<<endl;
                    break;
                }
            }
            else if(!a.empty()&&a.top()==k)
            {
                a.pop();
                k++;
                if(k==n+1){
                    flag=1;
                    cout<<"YES"<<endl;
                    break;
                }
            }
            else
            {

                a.push(b[i]);
                if(a.size()>m)
                {
                    flag=1;
                    cout<<"NO"<<endl;
                    break;
                }
                i++;
            }
            //cout<<i<<endl;
        }
        if(flag==0){
            cout<<"NO"<<endl;
        }

    }

    return 0;
}

7-12 玩转二叉树 (25分)

二叉树啊,参考的课本的代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

template <class DataType>
struct BiNode
{
     DataType data;
      BiNode<DataType> *lchild, *rchild;
      BiNode(DataType n){
        data=n;
        lchild=NULL;
        rchild=NULL;
      }
};

template <class DataType>
class BiTree
{
public:
    BiTree()
    {
        int n,i;
        cin>>n;
        int *q,*z;
        q=new int[n];
        z=new int[n];
        for(i=0; i<n; i++)
        {
            cin>>q[i];
        }
        for(i=0; i<n; i++)
        {
            cin>>z[i];
        }
        root = Creat(z,q,n);
    }
      void LeverOrder();                     //层序遍历二叉树
private:
      BiNode<DataType> *Creat(DataType *z,DataType *q,int n);       //构造函数调用
    BiNode<DataType> *root;                             //指向根结点的头指针
};

template <class DataType>
BiNode<DataType> *BiTree<DataType> ::Creat(DataType *q,DataType *z,int n){
    if(n==0)
        return NULL;
    int k=0;
    while(q[0]!=z[k]){
        k++;
    }
    BiNode<DataType> *t=new BiNode<DataType>(q[0]);
    t->lchild=Creat(q+1,z,k);//////////////////////
    t->rchild=Creat(q+k+1,z+k+1,n-k-1);////////////
    return t;

}
int c=0;
template <class DataType>
void BiTree<DataType> :: LeverOrder( )
{
      BiNode<DataType> *Q[100], *q = nullptr;      //顺序队列最多100个结点
      int front = -1, rear = -1;                      //队列初始化
      if (root == nullptr) return;                    //二叉树为空,算法结束
      Q[++rear] = root;                           //根指针入队
      while (front != rear)          //当队列非空时
      {
        q = Q[++front];
        if(c==0){
            cout << q->data;
            c=1;
        }else{
            cout <<" "<< q->data;
        }

          if (q->rchild != nullptr)  Q[++rear] = q->rchild;
          if (q->lchild != nullptr)  Q[++rear] = q->lchild;

      }
}
int main( )
{
    BiTree<int> asd;
    asd.LeverOrder();
    return 0;
}
原文地址:https://www.cnblogs.com/a-specter/p/13978703.html