目录

课时2作业1

课时2作业2

课时2作业3

课时3作业1

课时3作业2

课时4作业1

课时4作业2

课时4作业2

课时4作业3

课时5作业1

课时5作业2

课时6作业1

课时6作业2

课时7作业

课时8作业1

课时8作业2

课时10作业

课时11作业

课时12作业

课时13作业

课时14作业1

课时14作业2

课时15作业

课时19作业1

课时19作业2

课时20作业


课时2作业1

Description

各位C督学营的同学,大家好,这是一道开启编程之路的入门题,要求是请输出 hello wangdao

Input

不需要输入

Output

hello wangdao

Sample Input 1

不需要

Sample Output 1

hello wangdao
#include <cstdio>int main() {printf("hello wangdao");return 0;
}

课时2作业2

Description

你的任务是计算a+b

Input

输入包含a和b,通过空格隔开

Output

需要输出a、b的和

Sample Input 1

1 4

Sample Output 1

5
#include<bits/stdc++.h>//万能头文件,c++可以尝试使用哦~
using namespace std;
int main(){int a,b;scanf("%d %d",&a,&b);printf("%d",a+b);
}

课时2作业3

Description

读取一个65到122之间的整型数,然后以字符形式输出它,比如读取了97,输出字符a

Input

读取一个整型数,整型数 大于等于65,小于等于122

Output

输出整型数 在ASCII表中对应的字符

Sample Input 1

97

Sample Output 1

a
#include "bits/stdc++.h"
using namespace std;
int main(){int n;scanf("%d",&n);printf("%c",n);
}

课时3作业1

Description

判断某个年份是不是闰年,如果是闰年,请输出“yes”,否则请输出“no”

Input

输入一行,只有一个整数x (0<=x <=10000)

Output

输出只有一行字符

Sample Input 1

2000

Sample Output 1

yes

Sample Input 2

1999

Sample Output 2

no
#include <bits/stdc++.h>//万能头文件
using namespace std;
int main(){int n;//定义输入的年份scanf("%d",&n);//输入if(n%100 == 0){//判断是不是一百的倍数if(n%400 == 0)//判断是不是四百的倍数printf("yes");elseprintf("no");}else{if(n%4 == 0)//再看看是不是4的倍数printf("yes");elseprintf("no");}return 0 ;
}

课时3作业2

Description

读取一个整型数,字符,浮点数,分别到变量i,j,k中,然后将i,j,k直接相加并输出,小数点后保留两位小数,不用考虑输入的浮点数的小数部分超过了两位

Input

一个整型数,字符,浮点数

Output

i,j,k三个变量的求和值

Sample Input 1

10 a 98.3

Sample Output 1

205.30
#include "bits/stdc++.h"
using namespace std;
int main(){int i;char j;float k;scanf("%d %c%f",&i,&j,&k);printf("%.2f",i+j+k);
}

课时4作业1

Description

输入一个整型数,判断是否是对称数,如果是,输出yes,否则输出no,不用考虑这个整型数过大,int类型存不下,不用考虑负值;

例如 12321是对称数,输出yes,124421是对称数,输出yes,1231不是对称数,输出no

Input

一个整型数

Output

输出是yes,或者no

Sample Input 1

12321

Sample Output 1

yes

Sample Input 2

1231

Sample Output 2

no
#include "bits/stdc++.h"
using namespace std;
int main(){int a[15];//因为输入数据不超过int类型所以数据在2147483647之内,即数据最大为十位数//那么我定义一个数组让每一个变量放一个输入数据的一位数字int n,k=0;bool key = true;//定义一个波尔类型的,用来判断是不是前后的数据相同scanf("%d",&n);//输入数据while(n != 0){//判断数据是否读完k++;a[k] = n%10;n/=10;}for(int i = 1;i<=(k/2);i++){if(a[i] != a[k-i+1]){printf("no");key = false;//调整key值告诉后面的if语句这个数据不是对称数break;//发现不同就可以退出循环了,break是退出循环}}if(key == true){printf("yes");}
}

课时4作业2

Description

利用while或者for循环计算n!的值。

提示:n!=1*2*3…*n

Input

一个正整数n,1≤n≤10。

Output

n!的值。

Sample Input 1

2

Sample Output 1

2

Sample Input 2

5

Sample Output 2

120
#include "bits/stdc++.h"
using namespace std;
int main(){int n;//定义输入的数nint tot = 1;//因为10的阶乘只有3628800,在int范围之内足够处理scanf("%d",&n);//输入nfor(int i=1;i<=n;i++){//循环处理从1乘到ntot *= i;}printf("%d",tot);//输出
}

课时4作业2

Description

利用while或者for循环计算n!的值。

