其他分享
首页 > 其他分享> > 数据结构习题

数据结构习题

作者:互联网

01-复杂度1 最大子列和问题

 
#include <stdio.h>
int maxss(int* N, int len);
int main()
{
    int k;
    scanf("%d\n", &k);
    int i;
    int N[k];
    for (i=0;i<k;i++){
        scanf("%d", &N[i]);
    }
    printf("%d\n", maxss(N, k));
    return 0;
}

int maxss(int* N, int len){
    int ret = 0;
    int temp = 0;
    int i;
    for (i=0;i<len;i++){
        temp += N[i];
        if(temp<0)temp=0;
        if(temp>ret)ret=temp;
    }
    return ret;
}

01-复杂度2 Maximum Subsequence Sum

#include <stdio.h>
void maxss(int* ret, int len1, int* N, int len2);
int main()
{
    int k;
    scanf("%d\n", &k);
    int i;
    int N[k];
    for (i=0;i<k;i++){
        scanf("%d", &N[i]);
    }
    int ret[3];
    maxss(ret, 3, N, k);
    for (i=0;i<3;i++){
        printf("%d", ret[i]);
        if ( i!=2 ){
            printf(" ");
        }
        else printf("\n");
    }
    return 0;
}

void maxss(int* ret, int len1, int* N, int len2){
    int sum = -1;
    int label = 0;
    int first = 0;
    int last = len2 - 1;
    int firTemp = 0;
    int temp = 0;
    int i;
    for (i=0;i<len2;i++){
           if (label==0 && temp==0 && N[i]>=0){
            firTemp = i;
            if(N[i]==0) label=1;
        }
        temp += N[i];
        if (temp == 0)label=1;
        if(temp<0){
            temp=0;
            label=0;    
        }
        if(N[i]>=0 && temp>sum){
            sum = temp;
            first = firTemp;
            last = i;
        }
    }
    if(sum<0)sum=0;
    ret[0] = sum;
    ret[1] = N[first];
    ret[2] = N[last];
    return;
}

01-复杂度3 二分查找

Position BinarySearch( List L, ElementType X )
{
    int start=1, end=L->Last, middle;
    while(end>=start){
        middle = (start+end)/2;
        if (L->Data[middle]==X){
            return middle;
        }else if (L->Data[middle]>X){
            end = middle-1;
        }else{
            start = middle + 1;
        }
    }
    return NotFound;
}

02-线性结构2 一元多项式的乘法与加法运算

#include <stdio.h>

typedef struct PolyNode* Polynomial;
struct PolyNode{
    int coef;
    int expon;
    Polynomial link;
};
Polynomial ReadPoly();
void Attach(int c, int e, Polynomial *pRear);
Polynomial Add(Polynomial P1, Polynomial P2);
Polynomial Muti( Polynomial P1, Polynomial P2);
void PrintPoly(Polynomial P);

int main()
{
    Polynomial P1 = ReadPoly(), P2 = ReadPoly();
    Polynomial sum = Add(P1, P2);
    Polynomial product = Muti(P1, P2);
    PrintPoly(product);
    PrintPoly(sum);
    return 0;
}

Polynomial ReadPoly()
{
    Polynomial P, Rear,t;
    int c, e, N;
    
    scanf("%d", &N);
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    Rear = P;
    while ( N-- ){
        scanf("%d %d", &c, &e);
        Attach(c, e, &Rear);
    } 
    t = P;
    P = P->link;
    free(t);
    return P;
}

void Attach(int c, int e, Polynomial *pRear)
{
    Polynomial P;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->coef = c;
    P->expon = e;
    P->link = NULL;
    (*pRear)->link = P;
    *pRear = P;
}

Polynomial Add(Polynomial P1, Polynomial P2)
{
    Polynomial t1=P1,t2=P2;
    Polynomial t, P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link = NULL;
    Polynomial Rear = P;
    while ( t1&&t2 ){
        if (t1->expon == t2->expon){
            if (t1->coef+t2->coef){
                Attach(t1->coef+t2->coef, t1->expon, &Rear);
            }
            t1 = t1->link;
            t2 = t2->link;
        }
        else if (t1->expon > t2->expon){
            Attach(t1->coef, t1->expon, &Rear);
            t1 = t1->link;
        }
        else {
            Attach(t2->coef, t2->expon, &Rear);
            t2 = t2->link;
        }    
    }
    while (t1){
        Attach(t1->coef, t1->expon, &Rear);
        t1 = t1->link;
    }
    while (t2){
        Attach(t2->coef, t2->expon, &Rear);
        t2 = t2->link;
    }
    t = P;
    P = P->link;
    free(t);
    return P;
}

