PAT 常考题 cpp kit

考前最后一波

看题仔细

参数的含义不要弄错

一定时间内找不到错误并且确定算法没有问题可以考虑重写

优先队列

//升序队列
priority_queue <int,vector<int>,greater<int> > q;
//降序队列
priority_queue <int,vector<int>,less<int> >q;

//greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)

自定义类型

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

//方法1
struct tmp1 //运算符重载<
{
    int x;
    tmp1(int a) {x = a;}
    bool operator<(const tmp1& a) const
    {
        return x < a.x; //大顶堆
    }
};

//方法2
struct tmp2 //重写仿函数
{
    bool operator() (tmp1 a, tmp1 b) 
    {
        return a.x < b.x; //大顶堆
    }
};

int main() 
{
    tmp1 a(1);
    tmp1 b(2);
    tmp1 c(3);
    priority_queue<tmp1> d;
    d.push(b);
    d.push(c);
    d.push(a);
    while (!d.empty()) 
    {
        cout << d.top().x << '\n';
        d.pop();
    }
    cout << endl;

    priority_queue<tmp1, vector<tmp1>, tmp2> f;
    f.push(b);
    f.push(c);
    f.push(a);
    while (!f.empty()) 
    {
        cout << f.top().x << '\n';
        f.pop();
    }
}

AVL

BinTree SingleLroate(BinTree root)
{
    BinTree tt = root->lft;
    root->lft = tt->rgt;
    tt->rgt = root;
    updateHeight(root);
    updateHeight(tt);
    return tt;
}
BinTree SingleRroate(BinTree root)
{
    BinTree tt = root->rgt;
    root->rgt = tt->lft;
    tt->lft = root;
    updateHeight(root);
    updateHeight(tt);
    return tt;
}
BinTree RLroate(BinTree root)
{
    root->rgt = SingleLroate(root->rgt);
    return SingleRroate(root);
}
BinTree LRroate(BinTree root)
{
    root->lft = SingleRroate(root->lft);
    return SingleLroate(root);
}
BinTree Insert(BinTree root,int x)
{
    if(root == NULL)
    {
        return createNode(x);
    }
    if(x < root->val)
    {
        root->lft = Insert(root->lft,x);
        if(difHeight(root)==2)
        {
            if(x < root->lft->val)
            {
                root = SingleLroate(root);
            }
            else
            {
                root = LRroate(root);
            }
        }
    }
    else
    {
        root->rgt = Insert(root->rgt,x);
        if(difHeight(root)==-2)
        {
            if(x > root->rgt->val)
            {
                root = SingleRroate(root);
            }
            else
            {
                root = RLroate(root);
            }
        }
    }
    updateHeight(root);
    return root;
}

平方探测法

if(a[(x+i*i)%m]==0)
{
    a[(x+i*i)%m]=x;
    ans.push_back((x+i*i)%m);
    flag=1;
    break;
}

prim

#include <bits/stdc++.h>

using namespace std;
const int N=200;
int mp[N][N];
int near[N];
int cost[N];
int Prim(int n)
{
    memset(near,0,sizeof(near));
    memset(cost,0,sizeof(cost));
    int ret=0;
    for(int i=2;i<=n;i++)
    {
        if(mp[1][i])
        {
            near[i]=1;
            cost[i]=mp[1][i];
        }
    }
    near[1]=-1;
    for(int i=1;i<n;i++)//因为第一个点已经在生成树的集合里面了 所以只需要跑n-1遍
    {
        int v=-1;
        int _min=INT_MAX;
        for(int j=1;j<=n;j++)
        {
            if(near[j]!=-1&&cost[j])
            {
                if(cost[j]<_min)
                {
                    _min=cost[j];
                    v=j;
                }
            }
        }
        if(v==-1)break;
        ret+=_min;
        near[v]=-1;
        for(int j=1;j<=n;j++)
        {
            if(near[j]!=-1)
            {
                if(!cost[j])
                {
                    if(mp[v][j])
                    {
                        cost[j]=mp[v][j];
                        near[j]=v;
                    }
                }
                else
                {
                    if(mp[v][j]&&cost[j]>mp[v][j])
                    {
                        cost[j]=mp[v][j];
                        near[j]=v;
                    }
                }
            }
        }
    }
    return ret;
}
int main()
{
    int m,n,x,y,z;
    while(scanf("%d",&n)!=EOF&&n)
    {
        memset(mp,0,sizeof(mp));
        m=(n-1)*n/2;
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&z);
            mp[x][y]=mp[y][x]=z;
        }
        cout<<Prim(n)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值