提示:n!=1*2*3…*n

Input

一个正整数n,1≤n≤10。

Output

n!的值。

Sample Input 1

2

Sample Output 1

2

Sample Input 2

5

Sample Output 2

120
#include "bits/stdc++.h"
using namespace std;
int main(){int n;//定义输入的数nint tot = 1;//因为10的阶乘只有3628800,在int范围之内足够处理scanf("%d",&n);//输入nfor(int i=1;i<=n;i++){//循环处理从1乘到ntot *= i;}printf("%d",tot);//输出
}

课时4作业3

Description

某人想将手中的一张面值100元的人民币换成10元、5元、2元和1元面值的票子。要求换正好40张,且每种票子至少一张。问:有几种换法?

Input

无输入

Output

一个数,表示共有多少种换法

Sample Input 1

Sample Output 1

不能告知,因为只有一个数,偷偷告诉你小于100
#include <bits/stdc++.h>//万能头
using namespace std;
int main(){//做一个假设,自己兜里有100块钱然后用for循环去买1,2,5,10块钱的商品买40个int tot = 100;//tot表示自己兜里的100元钱int sum = 0;//表示总数,计算有多少种换法for(int i = 1;i<=37;i++){//i表示1元钱的数量,最少1张,最多37张,因为其他都最少需要1张tot-=i;for(int j = 1;j<=38-i;j++){//j表示2元钱的数量tot-=j*2;for(int k = 1;k<=39-i-j;k++){//k表示5元钱的数量tot-=k*5;if(tot - (40-i-j-k)*10 == 0){//用40-i-j-k表示10元钱的数量,如果正好40个买完之后100元钱正好花完那么就算入一个换法方式sum++;//sum计入一次换法}tot+=k*5;}tot+=j*2;}tot+=i;}printf("%d",sum);
}

课时5作业1

Description

输入N个数(N小于等于100),输出数字2的出现次数;

解题提示:

整型数组读取5个整型数的方法如下:

int a[100];

for(int i=0;i<5;i++)

{

scanf("%d",&a[i]);

}

Input

输入的格式是两行

第一行输入要输的元素个数,比如5

第二行输入  1 2 2 3 2,那么输出结果为3,因为2出现了3次

Output

统计数字2出现的次数

Sample Input 1

5
1 2 2 3 2

Sample Output 1

3
#include "bits/stdc++.h"
using namespace std;
int main(){int n,a;//定义一个n用来表示输入数据的总量,a用来存储输入的具体数据int tot = 0;//tot表示2的总数scanf("%d",&n);for(int i = 1;i<=n;i++){scanf("%d",&a);if(a == 2)//一旦输入的数据等于2就记录一次,tot加一tot++;}printf("%d",tot);return 0;
}

课时5作业2

Description

读取一个字符串,字符串可能含有空格,将字符串逆转,原来的字符串与逆转后字符串相同,输出0,原字符串小于逆转后字符串输出-1,大于逆转后字符串输出1。例如输入 hello,逆转后的字符串为 olleh,因为hello 小于 olleh,所以输出-1

注意最后的判断一定要这么写,因为strcmp标准C中并不是返回-1和1,而是负值和正值

int result = strcmp(c, d);

if (result < 0)

{

printf("%d\n",-1);

}

else if (result > 0)

{

printf("%d\n", 1);

}

else {

printf("%d\n", 0);

}

Input

输入一个字符串,例如 hello,当然输入的字符串也可能是 how are you,含有空格的字符串

Output

输出是一个整型数,如果输入的字符串是hello,那么输出的整型数为-1

Sample Input 1

hello

Sample Output 1

-1

Sample Input 2

cba

Sample Output 2

1

Sample Input 3

aba

Sample Output 3

0
#include "bits/stdc++.h"
using namespace std;
int main(){int k;//定义k表示输入的字符串的长度int t = 0;//定义一个t作为标志表示这个字符串是不是对称字符串char x[1001],y[1001];//x为输入的字符串,y是输入的字符串的对称表现形式scanf("%s",&x);//输入字符串k = strlen(x);//strlen函数表示计算出字符串的长度for(int i = k-1;i>=0;i--){//循环将字符串对称过来,并且用y储存y[k-i-1] = x[i];}for(int i = 0;i<k;i++){//从前往后对比字符串,比较谁大谁小if(x[i]>y[i]){//如果x大代表原字符串大,那么输出1printf("1");t = 1;//做一个标记表示字符串不是对称字符串break;//跳出循环不再执行}else if(x[i]<y[i]){//跟上面的同理printf("-1");t = 1;break;}}if(t == 0){//判断一下,如果前面循环没有break跳出那么就是一个对称的字符串,需要输出0printf("0");}
}

