ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【PAT甲级复习】 专题复习九:数学相关

2021-09-20 12:32:22  阅读:133  来源: 互联网

标签:right PAT 复习 int 31 30 while 甲级 left


文章目录

专题复习十(9.11):入门模拟与算法初步

1 日期处理

主要注意平年闰年

int month[13][2] = {	//第二维为0表示平年,为1表示闰年
    {0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}
};
bool isLeap(int year){
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

2 进制转换

P进制x转为10进制y:

int y = 0, product = 1;
while(x != 0){
    y = y + (x % 10) * product;
    x = x / 10;
    product = product * P;
}

10进制y转换为Q进制

int z[40], num = 0;
do{
    z[num++] = y % Q;
    y = y / Q;
}while(y != 0);

3 排序

4 散列

平方探测法的k只在 [0,Tsize) 范围内寻找即可

5 快速幂

递归写法:

typedef long long LL;
LL binaryPow(LL a, LL b, LL m){
    if(b == 0) return 1;
    if(b & 1) return a * binaryPow(a, b-1, m) % m;
    else{
        LL mul = binaryPow(a, b/2, m);
        return mul * mul % m;
    }
}

6 归并排序

void merge(int A[], int L1, int R1, int L2, int R2){
    int i = L1, j = L2;
    int temp[MAXN], index = 0;
    while(i <= R1 && j <= R2){
        if(A[i] <= A[j]){
            temp[index++] = A[i++];
        }
        else{
            temp[index++] = A[j++];
        }
    }
    while(i <= R1) temp[index++] = A[i++];
    while(j <= R2) temp[index++] = A[j++];
    for(i=0; i<index; i++){
        A[L1+i] = temp[i];
    }
}


void mergeSort(int A[]){
    for(int step = 2; step / 2 <= n; step *= 2){
        for(int i=1; i<=n; i+=step){
            sort(A+i, A+min(i+step, n+1));
        }
        //此处可以输出每一趟结束的序列
    }
}

7 快速排序

int Partition(int A[], int left, int right){
    int p = round(1.0 * rand() / RAND_MAX * (right-left) + left);
    swap(A[p], A[left]);
    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 quickSort(int A[], int left, int right){
    if(left < right){
        int pos = Partition(A, left, right);
        quickSort(A, left, pos - 1);
        quickSort(A, pos + 1, right);
    }
}

标签:right,PAT,复习,int,31,30,while,甲级,left
来源: https://blog.csdn.net/weixin_43992003/article/details/120389977

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有