Error[8]: Undefined offset: 277, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 114
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

目录

初级阶段

week1-day1作业

week1-day2作业

week1-day3作业

week2-day4作业1

week2-day5作业

week2-day6作业

 week3-day7作业1

week3-day7作业2

week3-day8作业

week3-day9作业

week4-day10作业

week4-day11作业

 week4-day12作业

中级阶段

中级-day1

 中级-day2作业

中级day3作业

中级-day4作业

 中级-day5作业​

中级-day6作业

中级-day7作业

中级-day8作业

中级-day9作业

中级-day10作业

中级day11-作业


初级阶段 week1-day1作业

#include 

int main()
{
    printf("hello wangdao\n");
    
    return 0;
}
week1-day2作业

#define _CRT_SECURE_NO_WARNINGS //解决scanf编译报错问题(考试不需要写)
#include

int main()
{
    int a,b;
    scanf("%d%d", &a,&b);//一定要在变量前加取地址符&
    printf("%d\n", a+b);
}

week1-day3作业

#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
    int a;
    scanf("%d", &a);
    if (65 < a < 122)
        printf("%c", a);
    return 0;
} 

week2-day4作业1

#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
    int year;
    scanf("%d", &year);
    if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)//能被4整除但不能被100整除或者能被400整除
    {
        printf("yes\n");

    }
    else {
        printf("no\n");
    }

}

week2-day5作业

#define _CRT_SECURE_NO_WARNINGS
#include
//一个scanf读多种类型的数据
//混合输入时每次在%c之前需要加入一个空格
int main()
{
    int i;
    char j;
    float k;
    scanf("%d %c%f", &i, &j, &k);
    //printf("%.2f",(float)i+(float)j+k);
    printf("%0.2f\n", i + j + k);
    return 0;
}

week2-day6作业

 #define _CRT_SECURE_NO_WARNINGS
#include
//把最初输入的整型数a,反过来后,再存到另外一个整形数b,判断a和b是否相等,如果相等就输出yes,不等就输出no
int main() 
{
    int a,b,c,tmp;
    while (scanf("%d", &a) != EOF)
    {
        b = 0;
        c = a;
        while (a)
        {
            tmp = a % 10;
            b = b * 10 + tmp;
            a = a / 10;
        }
        if (c == b)
        {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }
    }
    return 0;
}

 week3-day7作业1

//while循环

 #define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
    int n;
    int i = 1;
    int total = 1;//存储最终的和
    scanf("%d", &n);
    while (i <= n)//while后面不能加分号,否则会进入死循环
    {
        total = total * i;
        i++;
    }
    printf("%d\n", total);
}

 //for循环

#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
    int n;
    int i, total;
    scanf("%d", &n);//读取输入
    //for语句中只能有两个分号
    for (i = 1, total = 1; i <= n; i++)
    {
        total = total * i;
    }
    printf("%d\n", total);
}
week3-day7作业2

#include
int main() 
{
    int i, j, k, m, sum = 0;
    for (i = 1; i <= 40; i++)
    {
        for (j = 1; j <= 40; j++)
        {
            for (k = 1; k <= 40; k++)
            {
                for (m = 1; m <= 40; m++)
                {
                    if (i + j + k + m == 40 && 10 * i + 5 * j + k + m * 2 == 100)
                    {
                        sum++;
                    }
                }
            }
        }
    }
    printf("%d", sum);
    return 0;
    
} 
week3-day8作业

 #define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
    int a[100], n, k = 0;
    scanf("%d", &n);//接下来要输入多少个元素
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);//如何往数组里元素读取数据
        if (a[i] == 2)
        {
            k++;
        }
    }
    printf("%d\n",k);
}
week3-day9作业

 #define _CRT_SECURE_NO_WARNINGS