课时6作业1

Description

输入一个整型数,存入变量i,通过子函数change把主函数的变量i除2,然后打印i,例如如果输入的为10,打印出5,如果输入的为7,打印出3

Input

一个整型数

Output

对应整型数除2后的商

Sample Input 1

10

Sample Output 1

5

Sample Input 2

7

Sample Output 2

3
#include "bits/stdc++.h"
using namespace std;
void change(int *j){*j = *j/2;
}
int main(){int i;scanf("%d",&i);change(&i);printf("%d",i);
}

课时6作业2

Description

输入一个整型数,然后申请对应大小空间内存,然后读取一个字符串,字符串的输入长度小于最初输入的整型数大小,最后输出输入的字符串即可(无需考虑输入的字符串过长,超过了内存大小);

注意下面问题:

char *p;

scanf("%d",&n);

p=malloc(n);

scanf("%c",&c);//注意在scanf和gets中间使用scanf("%c",&c),去除换行

gets(p);

Input

一个整型数和一个字符串,例如

10

hello

Output

输出输入的字符串,上面输入的是hello,那么输出hello

Sample Input 1

10
hello

Sample Output 1

hello
#include <stdlib.h>
#include <stdio.h>
int main(){char* p;int n;char c;scanf("%d",&n);p=(char*)malloc(n);scanf("%c",&c);//注意在scanf和gets中间使用scanf("%c",&c),去除换行gets(p);puts(p);return 0;
}

课时7作业

Description

假如有n个台阶,一次只能上1个台阶或2个台阶,请问走到第n个台阶有几种走法?为便于读者理解题意,这里举例说明如下:假如有3个台阶,那么总计就有3种走法:第一种为每次上1个台阶,上3次;第二种为先上2个台阶,再上1个台阶;第三种为先上1个台阶,再上2个台阶。输入为n,输出为走到第n个台阶有几种走法

Input

比如输入是3

Output

如果输入是3,走到第3个台阶的走法总计有3种,1,1,1 和  1,2 和2,1,输出为3

Sample Input 1

1

Sample Output 1

1

Sample Input 2

3

Sample Output 2

3

Sample Input 3

4

Sample Output 3

5
#include "bits/stdc++.h"
using namespace std;
int search(int x){//定义一个搜索子函数if(x == 1)//如果询问上一级楼梯有多少种就返回一种return 1;else if(x == 2)//如果询问上两级楼梯有多少种就返回两种,可以先上一级再上一级也可以直接上两级return 2;return search(x-1)+search(x-2);//那么上x级楼梯就可以从x-1级楼梯上来也可以从x-2级楼梯上来,那么就是x-1级楼梯和x-2级楼梯上楼方法数的总和
}
int main(){int n;scanf("%d",&n);//输入楼梯数nprintf("%d",search(n));//输出搜索的n级楼梯需要多少多少种方法
}

课时8作业1

Description

输入一个学生的学号,姓名,性别,用结构体存储,通过scanf读取后,然后再通过printf打印输出

Input

学号,姓名,性别,例如输入 101 xiongda m

Output

输出和输入的内容一致,如果输入的是101 xiongda m,那么输出也是101 xiongda m

Sample Input 1

101 xiongda m

Sample Output 1

101 xiongda m
#include <bits/stdc++.h>
using namespace std;
struct student{int num;char name[20];char sex;
};
int main(){struct student s;scanf("%d %s %c",&s.num,&s.name,&s.sex);printf("%d %s %c",s.num,s.name,s.sex);return 0;
}

课时8作业2

Description

使用C++的引用,注意提交时把代码选为C++;在主函数定义字符指针 char *p,然后在子函数内malloc申请空间,通过fgets读取字符串,然后在主函数中进行输出;要求子函数使用C++的引用,注意在C++中从标准输入读取字符串,需要使用fgets(p,100,stdin)

Input

输入一个字符串,例如 I love C language

Output

如果输入的是I love C language,那么输出也是I love C language

Sample Input 1

I love C language

Sample Output 1

I love C language

Sample Input 2

how are you

Sample Output 2

how are you
#include <bits/stdc++.h>
using namespace std;
void change(char *&p){//&就是引用操作p = (char*)malloc(100);//从堆空间借100个字节来用fgets(p,50,stdin);//输入p最大为50个字节
}
int main(){char *p;//p指针change(p);//丢到函数里面引用操作puts(p);//输出free(p);//把p借的堆空间还回去return 0;
}

课时10作业

Description

