patクラスAシミュレーション

A1125

#include<iostream>
using namespace std;

/*
    学就要学最简洁,最高超的技术,背就背点更高超的技术,不断的要更新自己的学习方法和思路

大顶堆:根结点的值大于左杰点,且根结点大于右节点
小顶堆:根结点的值小于左杰点,且根结点小于右节点

*/

const int maxn=1010;

int n,m;

int a[maxn];

int ans;

void postOrder(int root)
{
    
    
    if(root>n)
        return ;
    postOrder(root*2);
    postOrder(root*2+1);
    cout << a[root];
    ans++;
    if(ans<n)
        cout << " ";
}

int main()
{
    
    
    cin >> m >> n;

    while(m--)
    {
    
    
        for(int i=1; i<=n; i++)
        {
    
    
            cin >> a[i];
        }
        ans=0;
        bool lt=false,gt=false;
        for(int i=1; i<=n; i++)          //遍历每个结点
        {
    
    
            for(int j=0; j<2; j++)
            {
    
    
                int gen=a[i],zi=a[i*2+j];   //a[i]代表根结点,zi代表左杰点,j为0时候为左孩子,j为1时为右孩子
                if(i*2+j<=n)          //表示在能取到的范围内
                {
    
    
                    if(gen>zi)  //大顶堆
                    {
    
    
                        gt=true;
                    }
                    else  //小顶堆
                    {
    
    
                        lt=true;
                    }
                }

            }
        }
        if(gt && lt)
        {
    
    
            cout << "Not Heap" << endl;
        }
        else if(gt)
        {
    
    
            cout << "Max Heap" << endl;
        }
        else
        {
    
    
            cout << "Min Heap" << endl;
        }
        postOrder(1);
        cout << endl;
    }

}

A1140

#include<iostream>
using namespace std;

string s;

int n,m;

int main()
{
    
    
    cin >> m >> n;
    s=to_string(m);

    //从当前这个字符开始,然后再后面寻找连续相同的字符的个数
    for(int i=0; i<n-1; i++)   //相当于进行n-1次操作
    {
    
    
        string next;             //每次找到下一个数组
        for(int j=0; j<s.size();)
        {
    
    
            int k=j+1;           //k从j+1开始
            while(k<s.size() && s[j]==s[k])
            {
    
    
                k++;
            }
            next+=s[j]+to_string(k-j);
            j=k;    //此时这个k表示的值是与s[j]表示的字符不同的位置
        }
        s=next;   //更新s
    }

    cout << s;
}

A1132

#include<iostream>
using namespace std;

int n;

string s;