#include
#include
int main()
{
    char a[20];
    char b[20];
    int len;
    gets(a);
    len = strlen(a);//读取数组a的长度
    for (int i = 0; i < len; i++)
    {
        b[i] = a[len - i - 1];//将a数组的下标存的数据赋值给逆转后b数组的下标
    }
    b[len] = '< 0)
    {
        printf("-1");
    }
    else
    {
        printf("0");
    }
}';//在b数组中,数据结束后添加结束符
    if (strcmp(a, b) > 0)//判断原字符串是否大于逆转后的字符串
    {
        printf("1");
    }
    else if (strcmp(a, b) 

week4-day10作业

 
#define _CRT_SECURE_NO_WARNINGS
#include

void change(int *j)//j称为形参,j=&i
{
    *j = *j/2;//指针的间接访问
}

int main()
{
    int i;//i是局部变量
    scanf("%d",&i);
    change(&i);//函数调用时,把&i称为实参
    printf("%d\n", i);
    return 0;
}

week4-day11作业

 
 
#define _CRT_SECURE_NO_WARNINGS
#include
//malloc可以帮我们实现动态数组
int main()
{
    int i;//申请多大的空间
    scanf("%d", &i);
    char* p;
    p = malloc(i);//malloc申请空间的单位是字节
    char c;
    scanf("%c", &c);//为了去除缓冲区里边的\n
    gets(p);//如果不通过上面的scanf去消除\n,gets不会卡住
    puts(p);
    return 0;
}

 week4-day12作业

 
#define _CRT_SECURE_NO_WARNINGS
#include 

int step(int n)
{
    if (1 == n || 2 == n)//递归的结束条件
    {
        return n;
    }
    return step(n - 1) + step(n - 2);//递归公式
}

int main()
{
    int n;//存储台阶
    scanf("%d", &n);
    printf("%d\n", step(n));
    return 0;
}

中级阶段 中级-day1

 
#define _CRT_SECURE_NO_WARNINGS
#include

//结构体指针
struct student
{
    int num;
    char name[20];
    char sex;
};//结构体类型声明,注意最后一定要加分号
int main()
{
    struct student sarr[1];
    int i=0;
        scanf("%d%s %c", &sarr[i].num, sarr[i].name, &sarr[i].sex);
        printf("%d %s %c\n", sarr[i].num, sarr[i].name, sarr[i].sex);
    
    return 0;
}

 中级-day2作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include
void modify_pointer(char*&p) {
    p = (char*)malloc(100);
    fgets(p, 100, stdin);//如果使用fgets传入的是一个指针变量,中间参数是指针指向的空间大小

}
int main() {
    char* p;
    modify_pointer(p);
    puts(p);
    return 0;
}

中级day3作业

 
 
#define _CRT_SECURE_NO_WARNINGS
#include
#include<1 || i>

#define MaxSize 50
typedef int ElemType;//顺序表中元素的类型
//静态分配
typedef struct {
    ElemType data[MaxSize];//定义的数组,用来存元素
    int length;//当前顺序表中有多少个元素
}SqList;

//i代表插入的位置,从1开始,e要插入的元素
bool ListInsert(SqList& L, int i, ElemType e)
{
    if (i<1 || i>L.length + 1)//判断要插入的位置是否合法
        return false;
    if (L.length >= MaxSize)//超出了空间,元素存储满了,不能再存了
        return false;
    for (int j = L.length; j >= i; j--)//移动顺序表中的元素
        L.data[j] = L.data[j - 1];
    L.data[i - 1] = e;//数组下标从零开始,插入第一个位置,访问的下标为零
    L.length++;
    return true;//插入成功,返回true
}
//删除使用元素e的引用的目的是拿出对应的值
bool ListDelete(SqList& L, int i, ElemType& e)
{
    if (i< L.length; j++)//从i的位置依次把元素往前覆盖
        L.data[j - 1] = L.data[j];
    L.length--;
    return true;//删除一个元素,顺序表长度减1
}
//打印顺序表元素
void PrintList(SqList& L)
{
    for (int i = 0; i < L.length; i++)
    {
        printf("%3d", L.data[i]);//要求所有元素打印到一排
    }
    printf("\n");
}
//要不要加引用,就是看是不是在子函数中去改变主函数中对应的变量,要改就要加
int main()
{
    
    SqList L;//顺序表的名称
    bool ret;//查看返回值,布尔型是true,或者false
    ElemType del;//用来存要删除的元素
    //首先手动在顺序表中赋值
    L.data[0] = 1;
    L.data[1] = 2;
    L.data[2] = 3;
    L.length = 3;//总计三个元素
    int i;
    scanf("%d\n", &i);
    ret = ListInsert(L, 2, i);//往第二个位置插入6这个元素
    if (ret)
    {
        PrintList(L);//打印成功后的顺序表
    }
    else {
        printf("插入失败\n");
    }
    int k;
    scanf("%d\n", &k);
    ret = ListDelete(L,k,del);//删除第一个位置的元素,把元素输出
    if (ret)
    {
        PrintList(L);
    }
    else {
        printf("false\n");
    }
    return 0;
}L.length)//如果删除的位置是不合法
        return false;
    if (L.length == 0)//顺序表中没有元素,无需删除
    {
        return false;
    }
    e = L.data[i - 1];//获取顺序表中对应的元素,赋值给e
    for (int j = i; j 

中级-day4作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include
typedef int ElemType;
typedef struct LNode {
    ElemType data;
    struct LNode* next;//指向下一个节点
}LNode,*LinkList;
//头插法新建链表
LinkList CreatList1(LinkList& L)//list_head_insert
{
    LNode* s; int x;
    L = (LinkList)malloc(sizeof(LNode));//带头结点的链表
    L->next = NULL;//L->data里边没放东西
    scanf("%d", &x);//从标准输入读取数据
    //3 4 5 6 9999
    while (x != 9999) {
        s = (LNode*)malloc(sizeof(LNode));//申请一个新空间给s,强制类型转换
        s->data = x;//把读取到的值,给新空间中的data成员
        s->next = L->next;//让新结点的next指针指向链表的第一个元素(第一个放我们数据的元素)
        L->next = s;//让s作为第一的元素
        scanf("%d", &x);//读取标准输入
    }
    return L;
}
//尾插法新建链表
LinkList CreatList2(LinkList &L)//list_tail_insert
{
    int x;
    L = (LinkList)malloc(sizeof(LNode));//带头结点的链表
    LNode* s, * r = L;//LinkList s,r=L;也可以,r代表链表表尾结点,指向链表尾部
    //3 4 5 6 9999
    scanf("%d\n", &x);
    while (x!=9999)
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = x;
        r->next = s;//让尾部节点指向新节点
        r = s;//r指向新的表尾结点
        scanf("%d\n", &x);
    }
    r->next = NULL;//尾结点的next指针赋值为null
    return L;
    
}
//打印链表中每个结点的值
void PrintList(LinkList L)
{
    L = L->next;
    while (L != NULL)//NULL是为了代表一张空的藏宝图
    {
        printf("%3d", L->data);//打印当前节点数据
        L = L->next;//指向下一个节点
        if (L != NULL)
        {
            printf(" ");
        }
    }
    printf("\n");
}
int main()
{
    LinkList L;
    LinkList search;
    CreatList1(L);//输入数据可以为3 4 5 6 7 9999,头插法新建链表
    PrintList(L);//链表打印
    CreatList2(L);//输入数据可以为3 4 5 6 7 9999,尾插法新建链表
    PrintList(L);
    return 0;
} 
 
 中级-day5作业 