初始化顺序表(顺序表中元素为整型),里边的元素是1,2,3,然后通过scanf读取一个元素(假如插入的是6),插入到第2个位置,打印输出顺序表,每个元素占3个空格,格式为1  6  2  3,然后scanf读取一个整型数,是删除的位置(假如输入为1),然后输出顺序表  6  2  3,假如输入的位置不合法,输出false字符串。提醒,Language一定要选为C++。

Input

第一次输入插入的元素值,第二次输入删除的位置

Output

假如插入的元素为6,那么输出为
1  6  2  3

假如删除的位置为1,那么输出为
6  2  3

Sample Input 1

6
1

Sample Output 1

  1  6  2  36  2  3

Sample Input 2

9
3

Sample Output 2

  1  9  2  31  9  3

Sample Input 3

9
6

Sample Output 3

  1  9  2  3
false
#include <cstdio>
#define Size 100
typedef struct {int s[Size] = {1,2,3};//顺序表定义内部的数据int len = 3;//顺序表对应的长度
}SqList;//SqList为顺序表的名字
//顺序表插入数据
bool ListInsert(SqList &L,int pos,int ele){if(pos > L.len+1 || pos < 1){return false;}if(L.len >= Size){return false;}for(int i =L.len ; i>=pos;i--){L.s[i] = L.s[i-1];}L.s[pos-1] = ele;L.len++;return true;
}
//顺序表删除数据
bool ListDelete(SqList &L,int pos){if(pos>L.len || pos<1){return false;}for(int i = pos;i<L.len;i++){L.s[i-1] = L.s[i];}L.len--;return true;
}int main(){int a;//a为输入的数据SqList L ;scanf("%d",&a);bool ret = ListInsert(L,2,a);if(ret){for(int i = 0;i<L.len;i++){printf("%3d",L.s[i]);}printf("\n");}else{printf("false");}scanf("%d",&a);ret = ListDelete(L,a);if(ret){for(int i = 0;i<L.len;i++){printf("%3d",L.s[i]);}printf("\n");}else{printf("false");}return 0;
}

课时11作业

Description

输入3 4 5 6 7 9999一串整数,9999代表结束,通过头插法新建链表,并输出,通过尾插法新建链表并输出。

注意输出要采用如下代码(因为OJ判题对空格敏感,因此需要用下面的打印代码来做):

//打印链表中每个结点的值

void PrintList(LinkList L)

{

L=L->next;

while(L!=NULL)

{

printf("%d",L->data);//打印当前结点数据

L=L->next;//指向下一个结点

if(L!=NULL)

{

printf(" ");

}

}

printf("\n");

}

完成作业的同学,可以购买《跟龙哥学C语言编程》,有很多课后习题可以练习,附带答案,或者直接B站搜王道论坛,看王道的数据结构,组成原理。

Input

3 4 5 6 7 9999,第二行也是3 4 5 6 7 9999,数据需要输入两次

Output

如果输入是3 4 5 6 7 9999,那么输出是7 6 5 4 3,数之间空格隔开,尾插法的输出是3 4 5 6 7

Sample Input 1

3 4 5 6 7 9999
3 4 5 6 7 9999

Sample Output 1

7 6 5 4 3
3 4 5 6 7

Sample Input 2

1 3 5 7 9 9999
1 3 5 7 9 9999

Sample Output 2

