• 算法设计作业


    7-8 python

    def quicksort(arr):
        if len(arr) <= 1:
            return arr
        pivot = arr[len(arr) // 2]
        left = [x for x in arr if x < pivot]
        middle = [x for x in arr if x == pivot]
        right = [x for x in arr if x > pivot]
        return quicksort(left) + middle + quicksort(right)
    
    
    # 测试代码
    n = int(input())
    arr = []
    for i in range(n):
        x = int(input())
        arr.append(x)
    arr = quicksort(arr)
    for index,a in enumerate(arr):
        if index == len(arr) - 1:
            print(f'{a}')
            break
        print(f'{a}', end=' ')
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    7-9 c++

    #include
    #include
    int a[110];
    int n;
    void merge(int a[],int tem[],int p,int q,int r)
    {
        int i=p;int j=q+1;int pos=p;
        while(i<=q&&j<=r){
            if(a[i]
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    7-10 c++

    #include
    using namespace std;
    const int N = 1010;
    int f[N];
    int v[N], w[N];
    int n, m;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
    
        cin >> m >> n;
        for (int i = 1; i <= n; i ++ ) cin >> v[i] >> w[i];
    
        for (int i = 1; i <= n; i++) {
            for (int j = m; j >= v[i]; j--) {
                f[j] = max(f[j], f[j - v[i]] + w[i]);
            }
        }
    
        cout << f[m] << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    7-11 python

    def matrix_chain_order(p):
        n = len(p) - 1 
        m = [[0 for x in range(n + 1)] for y in range(n + 1)]
    
    
        for L in range(2, n + 1):
            for i in range(1, n - L + 2):
                j = i + L - 1
                m[i][j] = float('inf')
                for k in range(i, j):
                    q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]
                    if q < m[i][j]:
                        m[i][j] = q
    
        return m[1][n]
    
    
    if __name__ == "__main__":
        n = int(input())
        dims = list(map(int, input().split()))
        print(matrix_chain_order(dims))
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    7-12 c++

    #include 
    #include 
    #include 
    using namespace std;
    void LCS(string s1,string s2)
    {
        int m=s1.length()+1;
        int n=s2.length()+1;
        int **c;
        int **b;
        c=new int* [m];
        b=new int* [m];
        for(int i=0;i=c[i+1][j])
                {
                    c[i+1][j+1]=c[i][j+1];
                    b[i+1][j+1]=2;          //2表示箭头向  上
                }
                else
                {
                    c[i+1][j+1]=c[i+1][j];
                    b[i+1][j+1]=3;          //3表示箭头向  左
                }
            }
        }
        stack same;                   //存LCS字符
        stack same1,same2;             //存LCS字符在字符串1和字符串2中对应的下标,方便显示出来
        for(int i = m-1,j = n-1;i >= 0 && j >= 0; )
        {
            if(b[i][j] == 1)
            {
                i--;
                j--;
                same.push(s1[i]);
                same1.push(i);
                same2.push(j);
            }
            else if(b[i][j] == 2)
                    i--;
                 else
                    j--;
        }
        for(int i=0;i
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
  • 相关阅读:
    目标5000万日活,Pwnk欲打造下一代年轻人的“迪士尼乐园”
    逮到一个阿里 10 年老 测试开发,聊过之后收益良多...
    wav文件碎片多删除后恢复案例
    作用域理解
    Win10 基于Docker使用tensorflow serving部署模型
    使用接口包装器模块简化在FPGA上实现PCIe的过程
    在 K8s 集群上部署 RabbitMQ 实战
    [netty-websocket-boot-starter] 轻量、高性能的WebSocket框架
    Dynamics 365 Environment Variables(环境变量)的应用
    SonarQube中的Quality Gate失败不会使Teamcity中的构建失败
  • 原文地址:https://blog.csdn.net/Young_Naive/article/details/133979915