#define _CRT_SECURE_NO_WARNINGS
#include
#include< 1)
        return NULL;
    while (p&&j
typedef int ElemType;
typedef struct LNode {
    ElemType data;
    struct LNode* next;//指向下一个节点
}LNode,*LinkList;
//尾插法建立链表
LinkList CreatList(LinkList& L)//list_tail_insert
{
    int x;
    L = (LinkList)malloc(sizeof(LNode));//带头结点的链表
    LNode* s, * r = L;//LinkList s,r=L;也可以,r代表链表表尾结点,指向链表尾部
    //3 4 5 6 7 9999
    scanf("%d", &x);
    while (x != 9999) {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = x;
        r->next = s;//让尾部节点指向新节点
        r = s;//r指向新的尾部节点
        scanf("%d", &x);
    }
    r->next = NULL;
    return L;
}
//按序号查找结点值
LNode* GetElem(LinkList L, int i)
{
    int j = 1;
    LNode* p = L->next;
    if (i == 0)
        return L;
    if (i next;
        j++;
    }
    return p;
}
//新结点插入第i个位置
bool LinstFrontInsert(LinkList L, int i, ElemType e)
{
    LinkList p = GetElem(L, i - 1);
    if (NULL == p)
    {
        return false;
    }
    LinkList s = (LNode*)malloc(sizeof(LNode));//为新插入的节点申请空间
    s->data = e;
    s->next = p->next;
    p->next = s;
    return true;
}
//删除第i个节点
bool ListDelete(LinkList L, int i)
{
    LinkList p = GetElem(L, i - 1);
    if (NULL == p)
    {
        return false;
    }
    LinkList q;
    q = p->next;
    p->next = q->next;//断链
    free(q);//释放对应节点的空间
    return true;
}
//打印链表中每个节点的位置
void PrintList(LinkList L)
{
    L = L->next;
    while (L!=NULL)
    {
        printf("%3d", L->data);
        L = L->next;//指向下一个节点
    }
    printf("\n");
}


int main()
{
    LinkList L;
    LinkList search;
    CreatList(L);
    //PrintList(L);
    search = GetElem(L, 2);//查找第二个位置的元素的值
    if (search != NULL)
    {
        printf("%d\n", search->data);

    }
    LinstFrontInsert(L, 2, 99);//新结点插入第i个位置
    PrintList(L);
    ListDelete(L, 4);//删除第4个节点
    PrintList(L);
}

中级-day6作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include< 5)
    {
        scanf("%d", &n1);
        EnQueue(Q, n1);
        n = n + 1;
    }
    Pop(S, m);
    Pop(S, m);
    Pop(S, m);
    printf("\n");
    if (n >
#define MaxSize 50
typedef int ElemType;
typedef struct {
    ElemType data[MaxSize];//数组
    int top;
}SqStack;
//栈初始化
void InitStack(SqStack& S)
{
    S.top = -1;//代表栈为空
}

//入栈
bool Push(SqStack& S, ElemType x)
{
    if (S.top == MaxSize - 1)//数组的大小不能改变,避免访问越界
    {
        return false;
    }
    S.data[++S.top] = x;
    return true;
}
//出栈
bool Pop(SqStack& S, ElemType& x)
{
    if (-1 == S.top)
        return false;
    x = S.data[S.top--];
    printf(" %d", x);
    return true;
}
//队列
typedef struct {
    ElemType data[5];//数组,存放MaxSize-1个元素
    int front, rear;//队列头,队列尾
}SqQueue;
//队列初始化
void InitQueue(SqQueue& Q)//初始化队列
{
    Q.rear = Q.front = 0;
}
//入队
bool EnQueue(SqQueue& Q, ElemType x)
{
    if ((Q.rear + 1) % MaxSize == Q.front)//判断是否队满
        return false;
    Q.data[Q.rear] = x;//3 4 5 6 7
    Q.rear = (Q.rear + 1) % MaxSize;
    return true;
}
//出队
bool DeQueue(SqQueue& Q, ElemType& x)
{
    if (Q.rear == Q.front)
        return false;
    x = Q.data[Q.front];//先进先出
    Q.front = (Q.front + 1) % MaxSize;
    printf(" %d", x);
    return true;
}
int main()
{
    SqStack S;//后进先出LIFO
    ElemType m;//用来存放拿出的元素
    InitStack(S);//初始化
    ElemType a, b, c;
    scanf("%d %d %d\n", &a, &b, &c);
    Push(S, a);
    Push(S, b);
    Push(S, c);
    SqQueue Q;
    ElemType element;//存储出队元素
    InitQueue(Q);//初始化队列
    //入队
    int n = 1;
    int n1;
    while (n  4)printf("false\n");
    for(int i=1;i

中级-day7作业

 
 
#define _CRT_SECURE_NO_WARNINGS
#include
#include
typedef int ElemType;
typedef char BiElemType;
typedef struct BiTNode {
    BiElemType c;//C就是书籍上的data
    struct BiTNode* lchild;
    struct BiTNode* rchild;
}BiTNode, * BiTree;


typedef struct tag {
    BiTree p;
    struct tag* pnext;
}tag_t, * ptag_t;
//队列的相关数据结构
typedef struct LinkNode {
    ElemType data;
    struct LinkNode* next;
}LinkNode;
typedef struct {
    LinkNode* front, * rear;
}LinkQueue;
void InitQueue(LinkQueue& Q);
bool IsEmpty(LinkQueue Q);
void EnQueue(LinkQueue& Q, ElemType x);
bool DeQueue(LinkQueue& Q, ElemType& x);


//带头结点的队列
void InitQueue(LinkQueue& Q)
{
    Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
    Q.front->next = NULL;
}

bool IsEmpty(LinkQueue Q)
{
    if (Q.front == Q.rear)
        return true;
    else
        return false;
}

void EnQueue(LinkQueue& Q, ElemType x)
{
    LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
    s->data = x; s->next = NULL;
    Q.rear->next = s;
    Q.rear = s;
}

bool DeQueue(LinkQueue& Q, ElemType& x)
{
    if (Q.front == Q.rear) return false;
    LinkNode* p = Q.front->next;//头结点什么都没存,所以头结点的下一个节点才有数据
    x = p->data;
    Q.front->next = p->next;
    if (Q.rear == p)
        Q.rear = Q.front;
    free(p);
    return true;
}


//前序遍历
void preOrder(BiTree p)
{
    if (p != NULL)
    {
        putchar(p->c);//等价于visit函数
        preOrder(p->lchild);
        preOrder(p->rchild);
    }
}

int main()
{
    BiTree pnew;
    int i, j, pos;
    char c;
    BiTree tree = NULL;//树根
    ptag_t phead = NULL, ptail = NULL, listpnew = NULL, pcur = NULL;//phead是队列头,ptail是队列尾
    while (scanf("%c", &c) != EOF)
    {
        if (c == '\n')
        {
            break;
        }
        pnew = (BiTree)calloc(1, sizeof(BiTNode));//calloc申请空间并对空间进行初始化,赋值为0
        pnew->c = c;//数据放进去
        listpnew = (ptag_t)calloc(1, sizeof(tag_t));//给队列结点申请空间
        listpnew->p = pnew;
        if (NULL == tree)
        {
            tree = pnew;//树的根
            phead = listpnew;//队列头
            ptail = listpnew;//队列尾
            pcur = listpnew;
            continue;
        }
        else
        {
            ptail->pnext = listpnew;//新节点放入链表,通过尾插法
            ptail = listpnew;//ptail指向队列尾部
        }//pcur始终指向要插入的位置
        if (NULL == pcur->p->lchild)//如何把新节点放入树
        {
            pcur->p->lchild = pnew;//把新节点放到要插入结点的左边
        }
        else if (NULL == pcur->p->rchild)
        {
            pcur->p->rchild = pnew;//把新节点放到要插入结点的右边
            pcur = pcur->pnext;//左右都放了结点后,pcur指向队列的下一个
        }

    }
    preOrder(tree);
}

中级-day8作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include
typedef char BiElemType;
typedef struct BiTNode {
    BiElemType c;//c就是书籍上的data
    struct BiTNode* lchild;
    struct BiTNode* rchild;
}BiTNode, * BiTree;

typedef struct tag {
    BiTree p;//树的某一个结点的地址值
    struct tag* pnext;
}tag_t, * ptag_t;
#define MaxSize 50
typedef BiTree ElemType;
//队列的相关数据结构
typedef struct LinkNode {
    ElemType data;
    struct LinkNode* next;
}LinkNode;
typedef struct {
    LinkNode* front, * rear;
}LinkQueue;
void InitQueue(LinkQueue& Q);
bool IsEmpty(LinkQueue Q);
void EnQueue(LinkQueue& Q, ElemType x);
bool DeQueue(LinkQueue& Q, ElemType& x);
//带头结点的队列
void InitQueue(LinkQueue& Q)
{
    Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
    Q.front->next = NULL;
}

bool IsEmpty(LinkQueue Q)
{
    if (Q.front == Q.rear)
        return true;
    else
        return false;
}

void EnQueue(LinkQueue& Q, ElemType x)
{
    LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
    s->data = x; s->next = NULL;
    Q.rear->next = s;
    Q.rear = s;
}

bool DeQueue(LinkQueue& Q, ElemType& x)
{
    if (Q.front == Q.rear) return false;
    LinkNode* p = Q.front->next;//头结点什么都没存,所以头结点的下一个节点才有数据
    x = p->data;
    Q.front->next = p->next;
    if (Q.rear == p)
        Q.rear = Q.front;
    free(p);
    return true;
}

//中序遍历  hdibjeafcg
void InOrder(BiTree p)
{
    if (p != NULL)
    {
        InOrder(p->lchild);
        putchar(p->c);
        InOrder(p->rchild);
    }
}
//hidjebfgca  后序遍历
void PostOrder(BiTree p)
{
    if (p != NULL)
    {
        PostOrder(p->lchild);
        PostOrder(p->rchild);
        putchar(p->c);
    }
}

//层次遍历,层序遍历,广度优先遍历
void LevelOrder(BiTree T)
{
    LinkQueue Q;//辅助队列
    InitQueue(Q);//初始化队列
    BiTree p;
    EnQueue(Q, T);//树根入队
    while (!IsEmpty(Q))
    {
        DeQueue(Q, p);//出队当前结点并打印
        putchar(p->c);
        if (p->lchild != NULL) //入队左孩子
            EnQueue(Q, p->lchild);
        if (p->rchild != NULL)  //入队右孩子
            EnQueue(Q, p->rchild);
    }
}
int main()
{
    BiTree pnew;
    int i, j, pos;
    char c;
    BiTree tree = NULL;//树根
    ptag_t phead = NULL, ptail = NULL, listpnew = NULL, pcur = NULL;//phead就是队列头,ptail就是队列尾
    //abcdefghij
    while (scanf("%c", &c) != EOF)
    {
        if (c == '\n')
        {
            break;
        }
        pnew = (BiTree)calloc(1, sizeof(BiTNode));//calloc申请空间并对空间进行初始化,赋值为0
        pnew->c = c;//数据放进去
        listpnew = (ptag_t)calloc(1, sizeof(tag_t));//给队列结点申请空间
        listpnew->p = pnew;
        if (NULL == tree)
        {
            tree = pnew;//树的根
            phead = listpnew;//队列头
            ptail = listpnew;//队列尾
            pcur = listpnew;
            continue;
        }
        else {
            ptail->pnext = listpnew;//新结点放入链表,通过尾插法
            ptail = listpnew;//ptail指向队列尾部
        }//pcur始终指向要插入的结点的位置
        if (NULL == pcur->p->lchild)//如何把新结点放入树
        {
            pcur->p->lchild = pnew;//把新结点放到要插入结点的左边
        }
        else if (NULL == pcur->p->rchild)
        {
            pcur->p->rchild = pnew;//把新结点放到要插入结点的右边
            pcur = pcur->pnext;//左右都放了结点后,pcur指向队列的下一个
        }
    }
    InOrder(tree);
    printf("\n");
    PostOrder(tree);
    printf("\n");
    LevelOrder(tree);
    printf("\n");
}

中级-day9作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include< T->
typedef int KeyType;
typedef struct BSTNode {
    KeyType key;
    struct BSTNode* lchild, * rchild;
}BSTNode, * BiTree;
//54 20 66 40 28 79 58
int BST_Insert(BiTree& T, KeyType k)
{
    if (NULL == T)
    {
        //为新节点申请空间
        T = (BiTree)malloc(sizeof(BSTNode));
        T->key = k;
        T->lchild = T->rchild = NULL;
        return 1;//代表插入成功
    }
    else if (k == T->key)
        return 0;//发现相同元素,就不插入
    else if (k < n)
    {
        BST_Insert(T, str[i]);
        i++;
    }
}
//中序遍历
void InOrder(BiTree T, KeyType str[], int& pos)
{
    if (T != NULL)
    {
        InOrder(T->key)
        return BST_Insert(T->lchild, k);
    else
        return BST_Insert(T->rchild, k);
}

//创建二叉排序树
void Create_BST(BiTree& T, KeyType str[], int n)
{
    T = NULL;
    int i = 0;
    while (i <= high)
    {
        mid = (low + high) / 2;
        if (L.elem[mid] == key)
            return mid;
        else if (L.elem[mid] >lchild, str, pos);
        printf("%3d", T->key);
        str[pos++] = T->key;//输出的同时存入数组
        InOrder(T->rchild, str, pos);
    }
}
typedef int ElemType;
typedef struct {
    ElemType* elem;//整形指针
    int TableLen;
}SSTable;
int Binary_Search(SSTable L, ElemType key)
{
    int low = 0, high = L.TableLen - 1, mid;
    while (low < 10; i++)
    {
        scanf("%d", &str[i]);
    }
    Create_BST(T, str, 10);
    int pos = 0;
    InOrder(T, str, pos);//中序遍历把有序数组的结果存到str数组中
    printf("\n");
    SSTable L;
    L.elem = str;
    L.TableLen = 10;
    pos = Binary_Search(L, 21);
    printf("%d\n", pos);
    return 0;

} key)
            high = mid - 1;
        else
            low = mid + 1;
    }
    return -1;
}
int main()
{
    BiTree T = NULL;
    BiTree parent;//存储父亲节点的地址值
    BiTree search;
    KeyType str[10];//将要进入二叉排序树的元素值
    int i;
    for (i = 0; i 

中级-day10作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include< 9; i++)
    {
        printf("%2d", arr[i]);
    }
    return 0;
}

int main()
{
    char j;
    scanf("%c",&j);
    int i;
    int arr[9] = { 0,1,1,2,2,3,1,1,2 };
    for (i = 0; i 

中级day11-作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include 
#include< ST.TableLen; i++)
    {
        ST.elem[i] = A[i];
    }

}
void ST_print(SSTable ST)
{
    for (int i = 0; i < ST.TableLen; i++)
    {
        printf("%3d", ST.elem[i]);
    }
    printf("\n");
}
void swap(ElemType& a, ElemType& b)
{
    ElemType tmp;
    tmp = a;
    a = b;
    b = tmp;
}
void BubbleSort1(ElemType A[], int n)
{
    int i, j, flag;
    for (i = 0; i < n - 1; i++)//i是控制有多少个有序了
    {
        flag = 0;
        for (j = n - 1; j >

typedef int ElemType;
typedef struct {
    ElemType* elem;//存储元素的起始地址
    int TableLen;
}SSTable;
void ST_Init(SSTable& ST, ElemType A[],int len)
{
    ST.TableLen = len;
    ST.elem = (ElemType*)malloc(sizeof(ElemType) * ST.TableLen);
    int i;
    for (i = 0; i < right; i++)
    {
        if (arr[i] < arr[right])
        {
            swap(arr[i], arr[k]);
            k++;
        }
    }
    swap(arr[k], arr[right]);
    return k;
}


//递归实现
void QuickSort(ElemType A[], int low, int high)
{
    if (low < high)
    {
        int pivotpos = Partition(A, low, high);//分割点左边的元素都比分割点要小,右边的比分割点大
        QuickSort(A, low, pivotpos - 1);
        QuickSort(A, pivotpos + 1, high);
    }
}
void InsertSort(ElemType A[], int n)
{
    int i, j;
    for (i = 2; i < n; i++)
    {
        if (A[i] < A[i - 1])
        {
            A[0] = A[i];//放到暂存位置,A[0]既是暂存,也是哨兵
            for (j = i - 1; A[0] < A[j]; --j)//移动元素,内层循环控制有序序列中的每一个元素和要插入的元素比较
                A[j + 1] = A[j];
            A[j + 1] = A[0];//把暂存元素插入到对应位置
        }
    }
}
int main()
{
    SSTable ST;
    ElemType A[10];
    int i;
    for (i = 0; i < 10; i++)
    {
        scanf("%d", &A[i]);
    }
    ST_Init(ST,A, 10);//初始化
    BubbleSort1(ST.elem, 10);//冒泡排序
    ST_print(ST);
    QuickSort(ST.elem, 0, 9);//快速排序
    ST_print(ST);
    InsertSort(ST.elem, 10);//插入排序
    ST_print(ST);
}

  i; j--)//内层控制比较,交换
        {
            if (A[j - 1] > A[j])
            {
                swap(A[j - 1], A[j]);
                flag = 1;
            }
        }
    }
}
int Partition(int* arr, int left, int right)
{
    int k, i;
    for (k = i = left; i 

 

 

 

 

 <===>)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
王道C语言督学营oj练习(更新学习中)_C_内存溢出

王道C语言督学营oj练习(更新学习中)

王道C语言督学营oj练习(更新学习中),第1张

目录

初级阶段

week1-day1作业

week1-day2作业

week1-day3作业

week2-day4作业1

week2-day5作业

week2-day6作业

 week3-day7作业1

week3-day7作业2

week3-day8作业

week3-day9作业

week4-day10作业

week4-day11作业

 week4-day12作业

中级阶段

中级-day1

 中级-day2作业

中级day3作业

中级-day4作业

 中级-day5作业​

中级-day6作业

中级-day7作业

中级-day8作业

中级-day9作业

中级-day10作业

中级day11-作业


初级阶段 week1-day1作业

#include 

int main()
{
    printf("hello wangdao\n");
    
    return 0;
}
week1-day2作业

#define _CRT_SECURE_NO_WARNINGS //解决scanf编译报错问题(考试不需要写)
#include

int main()
{
    int a,b;
    scanf("%d%d", &a,&b);//一定要在变量前加取地址符&
    printf("%d\n", a+b);
}

week1-day3作业

#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
    int a;
    scanf("%d", &a);
    if (65 < a < 122)
        printf("%c", a);
    return 0;
} 

week2-day4作业1

#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
    int year;
    scanf("%d", &year);
    if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)//能被4整除但不能被100整除或者能被400整除
    {
        printf("yes\n");

    }
    else {
        printf("no\n");
    }

}