9 7 5 3 1
1 3 5 7 9
#include<bits/stdc++.h>//万能头
using namespace std;typedef int ElemType;typedef struct LNode{ElemType data;struct LNode *next;
}LNode,*LinkList;//链表
//头插法
void list_insert_head(LinkList &L){LinkList s;ElemType x;L = (LinkList) malloc(sizeof (LNode));L ->next = NULL;scanf("%d",&x);while(x!=9999){s =(LinkList) malloc(sizeof (LNode));s->data = x;s->next = L->next;L->next = s;scanf("%d",&x);}
}
//尾插法
void list_insert_tail(LinkList &L){L = (LinkList) malloc(sizeof (LNode));ElemType x;LinkList s,r = L;scanf("%d",&x);while(x!=9999){s =(LinkList) malloc(sizeof (LNode));s->data = x;r->next = s;r = s;scanf("%d",&x);}s->next = NULL;
}
//输出格式
void PrintList(LinkList L){L=L->next;while(L!=NULL){printf("%d",L->data);//打印当前结点数据L=L->next;//指向下一个结点if(L!=NULL){printf(" ");}}printf("\n");
}int main(){LinkList L;list_insert_head(L);PrintList(L);list_insert_tail(L);PrintList(L);
}

课时12作业

Description

输入3 4 5 6 7 9999一串整数,9999代表结束,通过尾插法新建链表,查找第二个位置的值并输出,在2个位置插入99,输出为  3 99  4  5  6  7,删除第4个位置的值,打印输出为  3 99  4  6  7。

输出函数如下:

void PrintList(LinkList L)

{

L = L->next;

while (L != NULL)

{

printf("%3d", L->data);//打印当前结点数据

L = L->next;//指向下一个结点

}

printf("\n");

}

针对双向链表,有时间的同学自己练习即可,这道题同样也可以用双向链表来实现一遍

Input

输入是3 4 5 6 7 9999

Output

输出是

4

3 99  4  5  6  7

3 99  4  6  7

Sample Input 1

3 4 5 6 7 9999

Sample Output 1

43 99  4  5  6  73 99  4  6  7

Sample Input 2

1 3 5 7 9 9999

Sample Output 2

31 99  3  5  7  91 99  3  7  9
#include <bits/stdc++.h>
using namespace std;
typedef int Elemtype;
typedef struct LNode{Elemtype data;struct LNode* next;
}LNode,*LinkList;//单链表
//尾插法
void list_tail_insert(LinkList &L){L = (LinkList) malloc(sizeof (LNode));Elemtype x;LinkList s,r = L;scanf("%d",&x);while(x!=9999){s = (LinkList) malloc(sizeof (LNode));s->data = x;r->next = s;r = s;scanf("%d",&x);}s->next = NULL;
}
//输出格式
void PrintList(LinkList L){L = L->next;while (L != NULL){printf("%3d", L->data);//打印当前结点数据L = L->next;//指向下一个结点}printf("\n");
}
//按位置查找
LinkList get_elem(LinkList L,int x){int i =1;LinkList p = L->next;if(x == 0){return L;}if(x < 0){return NULL;}while(p&&i<x){p = p->next;i++;}return p;
}
//链表中间插入
bool list_front_insert(LinkList L,int x,Elemtype e){LinkList p = get_elem(L,x-1);if(p ==0){return false;}LinkList s = (LinkList) malloc(sizeof (LNode));s->data = e;s->next = p->next;p->next = s;return true;
}
//链表中间删除
bool list_front_delete(LinkList L,int x){LinkList p = get_elem(L,x-1);if(p == NULL){return false;}LinkList q = p->next;p->next = q->next;free(q);return true;
}int main(){LinkList L;list_tail_insert(L);LinkList p = get_elem(L,2);printf("%d\n",p->data);list_front_insert(L,2,99);PrintList(L);list_front_delete(L,4);PrintList(L);
}

课时13作业

Description

新建一个栈,读取标准输入3个整数3 4 5,入栈3 4 5,依次出栈,打印 5 4 3,新建循环队列(Maxsize为5),读取标准输入3 4 5 6 7,入队7时,队满,打印false,然后依次出队,输出 3 4 5 6

Input

读取标准输入,内容依次是3 4 5,换行后,接着是3 4 5 6 7

Output

如果输入是3 4 5,换行,接着是3 4 5 6 7,那么输出是

5 4 3

false

3 4 5 6

注意每个数字占用两个字符的位置,5之前是有一个空格的,第三行的3之前也是有一个空格的

Sample Input 1

3 4 5
3 4 5 6 7

Sample Output 1

 5 4 3
false3 4 5 6

Sample Input 2

1 3 9
1 3 5 7 9

Sample Output 2

 9 3 1
false1 3 5 7
#include <bits/stdc++.h>
using namespace std;
#define Maxsize 100
#define MaxSize 5
typedef int ElemType;typedef struct {ElemType data[Maxsize];int top;
}SqStack;//栈typedef struct{ElemType data[MaxSize];//数组存储MaxSize-1个元素int front,rear;//队列头,尾
}SqQueue;//栈初始化
void InitStack(SqStack &S){S.top = -1;}
//检查栈是否为空
bool EmptyStack(SqStack &S){if(S.top == -1){return true;}else{return false;}
}
//入栈
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(S.top == -1)return false;x = S.data[S.top--];return true;
}
//读取栈顶元素
bool GetTop(SqStack &S,ElemType &x){if(S.top == -1){return false;}x = S.data[S.top];return true;
}
//初始化队列
void Initqueue(SqQueue &Q){Q.rear = Q.front = 0;
}
//检查队列是否为空
bool EmptyQueue(SqQueue &Q){if(Q.front == Q.rear){return true;}else{return false;}
}
//入队
bool EnQueue(SqQueue &Q,ElemType x){if((Q.rear+1)%MaxSize==Q.front){return false;}Q.data[Q.rear] = x;Q.rear=(Q.rear+1)%MaxSize;return true;
}
//出队
bool DeQueue(SqQueue &Q,ElemType &x){if(EmptyQueue(Q)){return false;}x = Q.data[Q.front];Q.front= (Q.front+1)%MaxSize;return true;
}int main(){SqStack S;//栈SSqQueue Q;//队列QInitStack(S);ElemType x;for(int i=1;i<=3;i++){scanf("%d",&x);Push(S,x);}bool ret;ret = Pop(S,x);while(ret){printf("%2d",x);ret = Pop(S,x);}printf("\n");Initqueue(Q);scanf("%d",&x);ret = EnQueue(Q,x);while(ret){scanf("%d",&x);ret = EnQueue(Q,x);}printf("false\n");ret = DeQueue(Q,x);while(ret){printf("%2d",x);ret = DeQueue(Q,x);}
}

