011
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using namespace std;
int main()
{
int n,count;
cin>>n;
if(n>=2){
printf("%d ",2);
count++;
}
for(int i=0;i<n;i++){
for(int j=2;j*j<i;j++){
if(i%j==0){
continue;
}
else{
printf("%d ",i);
count++;
if(count%10==0){
printf("\n");
}
}
}
}
}
031
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using namespace std;
int main()
{
string s;
cin>>s;
string g=s;
for(int i=0;i<s.size();i++){
g[s.size()-i-1]=s[i];
}
if(s==g){
printf("回文");
}
else{
printf("非回文");
}
}
顺序表
1 |
|
顺序表
1 |
|
041
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
struct lnode
{
int ID;
struct lnode *next;
};
void BubbleSort(struct lnode * head);
int main()
{
int n, i, d;
while(scanf("%d", &n) != EOF)
{
//初始化带头节点的链表
struct lnode *head, *s, *r, *p;
r = head;
for(i = 0; i < n; i ++)
{
scanf("%d", &d);
s -> ID = d;
r -> next = s;
r = s;
}
r -> next = NULL;
//冒泡排序
BubbleSort(head);
//打印输出
for(p = head -> next; p != NULL; p = p -> next)
{
if(p -> next == NULL)
{
printf("%d\n", p -> ID);
}else
{
printf("%d ", p -> ID);
}
}
}
return 0;
}
void BubbleSort(struct lnode *head)
{
struct lnode *f, *p, *x, *y;
f = NULL;
if(head -> next == NULL || head -> next -> next == NULL)
{
return;
}
while(f != head->next->next)
{
for(p = head; p -> next -> next != f; p = p -> next)
{
if(p -> next -> ID > p -> next -> next ->ID)
{
x = p -> next;
y = p -> next -> next;
p -> next = y;
x -> next = y -> next;
y -> next = x;
}
}
f = p -> next;
}
}
041
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using namespace std;
struct Node{
char Element;
int length;
Node *last,*next;
};
typedef char ElementType;
typedef Node *List;
List L;
List CreateList(List &L)
{
List I=(List)malloc(sizeof(Node));
int length=0;L->next=L->last=nullptr;
return I;
}
void insect(List &L,ElementType a)
{
L->length++;
Node*n=(Node *)malloc(sizeof(Node));
n->Element=a;
if(L->next==nullptr){
L->last=n;L->next=n;
n->last=n;n->next=n;
}
else{
L->last->next;//L原来的最后一个节点的next指向新最后一个节点
n->last=L->last;//新最后一个节点的last指向旧最后一个节点
n->next=L->next;//新最后一个节点指向第一个节点(循环链表)
L->next->last=n;//第一个节点指向最后一个节点
L->last=n;//指定最后一个节点
}
}
void print(List &L)
{
int n=L->length;
Node *Node=L->next;
for(int i=0;i<n;i++){
cout<<Node->Element;
Node=Node->next;
}
}
void output3th(List &L)
{
Node *Node=L->next;
for(int i=0;i<3;i++){
Node=Node->next;
}
cout<<Node->Element<<endl;
}
void location(List &L)
{
char a;
Node *Node=L->next;
for(int i=1;i<=L->length;i++){
if(Node->Element==a){
cout<<"a location"<<i;
}
else
Node=Node->next;
}
}
void insect4f(List &L)
{
Node *m=(Node *)malloc(sizeof(Node));
Node *Node=L->next;
for(int i=1;i<=3;i++){
Node->next=m;
L->last->last->last=m;
m->last=Node->next;
m->next=L->last->last;
}
}
void delete3th(List &l)
{
Node *tmp,*Node=L->next;
for(int i=1;i<=3;i++){
Node=Node->next;
}
tmp=Node;free(tmp);//删掉啦第三个节点
L->next->next->next=L->last->last;//第二个节点的next指向第四个节点
L->last->last->last=L->next->next;//第四个节点的last指向第二个节点
}
void DestroyList(List &L)
{
Node *tmp,*Node=L->next;
for(int i=0;i<L->length;i++){
tmp=Node;
Node=Node->next;
free(tmp);
}
free(L);
}
int printlength(List &L)
{
return L->length;
}
int main()
{
char a,b,c,d,e,f;
List L=CreateList(L);
insect(L,a);insect(L,b);insect(L,c);insect(L,d);insect(L,e);
print(L);
cout<<L->length<<endl;
cout<<printlength(L)<<endl;
output3th(L);
location(L);
insect4f(L);
print(L);
delete3th(L);
print(L);
DestroyList(L);
}
051
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
typedef Node2 *HList;
//1.数据储存
//创建存储结构
typedef int ElemType;
typedef struct Node1
{
ElemType data[MaxCol];
struct Node1 *next;
}DList;
//指定每个表的行列数
typedef struct Node2
{
int Row, Col;
DList *next;
}HList;
//2.运算方法
//建表
void CreateTable(HList)
{
int i, j;
DList *r, *s;
h = (HList *)malloc(sizeof(HList));
printf("表的行列数:\n");
scanf("%d%d", &h->Row, &h->Col);
for (int i = 0; i < h->Row; i++) {
printf("第%d行:", i + 1);
s = (DList *)malloc(sizeof(DList);
for (int j = 0; j->h->Col; j++) {
scanf("%d", &s->data[j]);
if (h->next == NULL)
h->next = s;
else
r->next = s;
r = s;
}
r->next=NULL;
}
}
//销毁单链表
void DestroyTable(HList *h)
{
DList *pre = h->next, *p = pre->next;
while (p!=NULL)
{
free(pre);
pre = p;
p = p->next;
}
free(pre);
free(h);
}
//输出单链表
void DispTable(HList *h)
{
int j;
DList *p = h->next;
while (p!=NULL)
{
for (j = 0; j < h->Col; j++) {
printf("%4d", p->data[j]);
printf("\n");
p = p->next;
}
}
}
//表连接运算法
void LinkTable(HList *h1,HList *h2,HList *h)
{
int i, j;
DList *p = h->next, *q, *s, *r;
printf("连接字段是:第一个表序号,第二个表序号:");
scanf("%d%d", &i, &j);
h = (HList)malloc(sizeof(HList));
h->Row = 0;
h->Col = h1->Col + h2->Col;
h->next = NULL;
while (p!=NULL)
{
q = h2->next;
while (q != NULL)
{
if (p->data[i - 1] == q->data[j - 1]) {
s = (DList)malloc(sizeof(HList));
for (int k= 0; k < h1->Col; k++)
s->data[k] = p->data[k];
for (int k = 0; k < h2->Col; k++)
s->data[h1->Col + k] = q->data[k];
if (h->next == NULL)
h->next = s;
else
r->next = s;
r = s;
h->Row++;
}
p = p->next;
}
r->next = NULL;
}
}
void main()
{
HList *h1, *h2, *h;
printf("表1:、n");
CreateTable(h1);
printf("表2:\n");
CreateTable(h2);
LinkTable(h1, h2, h);
printf("链接结果表:\n");
DispTable(h);
DestroyTable(h1);
DestroyTable(h2);
DestroyTable(h);
}
061
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#include<iostream>
using namespace std;
typedef struct
{
int i, j;
int pre;
}Box;
typedef struct
{
Box data[10];
int front, rear;
}QuType;
bool mgpath1(int xi, int yi, int xe, int ye)
{
int i, j, find = 0, di;
QuType qu;
qu.front = qu.rear = -1;
qu.rear++;
qu.data[qu.rear].i = xi; qu.data[qu.rear].j = yi; //(xi, yi)进队
qu.data[qu.rear].pre = -1;
mg[xi][yi] = -1;
while (qu.front != qu.rear && !find)
{
qu.front++;
i = qu.data[qu.front].i; j = qu.data[qu.front].j;
if (i == xe&&j == ye)
{
find = 1;
print(qu, qu.front);
return true;
}
for (di = 0; di < 4; di++) {
switch (di)
{
case 0:i = qu.data[qu.front].i - 1; j = qu.data[qu.front].j; break;
case 1:i = qu.data[qu.front].i; j = qu.data[qu.front].j + 1; break;
case 2:i = qu.data[qu.front].i + 1; j = qu.data[qu.front].j; break;
case 3:i = qu.data[qu.front].i; j = qu.data[qu.front].j - 1; break;
}
if (mg[i][j] == 0) {
qu.rear++;
qu.data[qu.rear].i = i; qu.data[qu.rear].j = j;
qu.data[qu.rear].pre = qu.front;
mg[i][j] = -1;
}
}
}
return false;
}
//从队列qu中输出路径
void print(QuType qu, int front)
{
int k = front, j, ns = 0;
printf("\n");
do {
j = k;
k = qu.data[k].pre;
qu.data[j].pre = -1;
} while (k != 0);
printf("迷宫路径如下:、n");
k = 0;
while (k<10)
{
if (qu.data[k].pre == -1) {
ns++;
printf("\t(%d,%d)", qu.data[k].i, qu.data[k].j);
if (ns % 5 == 0) printf("\n");
}
k++;
}
printf("\n");
}
void main()
{
int M, N;
cin >> M >> N;
if (!mgpath1(1, 1, M, N))
printf("迷宫误解");
}
071
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
using namespace std;
typedef string SqString;
int index(SqString s, SqString t)
{
unsigned int i = 0, j = 0;
while ( i<s.length()&&j<t.length())
{
if (s[i] == t[j]) {
i++; j++;
}
else
{
i = i - j + 1;
j = 0;
}
if(j<=t.length())
break;
}
if (j <= t.length()){
return(i - t.length());}
else{
return(-1);}
}
int main()
{
string s = "aaaaab";
string t = "aaab";
index(s, t);
return 0;
}
08`
//创建二叉树
#include<btree.h>
void CreatBTNode(BTnode &b,char str)
{
BTNode St[MaxSize],p;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while(ch!=’\0’)
{switch(ch)
{
case ‘(‘:top++,St[top]=p;k=1;break;
case ‘)’:top–;break;
case ‘,’:k=z;break;
default:p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;p->lchild=p->rchild=NULL;
if(b==NULL)
b=p;
else
{
switch(k)
{
case1:St[top]->lchild=p;break;
case2:St[top]->rchild=p;break;
}
}
}
j++;ch=str[j];
}
}
//查找节点
/递归模型
f(b,x)=NULL
f(b,x)=b
f(b,x)=p
f(b,x)=f(b->rchild,x)
/
//对应的递归算法
BTNode FindNode(BTNode b,ElemType x)
{ BTNode *p;
if(b==NULL)
return NULL;
else if(b->data==x)
return b;
else
{ p=FindNode(b->lchild,x);
if(p!=NULL)
return p;
else
return FindNode(b->rchild,x);
}
}
//找孩子节点
BTNode lchildNode(BTNode p)
{
return p->lchild;
}
BTNode rchildNode(BTNode p)
{
return p->rchild;
}
//求高度
/递归模型
f(b)=0
f(b)=MAX{f(b->lchild),f(b->rchild)}
/
//递归算法
int BTNodeHeight(BTNode *b)
{ int lchild,rchild;
if(b==NULL) return 0;
else
{ lchild= BTNodeHeight(b->lchild);//求左子树的高度
rchild=BTNodeHeight(b->rchild);
return (lchild>rchild?(lchild+1:rchild+1));
}
}
//输出二叉树
void DispBTNode(BTNode *b)
{ if(b!=NULL)
{ printf(“%c”,b->data);
if(b->lchild!=NULL||b->rchild!=NULL)
{ printf(“(\n” );
DispBTNode(b->lchild);
if(b->rchild!=NULL) printf(“,\n” );
DispBTNode(b->rchild);
printf(“)\n” );
}
}
}
//二叉树的遍历
//先序遍历
void PreOrder(BTNode b)
{ if(b!=NULL)
{ printf(“%c\n”,b->data );
PreOrder(b->lchild);
PreOrder(b->rchild);
}
}
//中根遍历
void InOrder(BTNode b)
{ if(b!=NULL)
{
InOrder(b->lchild);
printf(“%c\n”,b->data );
InOrder(b->rchild);
}
}
//后根遍历
void PostOrder(BTNode b)
{ if(b!=NULL)
{
PostOrder(b->lchild);
PostOrder(b->rchild);
printf(“%c\n”,b->data );
}
}
void main()
{
BTNode b,p,lp,*rp;
CreatBTNode(b,”A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))”);
printf(“二叉树的基本运算”);
printf(“(1)输出二叉树:”); DispBTNode(b);printf(“\n” );
}