Polynomial Muti( Polynomial P1, Polynomial P2)
{
    Polynomial P, Rear,t1,t2,t;
    int c, e;
    
    if (!P1||!P2) return NULL;
    
    t1=P1;t2=P2;
    P = (Polynomial)malloc(sizeof(struct PolyNode));
    P->link =NULL;
    Rear = P;
    while(t2){
        Attach(t1->coef*t2->coef, t1->expon+t2->expon, &Rear);
        t2 = t2->link;
    }
    t1 = t1->link;
    while (t1){
        t2 = P2;Rear = P;
        while (t2){
            e = t1->expon+t2->expon;
            c = t1->coef*t2->coef;
            while (Rear->link&&Rear->link->expon>e){
                Rear = Rear->link;
            }
            if (Rear->link&&Rear->link->expon==e){
                if (Rear->link->coef+c){
                    Rear->link->coef+=c;
                }
                else{
                    t = Rear->link;
                    Rear->link = t->link;
                    free(t);
                }
            }
            else {
                t = (Polynomial)malloc(sizeof(struct PolyNode));
                t->coef = c;
                t->expon = e;
                t->link = Rear->link;
                Rear->link = t;
                Rear = Rear->link;
            }
            t2 = t2->link;
        }
        t1 = t1->link;
    }
    t2 = P;P= P->link;free(t2);
    return P;
}

void PrintPoly(Polynomial P)
{
    int flag = 1;
    if(!P){
        printf("0 0\n");
        return;
    }
    while (P){
        if (flag) 
            flag=0;
        else 
            printf(" ");
        printf("%d %d",P->coef, P->expon);
        P = P->link;
    }
    printf("\n");
}

03-树1 树的同构

#include <stdio.h>

typedef struct Node * BinTree;
struct Node{
    char data;
    char left;
    char right;
};

void addNode(BinTree*, int, char, char, char);
int isSameStruct(BinTree*, int, BinTree*, int);
char son(BinTree* bt,int index,int dir);

int main()
{
    int i, N1, N2;
    char data, left, right;
    scanf("%d\n", &N1);
    BinTree BinTree1[N1];
    for (i=0;i<N1;i++){
        scanf("%c %c %c\n", &data, &left, &right);
        addNode(BinTree1, i, data, left, right);
    }
    scanf("%d\n", &N2);
    BinTree BinTree2[N2];
    for (i=0;i<N2;i++){
        scanf("%c %c %c\n", &data, &left, &right);
        addNode(BinTree2, i, data, left, right);
    }
    if (isSameStruct(BinTree1, N1, BinTree2, N2)){
        printf("Yes");
    } else {
        printf("No");
    }
    return 0;
}
void addNode(BinTree* bt,int index,char data,char left,char right)
{
    BinTree t = (BinTree)malloc(sizeof(struct Node));
    t->data = data;
    t->left = left;
    t->right = right;
    bt[index] = t;
}
int isSameStruct(BinTree* BinTree1,int N1,BinTree* BinTree2,int N2)
{
    if (N1 != N2) return 0;
    int i, j, find=0;
    for ( i=0;i<N1;i++){
        for ( j=0;j<N2;j++ ){
            if (BinTree1[i]->data == BinTree2[j]->data){
                find = 1;
                if (son(BinTree1, i, 0)!=son(BinTree2, j, 0) && 
                son(BinTree1, i, 0)!=son(BinTree2, j, 1)){
                    return 0;
                }else if (son(BinTree1, i, 1)!=son(BinTree2, j, 0) && 
                son(BinTree1, i, 1)!=son(BinTree2, j, 1)){
                    return 0;
                }
            }
        }
        if (!find) return 0;
        find = 0;
    }
    return 1;
}
char son(BinTree* bt,int index,int dir)//左儿子dir为0,右儿子为1 
{
    if (!dir){
        if (bt[index]->left == '-'){
            return 0;
        }else{
            return bt[bt[index]->left - '0']->data;
        }
    }else{
        if (bt[index]->right == '-'){
            return 0;
        }else{
            return bt[bt[index]->right - '0']->data;
        }
    }
}

 

 

标签:int,t2,t1,link,Polynomial,习题,数据结构,Rear
来源: https://www.cnblogs.com/thewayof10/p/16683882.html