课时14作业1

Description

读取字符串abcdefghij,然后层次建树建立一颗二叉树,然后前序遍历输出abdhiejcfg,注意不要打印前序遍历几个汉字

Input

abcdefghij

Output

abdhiejcfg

Sample Input 1

abcdefghij

Sample Output 1

abdhiejcfg
#include <cstdio>
#include <cstdlib>typedef char BiElemtype;typedef struct BiTNode{BiElemtype weight;struct BiTNode *lchild;struct BiTNode *rchild;
}BiTNode,*BiTree;typedef struct tag{BiTree p;struct tag *next;
}tag_t,*ptag_t;void PreOrder(BiTree tree){//前序遍历if(tree != NULL){printf("%c",tree->weight);PreOrder(tree->lchild);PreOrder(tree->rchild);}
}int main(){BiTree pnew;char c;BiTree tree = NULL;ptag_t phead = NULL,ptail = NULL,listpnew = NULL,pcur = NULL;while(scanf("%c",&c)){if(c == '\n'){break;}pnew = (BiTree) calloc(1,sizeof(BiTNode));pnew->weight = c;listpnew = (ptag_t) calloc(1,sizeof(tag_t));listpnew->p = pnew;if(tree ==  NULL){tree = pnew;phead = listpnew;ptail = listpnew;pcur = listpnew;}else{ptail->next = listpnew;ptail = listpnew;if(pcur->p->lchild == NULL){pcur->p->lchild = pnew;}else if(pcur->p->rchild == NULL){pcur->p->rchild = pnew;pcur = pcur->next;}}}PreOrder(tree);//前序遍历return 0;
}

课时14作业2

Description

读取字符串abcdefghij,然后层次建树建立一颗二叉树,然后中序遍历输出 hdibjeafcg,后序遍历输出 hidjebfgca,层序遍历输出abcdefghij,注意不要输出汉字

Input

abcdefghij

Output

中序遍历输出hdibjeafcg,后序遍历输出 hidjebfgca,层序遍历输出abcdefghij,每个一行

Sample Input 1

abcdefghij

Sample Output 1

hdibjeafcg
hidjebfgca
abcdefghij
#include <cstdio>
#include <cstdlib>typedef char BiElemtype;typedef struct BiTNode{BiElemtype weight;struct BiTNode *lchild;struct BiTNode *rchild;
}BiTNode,*BiTree;typedef struct tag{BiTree p;struct tag *next;
}tag_t,*ptag_t;typedef BiTree Elemtype;typedef struct LinkNode{Elemtype data;struct LinkNode *next;
}LinkNode;typedef struct {LinkNode *front,*rear;
}LinkQueue;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;elsereturn 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 InOrder(BiTree tree){//中序遍历if(tree != NULL){InOrder(tree->lchild);printf("%c",tree->weight);InOrder(tree->rchild);}
}void PostOrder(BiTree tree){//后序遍历if(tree != NULL){PostOrder(tree->lchild);PostOrder(tree->rchild);printf("%c",tree->weight);}
}void LevelOrder(BiTree tree){LinkQueue Q;InitQueue(Q);BiTree p;EnQueue(Q,tree);while(!IsEmpty(Q)){DeQueue(Q,p);printf("%c",p->weight);if(p->lchild != NULL){EnQueue(Q,p->lchild);}if(p->rchild != NULL){EnQueue(Q,p->rchild);}}
}int main() {BiTree pnew;char c;BiTree tree = NULL;ptag_t phead = NULL, ptail = NULL, listpnew = NULL, pcur = NULL;while (scanf("%c", &c)) {if (c == '\n') {break;}pnew = (BiTree) calloc(1, sizeof(BiTNode));pnew->weight = c;listpnew = (ptag_t) calloc(1, sizeof(tag_t));listpnew->p = pnew;if (tree == NULL) {tree = pnew;phead = listpnew;ptail = listpnew;pcur = listpnew;} else {ptail->next = listpnew;ptail = listpnew;if (pcur->p->lchild == NULL) {pcur->p->lchild = pnew;} else if (pcur->p->rchild == NULL) {pcur->p->rchild = pnew;pcur = pcur->next;}}}InOrder(tree);printf("\n");PostOrder(tree);printf("\n");LevelOrder(tree);
}