int main()
{
    
    
    cin >> n;
    string temp1,temp2;
    int temp3,temp4;
    double origin;
    for(int i=0; i<n; i++)
    {
    
    
        cin >> s;
        temp1=s.substr(0,s.size()/2);
        temp2=s.substr(s.size()/2);
        temp3=stoi(temp1);
        temp4=stoi(temp2);
        origin=stoi(s);
        double result=origin/temp3/temp4;
        if(result-(int)result==0)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}

A1129

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

const int maxn=50010;

int ans[maxn]={
    
    0};   //统计每个商品出现的次数

int n,m,k;

int top_k[11];      //topk数组存放的就是商品编号

bool cmp(int x,int y)
{
    
    
    if(ans[x]!=ans[y])    
    {
    
    
        return ans[x]>ans[y];  //按照商品出现次数的降序排列
    }
    return x<y;     //如果出现次数相同,按照商品编号的升序排序
}

int main()
{
    
    
    cin >> n >> m;
    int id;
    k=0;
    for(int i=0; i<n; i++)   
    {
    
    
        cin >> id;     //输入所访问的商品编号

        if(i!=0)     //对于第1个值不输出
        {
    
    
            cout << id << ":";
            for(int j=0; j<k; j++)         //直接输出top数组
            {
    
    
                cout << " " << top_k[j];
            }
            cout << endl;
        }

        ans[id]++;    //统计每个商品的访问次数

        bool flag=false;    //如果当前访问的商品已经在top数组中,则不用吧该商品放到top数组中
        for(int j=0; j<k; j++)
        {
    
    
            if(top_k[j]==id)
                flag=true;
        }
        if(!flag)
            top_k[k++]=id;   //将商品id放到top数组中

        sort(top_k,top_k+k,cmp);   //对top数组进行题目要求排序

        k=min(k,m);    //如果k>m,则k还是m,因为题中要求最多m个输出,其实这个代码中k的值最大就是m或者m+1
    }

    return 0;
}

A1128

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

const int maxn=1111;

bool row[maxn],zhu[maxn*2],fu[maxn*2];

/*
    1.不要管他的题中的列是按照从下至上进行排列的,直接输入的就是表示的行(从上至下的)
    2.注意主队角线上的元素的y-x+n在一条线内是相同的,所有不同的对角线的y-x+n是不同的
    3.福对角线上的元苏的x+y在一条线内是相同的,所有不同的对角线的x+y是不同的
    
    4.x,y的取值范围分别是1--n,则为2n
        y-x的取值范围是 1-n---n-1 同时加上n 为 1---2n-1 
    所以注意对角上的值的范围是2*n


*/

int n;

int main()
{
    
    
    cin >> n;
    int temp1;
    while(n--)
    {
    
    
        memset(row,false,sizeof(row));   //跟新数组
        memset(zhu,false,sizeof(zhu));
        memset(fu,false,sizeof(fu));
        cin >> temp1;
        int temp2;
        bool success=true;
        for(int y=1; y<=temp1; y++)   
        {
    
    
            int x;
            cin >> x;
            if(row[x] || zhu[y-x+temp1] || fu[x+y])
                success=false;
            row[x]=zhu[y-x+temp1]=fu[x+y]=true;    //将输入的x的行,主副对角线上的值进行标记,不同的一对x,y如果不是在同一行,同一主副对角线 则 x y-x+n x+y表示的值是不同的具有唯一性,
                                            //所以在输入的x和对应的y,就对这些值对应的bool数组进行标记,一旦有相同则不成功
        }
        if(success==true)
        {
    
    
            cout << "YES" << endl;
        }
        else
            cout << "NO" << endl;
    }
    return 0;
}

ここに画像の説明を挿入

A1121

#include<iostream>
#include<map>
#include<set>
using namespace std;

const int maxn=99999;

map<int,int> ss;

int n,m;

int main()
{
    
    
    cin >> n;
    int temp1,temp2;
    for(int i=0; i<n; i++)
    {
    
    
        cin >> temp1 >> temp2;
        ss[temp1]=temp2;
        ss[temp2]=temp1;
    }
    cin >> m;
    set<int> people;
    int temp3;
    for(int i=1; i<=m; i++)
    {
    
    
        cin >> temp3;
        people.insert(temp3);
    }
    for(set<int>::iterator it=people.begin(); it!=people.end(); it++)
        if(people.find(ss[*it])!=people.end())
        {
    
    
            people.erase(ss[*it]);
            people.erase(*it);
        }

    cout << people.size() << endl;

    for(set<int>::iterator it=people.begin(); it!=people.end(); it++)
    {
    
    
        if(it!=people.begin())
            cout << " ";
        printf("%05d",*it);       //注意是保存5位数字

    }

    return 0;
}

A1109

#include<iostream>
#include<map>
#include<algorithm>
using namespace std;

const int maxn=10011;

struct node
{
    
    
    string name;
    int height;
}Node[maxn],Node1[maxn];

int n,k;

bool cmp(node a,node b)
{
    
    
    if(a.height!=b.height)
        return a.height>b.height;
    return a.name<b.name;
}

int main()
{
    
    
    cin >> n >> k;
    string temp1;
    for(int i=0; i<n; i++)
    {
    
    
        cin >> Node[i].name >> Node[i].height;
    }
    sort(Node,Node+n,cmp);

    for(int i=0,j=0; i<k; i++)
    {
    
    
        int len=n/k;
        if(!i)
        {
    
    
            len=n/k+n%k;
        }
        for(int r=len/2+1,l=r-1; r<=len || l>0; r++,l--)   //注意这里是从每行中个子最高的人的位置开始,向两边进行扩展,这里的l,r没有说必须同时满足l>0&&r<=len,只是从中间某个位置向两边填充,各自走各自的,所以用||
        {
    
    
            if(r<=len)
            {
    
    
                Node1[r]=Node[j++];
            }
            if(l>0)
                Node1[l]=Node[j++];
        }

        for(int s=1; s<=len; s++)
        {
    
    
            if(s!=1)
                cout << " ";
            cout << Node1[s].name;
        }
        cout << endl;
    }

}

おすすめ

転載: blog.csdn.net/wsfhdhjs/article/details/109728687