< 算法笔记(晴神宝典) - 读书笔记 >

时间:2020-01-09 14:41:45   收藏:0   阅读:523

算法笔记

第二章:C++快速入门

2.5 数组

2.7 指针

2.8 结构体

2.9 补充

const double eps = 1e-8;
const double Pi = acos(-1.0);

# define Equ(a,b) ((fabs((a)-(b)))<(eps))   // ==
# define More(a,b) (((a)-(b))>(eps))    // > 
# define Less(a,b) (((a)-(b))<(-eps))    // < 
# define More(a,b) (((a)-(b))>(-eps))    // >= 
# define More(a,b) (((a)-(b))<(eps))    // <= 

2.10 黑盒测试


while(scanf("%d %d",&a,&b) != EOF)
while(scanf("%s",str) != EOF)
while(gets(str) != NULL)

第四章 入门篇 - 算法初步

4.1 排序

4.1.1 选择排序

void selectSort(){
    for(int i = 1;i <= n; i++){ //进行n趟操作
        int k = i;
        for (int j =i; j <= n; j++){    //选出[i,n]中最小元素,下标为k
            if(A[j] < A[k])
                k = j;
        }
        int temp = A[i];
        A[i] = A[k];
        A[k] = temp;
    }
}

4.1.2 插入排序

int A[maxn], n;     //n为元素个数,数组下标为1-n
void insertSort(){
    for(int i = 2; i <= n; i++){    //进行n-1趟排序
        int temp = A[i], j = i;     //temp临时存放A[i],j从i开始往前枚举
        while(j > 1 && temp < A[j-1]){  //只要temp小于前一个元素A[j-1]
            A[j] = A[j-1];      //则把A[j-1]后移一位
            j--;
        }
        A[j] = temp;
    }
}

4.1.3 排序题与sort函数应用

int a[6] = {5,6,4,1,3,2}
sort(a,a+4) //将a[0]~a[3]进行排序
sort(a,a+6) //将a[0]~a[5]进行排序
bool cmp(int a, int b){     // 实现int类型从大到小排序
    return a>b;     // 返回结果为True时,a在b前排序;否则b在a前排序
}

struct node {
    int x,y;
}ssd[10];

bool cmp(node a, node b){   //实现对结构体的排序
    return a.x > b.x    // True则ab,false则ba  - 一级排序
}
bool cmp(node a, node b){   // 二级排序
    if(a.x != b.x)  return a.x > b.x;   //x不等时按x从大到小排序
    else return a.y < b.y;  //x相等时按y从小到大排序
}
// strcmp(s1,s2)应用
bool cmp(student a, student b){
    if(a.score != b.score)  return a.score > b.score;
    else return strcmp(a.name, b.name) < 0;     //名字字典序小的在前
}

// 并列排名问题 1 - 需要存储
stu[0] = 1;     //有序数组
for(int i = 1; i<n; i++){
    if(stu[i].score == stu[i-1].score)
        stu[i].rank = stu[i-1].rank;
    else stu[i].rank = i + 1;
}

// 并列排名问题 2 - 直接输出
r = 1;
for(int i  = 0;i < n;i++){
    if(i>0 && stu[i].score != stu[i-1].score )
        r = i+1;
    //  输出信息 or stu[i].rank = r;
}

4.2 散列

4.2.1 散列定义与整数散列

4.2.2 字符串hash初步

// A~Z
int hashFunc(char S[], int len){
    int id = 0;
    for(int i = 0; i < len; i++){
        id = id * 26 + (S[i]-'A')
    }
    return id;
}

// A~Za~z
int hashFunc(char S[], int len){
    int id = 0;
    for(int i = 0; i < len; i++){
        if(S[i]<='Z' && S[i]>='A')      // Z~a之间在ascll上不是相连的
            id = id * 52 + (S[i]-'A');
       else if(S[i]<='z' && S[i]>='a')
            id = id * 52 + (S[i]-'a') + 26;
    }
    return id;
}

// 假设A~Z且最后一位是数字
// A~Z
int hashFunc(char S[], int len){
    int id = 0;
    for(int i = 0; i < len; i++){
        id = id * 26 + (S[i]-'A');
    }
    id = id*10 + (S[i]-'0');
    return id;
}

4.3 递归

4.4 贪心

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 110;
struck Inteval {
    int x,y;    // 开区间左右端点
} I[maxn];

bool cmp(Inteval a, Inteval b){
    if(a.x != b.x) return a.x > b.x;
    else return a.y < b.y;
}

int main(){
    int n;
    while(scanf("%d", &n), n != 0){
        for(int i = 0; i<n; i++)
            scanf("%d%d",&I[i].x, &I[i].y);
        sort(I, I+n ,cmp);  //区间端点排序
        
        //ans记录不相交区间个数,lastX几里路上一个被选中区间的左端点
        int ans = 1, lastX = I[0].x;
        for(int i =1; i<n; i++){
            if(I[i].y <= lastX){        //如果该区间右端点在lastX左边,表示没有重合
                lastX = I[i].x;     //以I[i]作为新选中区间
                ans++;  //不相交区间个数+1
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

4.5 二分

4.5.1 二分查找(整数二分)

// 序列中是否存在满足条件的元素 
int binarySearch(int A[], int left, int right, int x){
    int mid;    // mid为left和right中点
    while(left <= right){
        mid = left+ (right - left) / 2;
        if(A[mid] == x) return mid;
        else if(A[mid] > x)
            right = mid - 1;
        else left = mid + 1;
    }
    return -1;
}

// 寻找有序序列第一个满足某条件元素位置
int solve(int left, int right){     //左闭右闭写法
    int mid;
    while(left < right){        // 若左开右闭,则 left+1<right
        mid = left + (right - left)/2;
        if( 条件成立 ){
            right = mid;
        } else left = mid +1;       // 若左开右闭,则 left=mid
    }
    return left;    // 左开右闭,return right
}

4.5.2 二分法拓展

4.5.3 快速幂

// 快速幂递归写法
typedef long long LL;
LL binaryPow(LL a, LL b){
    if(b == 0) return 1;
    if(b & 1) return a * binaryPow(a,b-1);
    else{
        LL mul = binaryPow(a,b/2)     // 不能同时调用两个binaryPow,时间复杂度指数倍
        return mul * mul;
    }
}

// 快速幂迭代写法
LL binaryPow(LL a, LL b){
    LL ans = 1;
    while(b > 0){
        if(b & 1)
            ans = ans * a;
        a = a* a;
        b >>= 1;
    }
    return ans;
}

4.6 two pointers

4.6.1 what‘s two pointers

原文:https://www.cnblogs.com/ymjun/p/12171132.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!