课时15作业

Description

读取10个元素 87  7 60 80 59 34 86 99 21  3,然后建立二叉查找树,中序遍历输出3  7 21 34 59 60 80 86 87 99,针对有序后的元素,存入一个长度为10的数组中,通过折半查找找到21的下标(下标为2),然后输出2

Input

标准输入读取10个元素 87  7 60 80 59 34 86 99 21  3

Output

中序遍历输出有序,每个元素占3个字母位置
3  7 21 34 59 60 80 86 87 99

接着输出2即可(就是元素21的下标),注意2直接在行首输出即可。

Sample Input 1

87  7 60 80 59 34 86 99 21  3

Sample Output 1

  3  7 21 34 59 60 80 86 87 99
2
#include<bits/stdc++.h>
using namespace std;
typedef int Elemtype;
typedef struct BSTNode{Elemtype key;struct BSTNode *lchild,*rchild;
}BSTNode,*BiTree;
//递归实现二叉排序树的插入操作
int BST_Insert(BiTree &T,Elemtype k){if(T == NULL) {T = (BiTree) malloc(sizeof(BSTNode));T->key = k;T->lchild = T->rchild = NULL;}else if(T->key == k){return 0;}else if(T->key < k){return BST_Insert(T->rchild,k);}else if(T->key > k){return BST_Insert(T->lchild,k);}return 0;
}
//建树
void Creat_BST(BiTree &T,Elemtype Str[],int k){T = NULL;for(int i = 0;i<k;i++){BST_Insert(T,Str[i]);}
}
//中序遍历
void Inorder(BiTree T,Elemtype *Str){static int pos = 0;if(T!=NULL){Inorder(T->lchild,Str);printf("%3d",T->key);Str[pos++] = T->key;Inorder(T->rchild,Str);}
}
//二分查找
int BinarySearch(Elemtype *Str,Elemtype key,int len){int low = 0;int high = len-1;int mid;while(low<=high){mid = (low+high)/2;if(key<Str[mid]){high = mid-1;}else if(key>Str[mid]){low = mid+1;}else{return mid;}}return 0;
}int main(){BiTree T;int len = 10;//长度Elemtype Str[10];for(int i = 0;i<len;i++){scanf("%d",&Str[i]);}Creat_BST(T,Str,len);Inorder(T,Str);printf("\n");Elemtype key = 21;printf("%d",BinarySearch(Str,key,len));return 0;
}

课时19作业1

Description

读取一个有符号数,对其进行左移,输出结果,对其进行右移,输出结果,例如,输入数值5,左移得到的结果是10,右移得到的结果是2。(不考虑左移后正值变为负值,负值变为正值的情况),每个输出占用2个字符位置,采用(%2d)

Input

输入的值为5

Output

输出左移1位后的值,为10,换行,再输出右移后的值为2。

Sample Input 1

5

Sample Output 1

102
#include<bits/stdc++.h>
using namespace std;
int main(){int n;scanf("%d",&n);printf("%2d\n",n<<1);printf("%2d",n>>1);return 0;
}

课时19作业2

Description

输入5个数,其中2个数出现2次,1个数是出现1次,找出出现1次的那个数,例如输入的是8 5 3 5 8,输出的值为3

Input

8 5 3 5 8

Output

3

Sample Input 1

8 5 3 5 8

Sample Output 1

3
#include<bits/stdc++.h>
using namespace std;
int main(){int n[5];for(int i = 0;i<5;i++){scanf("%d",&n[i]);}int result = 0;for(int i=0;i<5;i++){result ^= n[i];}printf("%d",result);return 0;
}

课时20作业

Description

例如浮点数4.5的指数部分的值为2,(129-127得到),小数部分是00100000000000000000000,里边1的个数只有1个,那么输出1,总计的输出就是  2  1,每个数占用3个字符位置%3d。现在输入的浮点数是1.456,那么需要大家通过人工单步调试,看看内存,算一算1.456的指数部分是多少,小数部分1的个数数一数,然后输出

Input

无需输入

Output

输出结果不能告诉大家,如果是4.5,输出是2  1

Sample Input 1

Sample Output 1

保密
#include<bits/stdc++.h>
using namespace std;
int main(){//float f = 1.456;// 3    f    b    a    5    e    3    5// 0011 1111 1011 1010 0101 1110 0011 0101// 0 01111111 01110100101111000110101printf("%3d%3d",0,13);return 0;
}

