PAT 临考

最大公因数

ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}
查找字符串a是否包含子串b,不是用strA.find(strB) > 0 而是 strA.find(strB) != string:npos

其中string:npos是个特殊值,说明查找没有匹配
long long ksm(long long a,long long b)
{
    int ans = 1,base = a;
    while(b != 0){
        if(b & 1 != 0){// 使用位运算判断奇数,比模运算更快
            ans *= base;
        }
        base *= base;
        b >>= 1;// 直接右移一位,比除2块
    }
    return ans;
}
long long convert(string n, long long radix){
    long long base = 1;
    long long num = 0;
    for(auto lt = n.rbegin(); lt != n.rend(); lt++){
        int temp = isdigit(*lt) ? *lt - '0' : *lt - 'a' + 10;
        num += temp * base;// 累加低位
        base *= radix; // 增大底数
    }
    return num;
}

随机主元,排序和找第k大

#include<bits/stdc++.h>
int rand_partition(int left,int right,int a[])
{
    int random_num=(round(1.0*rand()/RAND_MAX*(right-left)+left));
    std::swap(a[left],a[random_num]);
    int temp=a[left];
    while(left<right)
    {
        while(left<right&&a[right]>temp) right--;
        a[left]=a[right];
        while(left<right&&a[left]<=temp) left++;
        a[right]=a[left];
    }
    a[left]=temp;
    return left;//主元经过一轮交换后的位置
}
void quick_sort(int left,int right,int a[])
{
    if(left<right)
    {
        int pos=rand_partition(left,right,a);
        quick_sort(left,pos-1,a);
        quick_sort(pos+1,right,a);
    }
}
int find(int left,int right,int a[],int k)
{
    if(left==right) return a[left];
    int p=rand_partition(left,right,a);
    int m=p-left+1;
    if(m==k) return a[p];
    if(k<m) find(left,p-1,a,k);
    else find(p+1,right,a,k-m);//找k减去m个
}
signed main(void)
{
    srand((unsigned)time(NULL));
    int n1,n2,k;
    int a[400005];
    scanf("%d",&n1);
    for(int i=0;i<n1;i++)
    {
        scanf("%d",&a[i]);
    }
    scanf("%d",&n2);
    for(int i=n1;i<n1+n2;i++)
    {
        scanf("%d",&a[i]);
    }
    if((n1+n2)&1) k=(n1+n2)/2+1;
    else k=(n1+n2)/2;
    std::cout<<find(0,n1+n2-1,a,k);
    // std::sort(a,a+n1+n2);
    // for(int i=0;i<n1+n2;i++)
    // std::cout<<a[i]<<" ";
    system("pause");
}

建树

#include<bits/stdc++.h>
int ans=0;
int pre[31];
int post[31];
int in[31];
int data[31];
int get_pre[31];
int n;
struct node
{
    int value;
    node *lchild,*rchild;
};
node* pre_create(int inl,int inr,int prel,int prer)
{
    if(prel>prer) return NULL;
    node *temp=new node;
    temp->value=pre[prel];
    int i;
    for(i=inl;i<=inr;i++)
    {
        if(in[i]==temp->value) break;
    }
    int numleft=i-inl;
    temp->lchild=pre_create(inl,i-1,prel+1,prel+numleft);
    temp->rchild=pre_create(i+1,inr,prel+1+numleft+1,prer);
    return temp;
}
node* post_create(int inl,int inr,int postl,int postr)
{
    if(postl>postr) return NULL;
    node *temp=new node;
    temp->value=post[postr];
    int i;
    for(i=inl;i<=inr;i++)
    {
        if(in[i]==temp->value) break;
    }
    int numleft=i-inl;
    temp->lchild=post_create(inl,i-1,postl,postl+numleft-1);
    temp->rchild=post_create(i+1,inr,postl+numleft,postr-1);
    return temp;
}
void pre_order(node *root)
{
    get_pre[ans++]=root->value;
    if(root->lchild!=NULL) pre_order(root->lchild);
    if(root->rchild!=NULL) pre_order(root->rchild);
}
inline int read()
{
	int w=1,x=0;
	char ch=getchar();
	while(ch>'9'||ch<'0')
	{
		if(ch=='-') w=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-48,ch=getchar();
	return w*x;
}
signed main(void)
{
    n=read();
    for(int i=1;i<=n;i++) pre[i]=read();
    for(int i=1;i<=n;i++) post[i]=read();
    for(int i=1;i<=n;i++) in[i]=i;
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值