week2-day5作业

#define _CRT_SECURE_NO_WARNINGS
#include
//一个scanf读多种类型的数据
//混合输入时每次在%c之前需要加入一个空格
int main()
{
    int i;
    char j;
    float k;
    scanf("%d %c%f", &i, &j, &k);
    //printf("%.2f",(float)i+(float)j+k);
    printf("%0.2f\n", i + j + k);
    return 0;
}

week2-day6作业

 #define _CRT_SECURE_NO_WARNINGS
#include
//把最初输入的整型数a,反过来后,再存到另外一个整形数b,判断a和b是否相等,如果相等就输出yes,不等就输出no
int main() 
{
    int a,b,c,tmp;
    while (scanf("%d", &a) != EOF)
    {
        b = 0;
        c = a;
        while (a)
        {
            tmp = a % 10;
            b = b * 10 + tmp;
            a = a / 10;
        }
        if (c == b)
        {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }
    }
    return 0;
}

 week3-day7作业1

//while循环

 #define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
    int n;
    int i = 1;
    int total = 1;//存储最终的和
    scanf("%d", &n);
    while (i <= n)//while后面不能加分号,否则会进入死循环
    {
        total = total * i;
        i++;
    }
    printf("%d\n", total);
}

 //for循环

#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
    int n;
    int i, total;
    scanf("%d", &n);//读取输入
    //for语句中只能有两个分号
    for (i = 1, total = 1; i <= n; i++)
    {
        total = total * i;
    }
    printf("%d\n", total);
}
week3-day7作业2