王道C语言督学营OJ题解合集(24考研人持续更新ing)相关推荐

  1. 王道C语言督学营oj练习(全部更新完成)

    (本人是2022年3月跟着王道C语言训练营学习过的,代码只供学习参考,但都是ac成功了的,如果存在ac不了的情况就检查一下自己的代码,注意运行语言有时候是C有时候是C++) 目录 初级阶段 week1 ...

  2. 腾讯C++面试题【合集】来啦~持续更新中

    为了方便大家集中查看面经 这里给大家做一个合集 腾讯C++面试题 2020年腾讯实习生C++面试题及答案持续更新中(1) 2020年腾讯实习生C++面试题及答案持续更新中(2) 2020年腾讯实习生C ...

  3. c语言一些如暂停、清屏等功能(持续更新ing...)

    不完全代码: #include<stdlib.h>#include <windows.h>system("cls");//清屏system("pa ...

  4. AAAI2021论文合集汇总!(持续更新)

    论文标题 论文地址 论文作者 Non-Autoregressive Coarse-to-Fine Video Captioning https://arxiv.org/abs/1911.12018 B ...

  5. c语言综合作业题库,计算机二级等级考试《C语言》选择题专项练习合集

    计算机二级等级考试<C语言>选择题专项练习合集 第 500 题 (单项选择题)(每题 1.75 分) 题目分类:第二部分 二级C语言程序设计选择题 > 第3章 函数和数组 > ...

  6. 火龙果(redpitaya)开发板常用接口C语言开发指南(九)——产生信号脉冲(持续更新中)

    火龙果(redpitaya)开发板常用接口C语言开发指南(九)--产生信号脉冲(持续更新中) --本人为<火龙果实战指南--搭建基于Zynq处理器的测量仪器与创新实践平台>一书的作者之一, ...

  7. 【C语言】那些优秀代码里的骚操作(持续更新…)

    [C语言]那些优秀代码里的骚操作(持续更新-) 1.联合体`union`的妙用 2.`#include`的本质是什么? 3.脱裤子放屁的`do{ }while(0)` 4.一个成熟的代码要学会自己写函 ...

  8. ZZULIOJ1001~1100题解合集

    前言: 从本篇文章开始,将开始对ZZULIOJ上前200道基础常见的算法题进行讲解并发表下个人看法,本人能力有限,就当给大家提供一下解题的思路.这些题都是一些C语言中的常见题型,大家千万不要直接复制粘 ...

  9. 西电2020 python OJ作业(50道题目,持续更新)

    西电2020 python OJ作业(50道题,持续更新) 0.A+B problem 题目描述 用来适应环境的题目,自然比较简单些. 计算2个整数的和.这两个整数都在1到1000之间. 输入 输入只 ...

最新文章

  1. 十大问题诠释冯国华缘何入主金蝶
  2. Maven学习 使用Nexus搭建Maven私服(转)
  3. Java内存分配原理
  4. android拍照识别人脸,Android 人脸识别拍照demo
  5. 计算机大赛横幅标语有趣的,有趣的横幅标语
  6. 写给大忙人的维生素一览表【饮食健康规律】
  7. 拓端tecdat|R语言中的隐马尔可夫HMM模型实例
  8. 拨打freeswitch会议室:mod_local_stream.c:880 Unknown source default
  9. 数学分析练习题答案一(自己做的)
  10. matlab作图有拉盖尔,拉盖尔高斯光束matlab
  11. 百度地图jsApi,地图拖动,中心定位图标不动,准确获取拖动过后中心定位图标所在位置
  12. 剑指XX游戏(二) - 网易2011笔试题详解
  13. SQL 注释语句 (--与/*...*/)
  14. python改word域_python修改word
  15. 作业二:词云制作 使用软件wordart
  16. 一款小巧的kafka测试工具
  17. 中国移动企业短信通平台EMPP协议分析
  18. 使用JS代码简单实现九九乘法表
  19. 手把手教你如何修改JDK的版本问题
  20. 开源O2OA车辆管理:高效利用公车资源,提高使用效率

热门文章

  1. Deepgreen Greenplum 高可用(一) - Segment节点故障转移
  2. 数据库应用-后缀树及后缀数组(Suffix-BäumeSuffix-Arraz)-2
  3. 北大青鸟太原南内环校区
  4. Ubuntu安装AMD/ATI显卡驱动
  5. 【MIUI】MIUI Issues
  6. 用计算机制作数学PPT课件,使用flash制作数学课件经典案例.ppt
  7. 【无为则无心Python基础】— 12、Python的变量
  8. Docker 常见命令
  9. oracle分区表加索引,oracle分区表增加索引
  10. HTTP之长连接、短连接、持久连接