#include
int main() 
{
    int i, j, k, m, sum = 0;
    for (i = 1; i <= 40; i++)
    {
        for (j = 1; j <= 40; j++)
        {
            for (k = 1; k <= 40; k++)
            {
                for (m = 1; m <= 40; m++)
                {
                    if (i + j + k + m == 40 && 10 * i + 5 * j + k + m * 2 == 100)
                    {
                        sum++;
                    }
                }
            }
        }
    }
    printf("%d", sum);
    return 0;
    
} 
week3-day8作业

 #define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
    int a[100], n, k = 0;
    scanf("%d", &n);//接下来要输入多少个元素
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);//如何往数组里元素读取数据
        if (a[i] == 2)
        {
            k++;
        }
    }
    printf("%d\n",k);
}
week3-day9作业

 #define _CRT_SECURE_NO_WARNINGS
#include
#include
int main()
{
    char a[20];
    char b[20];
    int len;
    gets(a);
    len = strlen(a);//读取数组a的长度
    for (int i = 0; i < len; i++)
    {
        b[i] = a[len - i - 1];//将a数组的下标存的数据赋值给逆转后b数组的下标
    }
    b[len] = '< 0)
    {
        printf("-1");
    }
    else
    {
        printf("0");
    }
}';//在b数组中,数据结束后添加结束符
    if (strcmp(a, b) > 0)//判断原字符串是否大于逆转后的字符串
    {
        printf("1");
    }
    else if (strcmp(a, b) 

week4-day10作业

 
#define _CRT_SECURE_NO_WARNINGS
#include

void change(int *j)//j称为形参,j=&i
{
    *j = *j/2;//指针的间接访问
}

int main()
{
    int i;//i是局部变量
    scanf("%d",&i);
    change(&i);//函数调用时,把&i称为实参
    printf("%d\n", i);
    return 0;
}

week4-day11作业

 
 
#define _CRT_SECURE_NO_WARNINGS
#include
//malloc可以帮我们实现动态数组
int main()
{
    int i;//申请多大的空间
    scanf("%d", &i);
    char* p;
    p = malloc(i);//malloc申请空间的单位是字节
    char c;
    scanf("%c", &c);//为了去除缓冲区里边的\n
    gets(p);//如果不通过上面的scanf去消除\n,gets不会卡住
    puts(p);
    return 0;
}

 week4-day12作业

 
#define _CRT_SECURE_NO_WARNINGS
#include 

int step(int n)
{
    if (1 == n || 2 == n)//递归的结束条件
    {
        return n;
    }
    return step(n - 1) + step(n - 2);//递归公式
}

int main()
{
    int n;//存储台阶
    scanf("%d", &n);
    printf("%d\n", step(n));
    return 0;
}

中级阶段 中级-day1

 
#define _CRT_SECURE_NO_WARNINGS
#include

//结构体指针
struct student
{
    int num;
    char name[20];
    char sex;
};//结构体类型声明,注意最后一定要加分号
int main()
{
    struct student sarr[1];
    int i=0;
        scanf("%d%s %c", &sarr[i].num, sarr[i].name, &sarr[i].sex);
        printf("%d %s %c\n", sarr[i].num, sarr[i].name, sarr[i].sex);
    
    return 0;
}

 中级-day2作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include
void modify_pointer(char*&p) {
    p = (char*)malloc(100);
    fgets(p, 100, stdin);//如果使用fgets传入的是一个指针变量,中间参数是指针指向的空间大小

}
int main() {
    char* p;
    modify_pointer(p);
    puts(p);
    return 0;
}

中级day3作业

 
 
#define _CRT_SECURE_NO_WARNINGS
#include
#include<1 || i>

#define MaxSize 50
typedef int ElemType;//顺序表中元素的类型
//静态分配
typedef struct {
    ElemType data[MaxSize];//定义的数组,用来存元素
    int length;//当前顺序表中有多少个元素
}SqList;

//i代表插入的位置,从1开始,e要插入的元素
bool ListInsert(SqList& L, int i, ElemType e)
{
    if (i<1 || i>L.length + 1)//判断要插入的位置是否合法
        return false;
    if (L.length >= MaxSize)//超出了空间,元素存储满了,不能再存了
        return false;
    for (int j = L.length; j >= i; j--)//移动顺序表中的元素
        L.data[j] = L.data[j - 1];
    L.data[i - 1] = e;//数组下标从零开始,插入第一个位置,访问的下标为零
    L.length++;
    return true;//插入成功,返回true
}
//删除使用元素e的引用的目的是拿出对应的值
bool ListDelete(SqList& L, int i, ElemType& e)
{
    if (i< L.length; j++)//从i的位置依次把元素往前覆盖
        L.data[j - 1] = L.data[j];
    L.length--;
    return true;//删除一个元素,顺序表长度减1
}
//打印顺序表元素
void PrintList(SqList& L)
{
    for (int i = 0; i < L.length; i++)
    {
        printf("%3d", L.data[i]);//要求所有元素打印到一排
    }
    printf("\n");
}
//要不要加引用,就是看是不是在子函数中去改变主函数中对应的变量,要改就要加
int main()
{
    
    SqList L;//顺序表的名称
    bool ret;//查看返回值,布尔型是true,或者false
    ElemType del;//用来存要删除的元素
    //首先手动在顺序表中赋值
    L.data[0] = 1;
    L.data[1] = 2;
    L.data[2] = 3;
    L.length = 3;//总计三个元素
    int i;
    scanf("%d\n", &i);
    ret = ListInsert(L, 2, i);//往第二个位置插入6这个元素
    if (ret)
    {
        PrintList(L);//打印成功后的顺序表
    }
    else {
        printf("插入失败\n");
    }
    int k;
    scanf("%d\n", &k);
    ret = ListDelete(L,k,del);//删除第一个位置的元素,把元素输出
    if (ret)
    {
        PrintList(L);
    }
    else {
        printf("false\n");
    }
    return 0;
}L.length)//如果删除的位置是不合法
        return false;
    if (L.length == 0)//顺序表中没有元素,无需删除
    {
        return false;
    }
    e = L.data[i - 1];//获取顺序表中对应的元素,赋值给e
    for (int j = i; j 

中级-day4作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include
typedef int ElemType;
typedef struct LNode {
    ElemType data;
    struct LNode* next;//指向下一个节点
}LNode,*LinkList;
//头插法新建链表
LinkList CreatList1(LinkList& L)//list_head_insert
{
    LNode* s; int x;
    L = (LinkList)malloc(sizeof(LNode));//带头结点的链表
    L->next = NULL;//L->data里边没放东西
    scanf("%d", &x);//从标准输入读取数据
    //3 4 5 6 9999
    while (x != 9999) {
        s = (LNode*)malloc(sizeof(LNode));//申请一个新空间给s,强制类型转换
        s->data = x;//把读取到的值,给新空间中的data成员
        s->next = L->next;//让新结点的next指针指向链表的第一个元素(第一个放我们数据的元素)
        L->next = s;//让s作为第一的元素
        scanf("%d", &x);//读取标准输入
    }
    return L;
}
//尾插法新建链表
LinkList CreatList2(LinkList &L)//list_tail_insert
{
    int x;
    L = (LinkList)malloc(sizeof(LNode));//带头结点的链表
    LNode* s, * r = L;//LinkList s,r=L;也可以,r代表链表表尾结点,指向链表尾部
    //3 4 5 6 9999
    scanf("%d\n", &x);
    while (x!=9999)
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = x;
        r->next = s;//让尾部节点指向新节点
        r = s;//r指向新的表尾结点
        scanf("%d\n", &x);
    }
    r->next = NULL;//尾结点的next指针赋值为null
    return L;
    
}
//打印链表中每个结点的值
void PrintList(LinkList L)
{
    L = L->next;
    while (L != NULL)//NULL是为了代表一张空的藏宝图
    {
        printf("%3d", L->data);//打印当前节点数据
        L = L->next;//指向下一个节点
        if (L != NULL)
        {
            printf(" ");
        }
    }
    printf("\n");
}
int main()
{
    LinkList L;
    LinkList search;
    CreatList1(L);//输入数据可以为3 4 5 6 7 9999,头插法新建链表
    PrintList(L);//链表打印
    CreatList2(L);//输入数据可以为3 4 5 6 7 9999,尾插法新建链表
    PrintList(L);
    return 0;
} 
 
 中级-day5作业 
#define _CRT_SECURE_NO_WARNINGS
#include
#include< 1)
        return NULL;
    while (p&&j
typedef int ElemType;
typedef struct LNode {
    ElemType data;
    struct LNode* next;//指向下一个节点
}LNode,*LinkList;
//尾插法建立链表
LinkList CreatList(LinkList& L)//list_tail_insert
{
    int x;
    L = (LinkList)malloc(sizeof(LNode));//带头结点的链表
    LNode* s, * r = L;//LinkList s,r=L;也可以,r代表链表表尾结点,指向链表尾部
    //3 4 5 6 7 9999
    scanf("%d", &x);
    while (x != 9999) {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = x;
        r->next = s;//让尾部节点指向新节点
        r = s;//r指向新的尾部节点
        scanf("%d", &x);
    }
    r->next = NULL;
    return L;
}
//按序号查找结点值
LNode* GetElem(LinkList L, int i)
{
    int j = 1;
    LNode* p = L->next;
    if (i == 0)
        return L;
    if (i next;
        j++;
    }
    return p;
}
//新结点插入第i个位置
bool LinstFrontInsert(LinkList L, int i, ElemType e)
{
    LinkList p = GetElem(L, i - 1);
    if (NULL == p)
    {
        return false;
    }
    LinkList s = (LNode*)malloc(sizeof(LNode));//为新插入的节点申请空间
    s->data = e;
    s->next = p->next;
    p->next = s;
    return true;
}
//删除第i个节点
bool ListDelete(LinkList L, int i)
{
    LinkList p = GetElem(L, i - 1);
    if (NULL == p)
    {
        return false;
    }
    LinkList q;
    q = p->next;
    p->next = q->next;//断链
    free(q);//释放对应节点的空间
    return true;
}
//打印链表中每个节点的位置
void PrintList(LinkList L)
{
    L = L->next;
    while (L!=NULL)
    {
        printf("%3d", L->data);
        L = L->next;//指向下一个节点
    }
    printf("\n");
}


int main()
{
    LinkList L;
    LinkList search;
    CreatList(L);
    //PrintList(L);
    search = GetElem(L, 2);//查找第二个位置的元素的值
    if (search != NULL)
    {
        printf("%d\n", search->data);

    }
    LinstFrontInsert(L, 2, 99);//新结点插入第i个位置
    PrintList(L);
    ListDelete(L, 4);//删除第4个节点
    PrintList(L);
}

中级-day6作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include< 5)
    {
        scanf("%d", &n1);
        EnQueue(Q, n1);
        n = n + 1;
    }
    Pop(S, m);
    Pop(S, m);
    Pop(S, m);
    printf("\n");
    if (n >
#define MaxSize 50
typedef int ElemType;
typedef struct {
    ElemType data[MaxSize];//数组
    int top;
}SqStack;
//栈初始化
void InitStack(SqStack& S)
{
    S.top = -1;//代表栈为空
}

//入栈
bool Push(SqStack& S, ElemType x)
{
    if (S.top == MaxSize - 1)//数组的大小不能改变,避免访问越界
    {
        return false;
    }
    S.data[++S.top] = x;
    return true;
}
//出栈
bool Pop(SqStack& S, ElemType& x)
{
    if (-1 == S.top)
        return false;
    x = S.data[S.top--];
    printf(" %d", x);
    return true;
}
//队列
typedef struct {
    ElemType data[5];//数组,存放MaxSize-1个元素
    int front, rear;//队列头,队列尾
}SqQueue;
//队列初始化
void InitQueue(SqQueue& Q)//初始化队列
{
    Q.rear = Q.front = 0;
}
//入队
bool EnQueue(SqQueue& Q, ElemType x)
{
    if ((Q.rear + 1) % MaxSize == Q.front)//判断是否队满
        return false;
    Q.data[Q.rear] = x;//3 4 5 6 7
    Q.rear = (Q.rear + 1) % MaxSize;
    return true;
}
//出队
bool DeQueue(SqQueue& Q, ElemType& x)
{
    if (Q.rear == Q.front)
        return false;
    x = Q.data[Q.front];//先进先出
    Q.front = (Q.front + 1) % MaxSize;
    printf(" %d", x);
    return true;
}
int main()
{
    SqStack S;//后进先出LIFO
    ElemType m;//用来存放拿出的元素
    InitStack(S);//初始化
    ElemType a, b, c;
    scanf("%d %d %d\n", &a, &b, &c);
    Push(S, a);
    Push(S, b);
    Push(S, c);
    SqQueue Q;
    ElemType element;//存储出队元素
    InitQueue(Q);//初始化队列
    //入队
    int n = 1;
    int n1;
    while (n  4)printf("false\n");
    for(int i=1;i

中级-day7作业

 
 
#define _CRT_SECURE_NO_WARNINGS
#include
#include
typedef int ElemType;
typedef char BiElemType;
typedef struct BiTNode {
    BiElemType c;//C就是书籍上的data
    struct BiTNode* lchild;
    struct BiTNode* rchild;
}BiTNode, * BiTree;


typedef struct tag {
    BiTree p;
    struct tag* pnext;
}tag_t, * ptag_t;
//队列的相关数据结构
typedef struct LinkNode {
    ElemType data;
    struct LinkNode* next;
}LinkNode;
typedef struct {
    LinkNode* front, * rear;
}LinkQueue;
void InitQueue(LinkQueue& Q);
bool IsEmpty(LinkQueue Q);
void EnQueue(LinkQueue& Q, ElemType x);
bool DeQueue(LinkQueue& Q, ElemType& x);


//带头结点的队列
void InitQueue(LinkQueue& Q)
{
    Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
    Q.front->next = NULL;
}

bool IsEmpty(LinkQueue Q)
{
    if (Q.front == Q.rear)
        return true;
    else
        return false;
}

void EnQueue(LinkQueue& Q, ElemType x)
{
    LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
    s->data = x; s->next = NULL;
    Q.rear->next = s;
    Q.rear = s;
}

bool DeQueue(LinkQueue& Q, ElemType& x)
{
    if (Q.front == Q.rear) return false;
    LinkNode* p = Q.front->next;//头结点什么都没存,所以头结点的下一个节点才有数据
    x = p->data;
    Q.front->next = p->next;
    if (Q.rear == p)
        Q.rear = Q.front;
    free(p);
    return true;
}


//前序遍历
void preOrder(BiTree p)
{
    if (p != NULL)
    {
        putchar(p->c);//等价于visit函数
        preOrder(p->lchild);
        preOrder(p->rchild);
    }
}

int main()
{
    BiTree pnew;
    int i, j, pos;
    char c;
    BiTree tree = NULL;//树根
    ptag_t phead = NULL, ptail = NULL, listpnew = NULL, pcur = NULL;//phead是队列头,ptail是队列尾
    while (scanf("%c", &c) != EOF)
    {
        if (c == '\n')
        {
            break;
        }
        pnew = (BiTree)calloc(1, sizeof(BiTNode));//calloc申请空间并对空间进行初始化,赋值为0
        pnew->c = c;//数据放进去
        listpnew = (ptag_t)calloc(1, sizeof(tag_t));//给队列结点申请空间
        listpnew->p = pnew;
        if (NULL == tree)
        {
            tree = pnew;//树的根
            phead = listpnew;//队列头
            ptail = listpnew;//队列尾
            pcur = listpnew;
            continue;
        }
        else
        {
            ptail->pnext = listpnew;//新节点放入链表,通过尾插法
            ptail = listpnew;//ptail指向队列尾部
        }//pcur始终指向要插入的位置
        if (NULL == pcur->p->lchild)//如何把新节点放入树
        {
            pcur->p->lchild = pnew;//把新节点放到要插入结点的左边
        }
        else if (NULL == pcur->p->rchild)
        {
            pcur->p->rchild = pnew;//把新节点放到要插入结点的右边
            pcur = pcur->pnext;//左右都放了结点后,pcur指向队列的下一个
        }

    }
    preOrder(tree);
}

中级-day8作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include
typedef char BiElemType;
typedef struct BiTNode {
    BiElemType c;//c就是书籍上的data
    struct BiTNode* lchild;
    struct BiTNode* rchild;
}BiTNode, * BiTree;

typedef struct tag {
    BiTree p;//树的某一个结点的地址值
    struct tag* pnext;
}tag_t, * ptag_t;
#define MaxSize 50
typedef BiTree ElemType;
//队列的相关数据结构
typedef struct LinkNode {
    ElemType data;
    struct LinkNode* next;
}LinkNode;
typedef struct {
    LinkNode* front, * rear;
}LinkQueue;
void InitQueue(LinkQueue& Q);
bool IsEmpty(LinkQueue Q);
void EnQueue(LinkQueue& Q, ElemType x);
bool DeQueue(LinkQueue& Q, ElemType& x);
//带头结点的队列
void InitQueue(LinkQueue& Q)
{
    Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
    Q.front->next = NULL;
}

bool IsEmpty(LinkQueue Q)
{
    if (Q.front == Q.rear)
        return true;
    else
        return false;
}

void EnQueue(LinkQueue& Q, ElemType x)
{
    LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
    s->data = x; s->next = NULL;
    Q.rear->next = s;
    Q.rear = s;
}

bool DeQueue(LinkQueue& Q, ElemType& x)
{
    if (Q.front == Q.rear) return false;
    LinkNode* p = Q.front->next;//头结点什么都没存,所以头结点的下一个节点才有数据
    x = p->data;
    Q.front->next = p->next;
    if (Q.rear == p)
        Q.rear = Q.front;
    free(p);
    return true;
}

//中序遍历  hdibjeafcg
void InOrder(BiTree p)
{
    if (p != NULL)
    {
        InOrder(p->lchild);
        putchar(p->c);
        InOrder(p->rchild);
    }
}
//hidjebfgca  后序遍历
void PostOrder(BiTree p)
{
    if (p != NULL)
    {
        PostOrder(p->lchild);
        PostOrder(p->rchild);
        putchar(p->c);
    }
}

//层次遍历,层序遍历,广度优先遍历
void LevelOrder(BiTree T)
{
    LinkQueue Q;//辅助队列
    InitQueue(Q);//初始化队列
    BiTree p;
    EnQueue(Q, T);//树根入队
    while (!IsEmpty(Q))
    {
        DeQueue(Q, p);//出队当前结点并打印
        putchar(p->c);
        if (p->lchild != NULL) //入队左孩子
            EnQueue(Q, p->lchild);
        if (p->rchild != NULL)  //入队右孩子
            EnQueue(Q, p->rchild);
    }
}
int main()
{
    BiTree pnew;
    int i, j, pos;
    char c;
    BiTree tree = NULL;//树根
    ptag_t phead = NULL, ptail = NULL, listpnew = NULL, pcur = NULL;//phead就是队列头,ptail就是队列尾
    //abcdefghij
    while (scanf("%c", &c) != EOF)
    {
        if (c == '\n')
        {
            break;
        }
        pnew = (BiTree)calloc(1, sizeof(BiTNode));//calloc申请空间并对空间进行初始化,赋值为0
        pnew->c = c;//数据放进去
        listpnew = (ptag_t)calloc(1, sizeof(tag_t));//给队列结点申请空间
        listpnew->p = pnew;
        if (NULL == tree)
        {
            tree = pnew;//树的根
            phead = listpnew;//队列头
            ptail = listpnew;//队列尾
            pcur = listpnew;
            continue;
        }
        else {
            ptail->pnext = listpnew;//新结点放入链表,通过尾插法
            ptail = listpnew;//ptail指向队列尾部
        }//pcur始终指向要插入的结点的位置
        if (NULL == pcur->p->lchild)//如何把新结点放入树
        {
            pcur->p->lchild = pnew;//把新结点放到要插入结点的左边
        }
        else if (NULL == pcur->p->rchild)
        {
            pcur->p->rchild = pnew;//把新结点放到要插入结点的右边
            pcur = pcur->pnext;//左右都放了结点后,pcur指向队列的下一个
        }
    }
    InOrder(tree);
    printf("\n");
    PostOrder(tree);
    printf("\n");
    LevelOrder(tree);
    printf("\n");
}

中级-day9作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include< T->
typedef int KeyType;
typedef struct BSTNode {
    KeyType key;
    struct BSTNode* lchild, * rchild;
}BSTNode, * BiTree;
//54 20 66 40 28 79 58
int BST_Insert(BiTree& T, KeyType k)
{
    if (NULL == T)
    {
        //为新节点申请空间
        T = (BiTree)malloc(sizeof(BSTNode));
        T->key = k;
        T->lchild = T->rchild = NULL;
        return 1;//代表插入成功
    }
    else if (k == T->key)
        return 0;//发现相同元素,就不插入
    else if (k < n)
    {
        BST_Insert(T, str[i]);
        i++;
    }
}
//中序遍历
void InOrder(BiTree T, KeyType str[], int& pos)
{
    if (T != NULL)
    {
        InOrder(T->key)
        return BST_Insert(T->lchild, k);
    else
        return BST_Insert(T->rchild, k);
}

//创建二叉排序树
void Create_BST(BiTree& T, KeyType str[], int n)
{
    T = NULL;
    int i = 0;
    while (i <= high)
    {
        mid = (low + high) / 2;
        if (L.elem[mid] == key)
            return mid;
        else if (L.elem[mid] >lchild, str, pos);
        printf("%3d", T->key);
        str[pos++] = T->key;//输出的同时存入数组
        InOrder(T->rchild, str, pos);
    }
}
typedef int ElemType;
typedef struct {
    ElemType* elem;//整形指针
    int TableLen;
}SSTable;
int Binary_Search(SSTable L, ElemType key)
{
    int low = 0, high = L.TableLen - 1, mid;
    while (low < 10; i++)
    {
        scanf("%d", &str[i]);
    }
    Create_BST(T, str, 10);
    int pos = 0;
    InOrder(T, str, pos);//中序遍历把有序数组的结果存到str数组中
    printf("\n");
    SSTable L;
    L.elem = str;
    L.TableLen = 10;
    pos = Binary_Search(L, 21);
    printf("%d\n", pos);
    return 0;

} key)
            high = mid - 1;
        else
            low = mid + 1;
    }
    return -1;
}
int main()
{
    BiTree T = NULL;
    BiTree parent;//存储父亲节点的地址值
    BiTree search;
    KeyType str[10];//将要进入二叉排序树的元素值
    int i;
    for (i = 0; i 

中级-day10作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include< 9; i++)
    {
        printf("%2d", arr[i]);
    }
    return 0;
}

int main()
{
    char j;
    scanf("%c",&j);
    int i;
    int arr[9] = { 0,1,1,2,2,3,1,1,2 };
    for (i = 0; i 

中级day11-作业

 
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include 
#include< ST.TableLen; i++)
    {
        ST.elem[i] = A[i];
    }

}
void ST_print(SSTable ST)
{
    for (int i = 0; i < ST.TableLen; i++)
    {
        printf("%3d", ST.elem[i]);
    }
    printf("\n");
}
void swap(ElemType& a, ElemType& b)
{
    ElemType tmp;
    tmp = a;
    a = b;
    b = tmp;
}
void BubbleSort1(ElemType A[], int n)
{
    int i, j, flag;
    for (i = 0; i < n - 1; i++)//i是控制有多少个有序了
    {
        flag = 0;
        for (j = n - 1; j >

typedef int ElemType;
typedef struct {
    ElemType* elem;//存储元素的起始地址
    int TableLen;
}SSTable;
void ST_Init(SSTable& ST, ElemType A[],int len)
{
    ST.TableLen = len;
    ST.elem = (ElemType*)malloc(sizeof(ElemType) * ST.TableLen);
    int i;
    for (i = 0; i < right; i++)
    {
        if (arr[i] < arr[right])
        {
            swap(arr[i], arr[k]);
            k++;
        }
    }
    swap(arr[k], arr[right]);
    return k;
}


//递归实现
void QuickSort(ElemType A[], int low, int high)
{
    if (low < high)
    {
        int pivotpos = Partition(A, low, high);//分割点左边的元素都比分割点要小,右边的比分割点大
        QuickSort(A, low, pivotpos - 1);
        QuickSort(A, pivotpos + 1, high);
    }
}
void InsertSort(ElemType A[], int n)
{
    int i, j;
    for (i = 2; i < n; i++)
    {
        if (A[i] < A[i - 1])
        {
            A[0] = A[i];//放到暂存位置,A[0]既是暂存,也是哨兵
            for (j = i - 1; A[0] < A[j]; --j)//移动元素,内层循环控制有序序列中的每一个元素和要插入的元素比较
                A[j + 1] = A[j];
            A[j + 1] = A[0];//把暂存元素插入到对应位置
        }
    }
}
int main()
{
    SSTable ST;
    ElemType A[10];
    int i;
    for (i = 0; i < 10; i++)
    {
        scanf("%d", &A[i]);
    }
    ST_Init(ST,A, 10);//初始化
    BubbleSort1(ST.elem, 10);//冒泡排序
    ST_print(ST);
    QuickSort(ST.elem, 0, 9);//快速排序
    ST_print(ST);
    InsertSort(ST.elem, 10);//插入排序
    ST_print(ST);
}

  i; j--)//内层控制比较,交换
        {
            if (A[j - 1] > A[j])
            {
                swap(A[j - 1], A[j]);
                flag = 1;
            }
        }
    }
}
int Partition(int* arr, int left, int right)
{
    int k, i;
    for (k = i = left; i 

 

 

 

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/584948.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)