课堂练习

平台

SCU自家题库,大二上学期数据结构与算法课题目。

第一周习题

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
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100;
int a[maxn];
int main()
{
int n;cin >> n;
for(int i = 0;i < n;++ i)
{
cin >> a[i] ;
}
int m;
cin >> m;
int tmp = -1;
for(int i = 0;i < n;++ i)
{
if(a[i] == m)
{
tmp = i;
break;
}
}
int ans = 0;
for(int i = tmp+1;i < n;++ i)
{
if(a[i] > m){
ans ++ ;
}
}
if(tmp == -1)
{
cout << "none";
}else{
cout << ans;
}

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100;
int a[maxn];
int b[maxn];
int c[maxn];
int main()
{
int n,m;
cin >> n >> m;
for(int i = 0;i < n;++ i)
{
cin >> a[i];
}
for(int i = 0;i < m;++ i)
{
cin >> b[i];
}
int pa = 0;
int pb = 0;
int pc = 0;
while(pa < n || pb < m)
{
if(pa >= n)
{
c[pc++] = b[pb++];
continue;
}
if(pb >= m){
c[pc++] = a[pa++];
continue;
}
if(a[pa] < b[pb])
{
c[pc++] = a[pa++];
}else{
c[pc++] = b[pb++];
}
}
for(int i = 0;i < pc;++ i)
{
cout << c[i] << " \n"[i==pc];
}

return 0;
}
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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include<bits/stdc++.h>  
using namespace std;

const int maxn = 2;
char font[maxn][maxn] = {{'R', 'R'}, {'R', 'R'}};
char leftt[maxn][maxn] = {{'B', 'B'}, {'B', 'B'}};
char rightt[maxn][maxn] = {{'G', 'G'}, {'G', 'G'}};
char top[maxn][maxn] = {{'Y', 'Y'}, {'Y', 'Y'}};
char down[maxn][maxn] = {{'W', 'W'}, {'W', 'W'}};
char back[maxn][maxn] = {{'O', 'O'}, {'O', 'O'}};
char op[1000];
//void getprint(char (*data)[maxn])
//{
// for(int i = 0; i < maxn; ++i)
// {
// for(int j = 0; j < maxn; ++j)
// {
// cout << data[i][j] << " \n"[j==maxn];
// }
// }
//}
void printans()
{
printf(" ");
printf("%c %c\n",top[0][0],top[0][1]);
printf(" ");
printf("%c %c\n",top[1][0],top[1][1]);

printf("%c %c %c %c %c %c %c %c\n",leftt[0][0],leftt[0][1],font[0][0],font[0][1],rightt[0][0],rightt[0][1],back[0][0],back[0][1]);
printf("%c %c %c %c %c %c %c %c\n",leftt[1][0],leftt[1][1],font[1][0],font[1][1],rightt[1][0],rightt[1][1],back[1][0],back[1][1]);

printf(" ");
printf("%c %c\n",down[0][0],down[0][1]);
printf(" ");
printf("%c %c",down[1][0],down[1][1]);
}
//void swap(char *a, char *b) {
// char c;
// c = *a;
// *a = *b;
// *b = c;
//}
void getswapF(char (*data1)[maxn],char (*data2)[maxn],char (*data3)[maxn],char (*data4)[maxn])
{
//上、右、下、左
char tmp1 = data4[1][1];
char tmp2 = data4[0][1];

//下->左
data4[0][1] = data3[0][0];
data4[1][1] = data3[0][1];

//右->下
data3[0][1] = data2[0][0];
data3[0][0] = data2[1][0];

//上->右
data2[0][0] = data1[1][0];
data2[1][0] = data1[1][1];

//左->上
data1[1][0] = tmp1;//(data4[1][1])
data1[1][1] = tmp2;//(data4[0][1])
}
void getswapf(char (*data1)[maxn],char (*data2)[maxn],char (*data3)[maxn],char (*data4)[maxn])
{
//上、右、下、左
//保存上
char tmp1 = data1[1][0];
char tmp2 = data1[1][1];

//右->上
data1[1][0] = data2[0][0];
data1[1][1] = data2[1][0];

//下->右
data2[1][0] = data3[0][0];
data2[0][0] = data3[0][1];

//左->下
data3[0][0] = data4[0][1];
data3[0][1] = data4[1][1];

//上->左
data4[1][1] = tmp1;//(data1[1][0])
data4[0][1] = tmp2;//(data1[1][1])
}
void getswapU(char (*data1)[maxn],char (*data2)[maxn],char (*data3)[maxn],char (*data4)[maxn])
{
//U:后、左、前、右
char tmp1 = data1[0][0];
char tmp2 = data1[0][1];

//前->右
data1[0][0] = data2[0][0];
data1[0][1] = data2[0][1];

//左->前
data2[0][0] = data3[0][0];
data2[0][1] = data3[0][1];

//后->左
data3[0][0] = data4[0][0];//!!!
data3[0][1] = data4[0][1];//!!!

data4[0][0] = tmp1;//!!!data1[0][0]
data4[0][1] = tmp2;//!!!data1[0][1]
}
void getswapR(char (*data1)[maxn],char (*data2)[maxn],char (*data3)[maxn],char (*data4)[maxn])
{
char tmp1 = data1[0][1];
char tmp2 = data1[1][1];
//前、上、后、下
//下->前
data1[0][1] = data4[0][1];
data1[1][1] = data4[1][1];

//后->下
data4[1][1] = data3[0][0]; //!!!
data4[0][1] = data3[1][0]; //!!!

//上->后
data3[0][0] = data2[1][1];
data3[1][0] = data2[0][1];

//前-上
data2[0][1] = tmp1;//data1[0][1]
data2[1][1] = tmp2;//data1[1][1]
}
void getswapr(char (*data1)[maxn],char (*data2)[maxn],char (*data3)[maxn],char (*data4)[maxn])
{

//前、上、后、下
//保存前
char tmp1 = data1[0][1];
char tmp2 = data1[1][1];

//上->前
data1[0][1] = data2[0][1];
data1[1][1] = data2[1][1];

//后->上
data2[0][1] = data3[1][0]; //!!!
data2[1][1] = data3[0][0]; //!!!

data3[1][0] = data4[0][1];
data3[0][0] = data4[1][1];

data4[0][1] = tmp1;//data1[0][1]
data4[1][1] = tmp2;//data1[1][1]
}
void adjust(char (*data)[maxn],int op)
{
char tmp = data[0][0];
if(op == 1){
data[0][0] = data[1][0];
data[1][0] = data[1][1];
data[1][1] = data[0][1];
data[0][1] = tmp;
}else{
data[0][0] = data[0][1];
data[0][1] = data[1][1];
data[1][1] = data[1][0];
data[1][0] = tmp;
}
}
int main()
{
scanf("%s",op);
for(int i = 0;i < strlen(op);++ i)
{
if(op[i] == 'F')
{
adjust(font,1);//顺时针旋转正面
getswapF(top,rightt,down,leftt);
}
if(op[i] == 'f')
{
adjust(font,0);//逆时针旋转正面
getswapf(top,rightt,down,leftt);
}
if(op[i] == 'u')
{
adjust(top,0);//逆时针旋转顶面
getswapU(rightt,font,leftt,back);
}
if(op[i] == 'U')
{
adjust(top,1);//顺时针旋转顶面
getswapU(back,leftt,font,rightt);
}
if(op[i] == 'R')
{
adjust(rightt,1);//顺时针旋转右面
getswapR(font,top,back,down);
}
if(op[i] == 'r')
{
adjust(rightt,0);//逆时针旋转右面
getswapr(font,top,back,down);
}
}
printans();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<bits/stdc++.h>
using namespace std;
map<int,int> mp;
int main(){
int n,x;
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d",&x);
mp[x]++;
}
for(int i=-10000;i<=10000;i++){
while(mp[i]>0){
mp[i]--;
printf("%d ",i);
}
}
return 0;
}

是对C语言的复习,同时也为后边做了铺垫。

数组合并:这种有序合并方式是(第十章)归并排序的核心操作之一。
二阶魔方:模拟题,模拟方式是数组元素的交换,考验耐心。
队列:典型桶排序(第十章)。

第一次作业(国庆)

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
#include<bits/stdc++.h>
using namespace std;
typedef struct Node* List;
struct Node{
int data;
List next;
};
List Read()
{
List head,tail;
head = (List)malloc(sizeof(struct Node));
tail = head;
head->next = NULL;
int n;
cin >> n;
while(n != -1)
{
List add = (List)malloc(sizeof(struct Node));
add->data = n;
tail->next = add;
tail = add;
cin >> n;
}
tail->next = NULL;
return head;
}
List Merge(List L1, List L2) {
List head = (List)malloc(sizeof(struct Node));
if (head == NULL) {
return NULL;
}
head->next = NULL;
List L = head;
L1 = L1->next;
L2 = L2->next;

while (L1 != NULL && L2 != NULL) {
if (L1->data <= L2->data) {
L->next = L1;
L = L->next;
L1 = L1->next;
} else {
L->next = L2;
L = L->next;
L2 = L2->next;
}
}

while (L1 != NULL) {
L->next = L1;
L = L->next;
L1 = L1->next;
}

while (L2 != NULL) {
L->next = L2;
L = L->next;
L2 = L2->next;
}

return head;
}
void Print(List L3)
{
List L = L3;
while(L->next != NULL)
{
cout << L->next->data << " ";
L = L->next;
}
}
int main()
{
List L1 = Read();

List L2 = Read();
List L3 = Merge(L1,L2);
Print(L3);

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
typedef struct Node* List;
struct Node{
int data;
List next;
};
List Read()
{
List head,L;
head = (List)malloc(sizeof(struct Node));
head->next = NULL;
L = head;

int n;
cin >> n;
while(n != -1)
{
List add = (List)malloc(sizeof(struct Node));
add->data = n;
add->next = NULL;
L->next = add;
L = L->next;
cin >> n;
}
L->next = NULL;
return head;
}
void Print(List L1)
{
List L = L1;
L = L->next;
while(L != NULL)
{
cout << L->data << " ";
L = L->next;
}
}
void Delete(List &Lpre,List& L)
{
Lpre->next = L->next;
free(L);
L = Lpre->next;
}
void Unique(List L)
{
List L1 = L;
L1 = L1->next;
while(L1 != NULL)
{
List L2 = L1->next;
List L3 = L1;
int cnt = 0;
while(L2)
{
// cnt ++ ;
// cout << L2->data << " "<< L1->data << endl;
if(L2->data == L1->data)
{
Delete(L3,L2);
}else{
L3 = L2;
L2 = L2->next;
}
//
// if(cnt > 100 ) break;
}

L1 = L1->next;
}
}

int main()
{
List L1 = Read();
// Print(L1);
Unique(L1);
Print(L1);
return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
typedef struct Node* List;
struct Node{
int data;
List next;
};
List Read()
{
List L,head;
head = (List)malloc(sizeof(struct Node));
head->next = NULL;
L = head;

int n;cin >> n;
while(n != -1)
{
List add = (List)malloc(sizeof(struct Node));
add->data = n;
add->next = NULL;
L->next = add;
L = L->next;
cin >> n;
}
L->next = NULL;
return head;
}
void Del(List &Lpre,List &L)
{
Lpre->next = L->next;
free(L);
L = Lpre->next;
}
void Print(List L)
{
List L1 = L;
L1 = L1->next;
while(L1 != NULL)
{
cout << L1->data << " ";
L1 = L1->next;
}
}
void Delete(List L,int del)
{
List L1 = L;
L1 = L1->next;
List L2 = L;
while(L1 != NULL)
{
if(L1->data == del)
{
Del(L2,L1);
}else{
L2 = L1;
L1 = L1->next;
}
}
}
int main()
{
List L = Read();
int del;cin >> del;
Delete(L,del);
Print(L);

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
typedef struct Node* List;
struct Node{
int data;
List pre;
List next;
};
int countt;
List Read()
{
List head,L;
head = (List)malloc(sizeof(struct Node));
head->next = NULL;
head->data = -1;
L = head;
int n;cin >> n;
while(n!=-1)
{
List add = (List)malloc(sizeof(struct Node));
add->data = n;
add->next = NULL;
L->next = add;
add->pre = L;
L = L->next;
cin >> n;
}
L->next = head;
head->pre = L;
return L;
}
void getreverse(List L,int *a)
{
List X = L;
countt = 0;
while(X->data != -1)
{
a[countt] = X->data;
countt++ ;
X = X->pre;
}
}
int main()
{
List L = Read();
int a[10010];
memset(a,-1,sizeof(a));
getreverse(L,a);
for(int i = 0;i < countt;++ i)
{
cout << a[i] << " " ;
}
return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
typedef struct Node* List;
struct Node{
int data;
int index;
List next;
};
List Read()
{
List head,L;
head = (List)malloc(sizeof(struct Node));
head->next = NULL;
L = head;

int n;
cin >> n;
int countt = 0;
while(n != -1)
{
List add = (List)malloc(sizeof(struct Node));
add->data = n;
add->next = NULL;
add->index = countt++ ;
L->next = add;
L = L->next;
cin >> n;
}
L->next = NULL;
return head;
}
void Print(List L1)
{
List L = L1;
L = L->next;
while(L != NULL)
{
cout << L->data << " ";
L = L->next;
}
}
List Operate(List L1,List L2,int n,int m)
{
List nL = L1;
List npreL = L1;
nL = nL->next;
while(nL!=NULL&&nL->index != n)
{
npreL = nL;
nL = nL->next;
}

List mL = L1;
mL = mL->next;
while(mL!=NULL&&mL->index != m)
{
mL = mL->next;
}

while(nL!=mL)
{
List s = nL;
nL = nL->next;
free(s);
}
List tail = L2->next;
while(tail->next != NULL)
{
tail = tail->next;
}

npreL->next = L2->next;
tail->next = mL->next;
return L1;
}
int main()
{
List L1 = Read();
int n,m;
cin >> n >> m;
List L2 = Read();
List L3 = Operate(L1,L2,n,m);
Print(L3);
return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
typedef struct Node* List;
int countt = 0;
int ans = 0;
struct Node{
int data;
int mi;
List next;
};
List Read()
{
List head,tail;
head = (List)malloc(sizeof(struct Node));
tail = head;
head->next = NULL;
int n;
cin >> n;
while(n != -1)
{
List add = (List)malloc(sizeof(struct Node));
add->data = n;
add->mi = countt++ ;
tail->next = add;
tail = add;
cin >> n;
}
tail->next = NULL;
return head;
}
void Print(List L3)
{
List L = L3;
L = L->next;
while(L!=NULL)
{
int mi = countt - L->mi - 1;
if(L->data)
{

ans += pow(2,mi);
}
// cout << L->data << " " << mi;
// cout << endl;
L = L->next;
}
cout << ans << endl;
}
int main()
{
List L = Read();
Print(L);

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
typedef struct Node* List;
struct Node{
int data;
int index;
List pre;
List next;
};
List Read()
{
List head,L;
head = (List)malloc(sizeof(struct Node));
head->next = NULL;
L = head;

int n;
cin >> n;
int countt = 1;
while(n != -1)
{
List add = (List)malloc(sizeof(struct Node));
add->data = n;
add->index = countt++ ;
add->next = NULL;
L->next = add;
add->pre = L;
L = L->next;
cin >> n;
}
L->next = NULL;
return head;
}
void Print(List L1)
{
List L = L1;
L = L->next;
while(L != NULL)
{
cout << L->data << " ";
L = L->next;
}
}
void Operate(List L1,int n,int m)
{
List nL = L1;
List nLpre = L1;
nL = nL->next;
while(nL!=NULL && nL->index != n){
nLpre = nL;
nL = nL->next;
}
List mL = L1;
List mLnxt = L1;
mL = mL->next;
while(mL!=NULL && mL->index != m){
mL = mL->next;
}
mLnxt = mL->next;


nLpre->next = mL;
List L2 = mL;
while(L2->index != n)
{
L2->next = L2->pre;
L2 = L2->pre;
}
L2->next = nL;
nL->next = mLnxt;

}
int main()
{
List L1 = Read();
int n,m;
cin >> n >> m;
Operate(L1,n,m);
Print(L1);
return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
typedef struct Node* List;
int countt = 1;
struct Node{
int data;
int index;
List next;
};
List Read()
{
List head,L;
head = (List)malloc(sizeof(struct Node));
head->next = NULL;
L = head;

int n;
cin >> n;
while(n != -1)
{
List add = (List)malloc(sizeof(struct Node));
add->data = n;
add->index = countt++ ;
add->next = NULL;
L->next = add;
L = L->next;
cin >> n;
}
L->next = NULL;
return head;
}
void Print(List L1)
{
List L = L1;
L = L->next;
while(L != NULL)
{
cout << L->data << " ";
L = L->next;
}
}
int main()
{
List L1 = Read();
int reDel;cin >> reDel;
int Del = countt - reDel ;
List L = L1;
List preL = L;
L = L->next;

while(L != NULL && L->index != Del)
{
preL = L;
L = L->next;
}
preL->next = L->next;
free(L);
Print(L1);

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
typedef struct Node* List;
struct Node{
int data;
List pre;
List next;
};
List Read()
{
List head,L;
head = (List)malloc(sizeof(struct Node));
head->next = NULL;
L = head;

int n;
cin >> n;
int countt = 1;
while(n != -1)
{
List add = (List)malloc(sizeof(struct Node));
add->data = n;
add->next = NULL;
L->next = add;
add->pre = L;
L = L->next;
cin >> n;
}
L->next = NULL;
return head;
}
void Print(List L1)
{
List L = L1;
L = L->next;
while(L != NULL)
{
cout << L->data << " ";
L = L->next;
}
}
void Operate(List L)
{
List pre,tail;
pre = L->next;
tail = L->next;
while(tail->next!=NULL)
{
tail = tail->next;
}
List arrange = L;
int i = 1;
while(pre != tail)
{
if(i)
{
arrange->next = pre;
pre = pre->next;
arrange = arrange->next;

}else{
arrange->next = tail;
tail = tail->pre;
arrange = arrange->next;
}
i = 1-i;
}
arrange->next = pre;
arrange = arrange->next;
arrange->next = NULL;
}
int main()
{
List L1 = Read();
Operate(L1);
Print(L1);
return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
typedef struct Node* List;
struct Node{
int data;
List pre;
List next;
};
int ans = 0;
List Read()
{
List head,L;
head = (List)malloc(sizeof(struct Node));
head->next = NULL;
L = head;

int n;
cin >> n;
int countt = 1;
while(n != -1)
{
List add = (List)malloc(sizeof(struct Node));
add->data = n;
add->next = NULL;
L->next = add;
add->pre = L;
L = L->next;
cin >> n;
}
L->next = NULL;
return head;
}
void Operate(List L)
{
List pre,tail;
pre = L->next;
tail = L->next;
while(tail->next!=NULL)
{
tail = tail->next;
}
int temp = 0;
while(pre->next != tail->pre && pre->next && tail->pre)
{
ans = max(ans,pre->data+tail->data);
pre = pre->next;
tail = tail->pre;
}
}
int main()
{
List L1 = Read();
Operate(L1);
cout << ans ;
return 0;
}

第二次作业

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
#include<bits/stdc++.h>
using namespace std;

typedef struct stk* stackk;
struct stk{
char c;
stackk nex;
stackk prior;
};
const int maxn = 1e3+10;
char a[maxn];
stackk init()
{
stackk head;
head = (stackk)malloc(sizeof(struct stk));
head->nex = NULL;
head->prior = NULL;
return head;
}
void Push(stackk L,char s)
{
stackk add;
add = (stackk)malloc(sizeof(struct stk));
add->c = s;

stackk tail = L;
while(tail->nex)
{
tail = tail->nex;
}
add->prior = tail;
tail->nex = add;
add->nex = NULL;
}
void Print(stackk L)
{
L = L->nex;
while(L)
{
printf("%c",L->c);
L = L->nex;
}
}
void Judge(stackk L)
{
stackk head = L->nex;
stackk tail = head;
while(tail->nex)
{
tail = tail->nex;
}
int mark = 1;
while(head != tail && head->nex != tail)
{
if(head->c != tail->c)
{
mark = 0;
break;
}
head = head->nex;
tail = tail->prior;
}
if(mark)
{
printf("true\n");
}else{
printf("false\n");
}
}
int main()
{
stackk L1 = init();
char c;
scanf("%c",&c);
while(c!='\n' && c!='\r')
{
Push(L1,c);
scanf("%c",&c);
}
Judge(L1);
// Print(L1);
return 0;
}
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
#include<bits/stdc++.h>
using namespace std;

typedef struct stk* S;
struct stk{
int data;
S top;
};
int ans = 0;
void Push(S &S1,int data,S &head)
{
S add = (S)malloc(sizeof(struct stk));
add->data = data;
add->top = S1;
S1 = add;

head->data++ ;
ans += head->data*data;
}
bool StackEmpty(S S1)
{
if(S1->top==NULL){
return true;
}else{
return false;
}
}
int main()
{
S S1 = (S)malloc(sizeof(struct stk));
S top = (S)malloc(sizeof(struct stk));
S1->data = 0;
S1->top = NULL;
top->top = S1;
int data;
scanf("%d",&data);

top->data = data;
S1->data ++ ;
ans += S1->data*data;

while(data!=0)
{
scanf("%d",&data);
Push(top,data,S1);
}
printf("%d\n",ans);
while(!StackEmpty(top))
{
if(top->data != 0)
{
printf("%d ",top->data);
}
top = top->top;
}


return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
typedef struct stk* StackList;
struct stk{
char c;
StackList top;
};
string ans;
void Push(StackList& Ptop,char s)
{
StackList node = (StackList)malloc(sizeof(struct stk));

node->c = s;
node->top = Ptop;
Ptop = node;
}
void Pop(StackList & Ptop)
{

StackList Pointer = Ptop->top;
free(Ptop);
Ptop = Pointer;
}
void Print(StackList & top)
{
while(top->top != NULL)
{
// printf("%c",top->c);
ans += top->c;
top = top->top;
}
}
int main()
{
StackList top = (StackList)malloc(sizeof(struct stk));
top->top = NULL;

string s;
cin >> s;
if(s == "")
{
return 0;
}
char pre = s[0];
for(int i = 0;i < s.length();++ i){
if(pre == s[i] && i > 0)
{
Pop(top);
pre = top->c;
continue;
}
Push(top,s[i]);
pre = top->c;
}
Print(top);
if(ans.empty())
{
cout << "0" ;
}else{
reverse(ans.begin(),ans.end());
cout << ans;
}

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
typedef struct {
int *base;
int front;
int rear;
}SqQueue;

int m,n;
const int maxn = 1e3 + 10;
int init(SqQueue & Q,int x)
{
Q.base = new int[maxn];
if(!Q.base)
{
return false;
}
Q.front = Q.rear = 0;

if((Q.rear + 1) % maxn == Q.front)
{
return false;
}
for(int i = 1;i <= x;++ i)
{
Q.base[Q.rear] = i;
Q.rear = (Q.rear + 1) % maxn;
}

return true;
}
int main()
{

cin >> m >> n;
int k;
cin >> k;
SqQueue Qm;
SqQueue Qn;
init(Qm,m);
init(Qn,n);
int i = Qm.front;
int j = Qn.front;
while(k--)
{
cout << Qm.base[i] << " " << Qn.base[j] <<endl;
i = (i + 1) % maxn;
if(i == Qm.rear)
{
i = Qm.front;
}
j = (j + 1) % maxn;
if(j == Qn.rear)
{
j = Qn.front;
}
}


return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+10;
int students[maxn];
int sandwiched[maxn];
//void getprint(int qp,int countt)
//{
// while(qp != countt+1)
// {
// cout << students[qp++] << " " ;
// }
// cout << endl;
//}
int main()
{
int countt = 0;
int a;
while(1)
{
scanf("%d",&a);
if(a == -1)
{
break;
}else{
students[countt++] = a;
}
}
countt--;
int qp = 0;


int counttt = 0;
int stkp = 0;
while(1)
{
scanf("%d",&a);
if(a == -1)
{
break;
}else{
sandwiched[counttt++] = a;
}
}
counttt--;

int mark = 1;
int ans = 0;
int breakpoint = 0;
while(1)
{
if(sandwiched[stkp] == students[qp])
{
qp ++ ;
stkp ++ ;
breakpoint = 0;
}else{
// cout << students[qp] << " is moved" << "The next is " << students[qp+1] << endl;

students[++countt] = students[qp++];
breakpoint ++ ;
}
if(breakpoint == countt - qp +1)
{
break;
}

}
cout << counttt - stkp + 1 ;

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin >> n;
stack<int> s;
for(int i = 0;i < n;++ i)
{
string op;cin >> op;
if(op == "push")
{
int x;
cin >> x;
s.push(x);
}else if(op == "pop")
{
if(s.empty()){
cout << "error" << endl;
}else{
cout << s.top() << endl;
s.pop();
}
}else if(op == "top")
{
if(s.empty()){
cout << "error" << endl;
}else{
cout << s.top() << endl;
}
}
}


return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+10;
int pushed[maxn];
int popped[maxn];
int main()
{
int a;
int n = 0;
while(1)
{
scanf("%d",&a);
if(a == -1)break;
pushed[n++] = a;
}

int n1 = 0;
while(1){
scanf("%d",&a);
if(a == -1)break;
popped[n1++] = a;
}

stack<int> stk;
int j = 0;
for (int i = 0;i < n;++ i) {
stk.push(pushed[i]);
while (!stk.empty() && stk.top() == popped[j]) {
stk.pop();
++j;
}
}
if(j == n)
{
cout << "true\n" ;
}else{
cout << "false\n" ;
}

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;

int main()
{
int n,q;
cin >> n >> q;
queue<int> Q;
for(int i = 0;i < q; ++ i)
{
string op;cin >> op;
if(op == "push")
{
int x;cin >> x;
if(Q.size() == n){
cout << "full" << endl;
}else{
Q.push(x);
}

}else if(op == "front"){
if(!Q.empty())
{
cout << Q.front() << endl;
}else{
cout << "empty" << endl;
}
}else if(op == "pop"){
if(!Q.empty())
{
cout << Q.front() << endl;
Q.pop();
}else{
cout << "empty" << endl;
}
}
}


return 0;
}
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
#include<bits/stdc++.h>
using namespace std;

int main()
{
int n,q;
cin >> n >> q;
queue<int> Q;
for(int i = 0;i < q; ++ i)
{
string op;cin >> op;
if(op == "push")
{
int x;cin >> x;
if(Q.size() == n){
cout << "full" << endl;
}else{
Q.push(x);
}

}else if(op == "front"){
if(!Q.empty())
{
cout << Q.front() << endl;
}else{
cout << "empty" << endl;
}
}else if(op == "pop"){
if(!Q.empty())
{
cout << Q.front() << endl;
Q.pop();
}else{
cout << "empty" << endl;
}
}
}


return 0;
}
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
#include<bits/stdc++.h>
using namespace std;

int main()
{
string s;
cin >> s;
int maxn = 0;
stack<char> stk;
int temp = 0;
int i = 0;
stk.push(-1);
for(int i = 0;i < s.length();++ i)
{
if(s[i] == '('){
stk.push(i);
}else {
stk.pop();
if(stk.empty()){
stk.push(i);
}else {
maxn = max(maxn,i - stk.top());
}
}
}


cout << maxn << endl;

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
queue<int>q;
int main()
{
int n;
int k;
cin >> n >> k;
for(int i = 1;i <= n;++ i)
{
q.push(i);
}
int winner = -1;


while(1)
{
if(n == 1)
{
winner = q.front();
break;
}
for(int i = 1;i < k;++ i)
{
int x = q.front();
q.pop();
q.push(x);
}
q.pop();
n--;
}
cout << winner << endl;

return 0;
}

实验课循环链表练习

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
#include<bits/stdc++.h>
using namespace std;

void YangHui(int n)
{
queue<int>Q;
int j,s,t;
printf("第1行:1\n");
Q.push(0);
Q.push(1);
Q.push(1);

for(j = 2;j < n;++ j)
{
Q.push(0);
printf("第%d行:",j);
do{
s = Q.front();
Q.pop();
t = Q.front();
if(t)
{
printf("%d\t",t);
}else{
printf("\n");
}
Q.push(s+t);
}while(t!= 0);
}
Q.pop();
while(!Q.empty())
{
cout << Q.front() << '\t';
Q.pop();
}
}

int main()
{
int n;
while(true)
{
printf("输入行数(小于等于20):");
cin >> n;
if(n <= 20 && n > 1)
{
YangHui(n);
break;
}else{
printf("重新输入");
}
}


return 0;
}
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
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
128
129
130
#include "stdio.h"
#include "malloc.h"
#define OK 1
#define OVERFLOW -1
#define ERROR 0
#define QMAXSIZE 23//定义长度,长度>=输出行数+3
typedef int ElemType;
typedef int Status;

typedef struct
{
ElemType *base; //初始化的动态分配存储空间
int front; //头指针,队列不空指向队列头元素
int rear; //尾指针,队列不空指向队列尾元素下一位置
}SqQueue;

Status InitQueue(SqQueue &Q)
{//构造空队列

Q.base = new ElemType[QMAXSIZE]; //分配数组空间
//Q.base = (QElemType*)MAXQSIZE * sizeof(QElemType); C语言语法
if (!Q.base)
{
exit(OVERFLOW); //存储分配失败
}
Q.front = Q.rear = 0; //头指针尾指针置为0, 队列为空
return true;

}

Status QueueLength(SqQueue Q)
{//获取队列长度

return (Q.rear+QMAXSIZE-Q.front)%QMAXSIZE;
}

void GetHead(SqQueue Q,ElemType &e)
{//返回队头元素
if(Q.front == Q.rear)
e=0;
else
e=Q.base[Q.front];
}
Status QueueEmpty(SqQueue &Q)
{//判断队列是否为空
if(Q.front==Q.rear)
return OK;
else
return ERROR;
}


Status EnQueue(SqQueue &Q,ElemType e)
{//插入元素
if((Q.rear+1) % QMAXSIZE == Q.front)
{
return false;
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear+1) % QMAXSIZE;
return true;
}

Status DeQueue(SqQueue &Q,ElemType &e)
{//删除元素
if (!QueueEmpty(Q)) //如果队列非空
{
e = Q.base[Q.front];//将出栈元素保存到value中
Q.front = (Q.front + 1) % QMAXSIZE; //队头指针+1
return true;
}
return false; //队列为空,出队失败
}



void PrintQueue(SqQueue Q, ElemType n)
{//遍历循环队列,并打印所有元素
while(Q.front != Q.rear)
{
printf("%d\t",Q.base[Q.front]);
Q.front = (Q.front + 1) % QMAXSIZE;
}
}
void YangHui(int n)
{//杨辉三角
SqQueue Q;
ElemType j,s,t;
printf("第1行:1\n");
InitQueue(Q);
EnQueue(Q,0); /*开始*/
EnQueue(Q,1); /*第1行*/
EnQueue(Q,1);
for(j=2;j<n;j++) //从第二行开始循环到n-1行
{
EnQueue(Q, 0); /*第j行的结束符*/
printf("第%d行:",j);
do
{
DeQueue(Q,s);
GetHead(Q,t);
if(t)
printf("%d\t",t); /*非0输出,否则换行*/
else
printf("\n");
EnQueue(Q,s+t);
}
while(t!=0); /*遇到结束符前循环*/
}
DeQueue(Q,s); //输出最后一行
PrintQueue(Q,j);
}

int main()
{
ElemType n;
while(true)
{
printf("请输入输出的行数(小于等于%d):",QMAXSIZE-3);
scanf("%d",&n);
if(n<=(QMAXSIZE-3)&&n>1)
{
YangHui(n);
break;
}
else
printf("输入错误,请重新输入\n");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<bits/stdc++.h>
using namespace std;

int main()
{
stack<int> a;
int x;
scanf("%d",&x);
while(x != 0)
{
a.push(x);
scanf("%d",&x);
}
while(!a.empty())
{
cout << a.top() << " ";
a.pop();
}

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
int n;
const int maxn = 1e4 + 10;
int a[maxn];
int b[maxn];
int main()
{
cin >> n;
for(int i = 0;i < n;++ i)
{
cin >> a[i];
}
for(int i = 0;i < n;++ i)
{
int countt = 0;
for(int j = i;j >= 0;j--)
{
if(a[j] < a[i])
{
countt ++ ;
}
}
b[i] = countt;
}
for(int i = 0;i < n;++ i)
{
cout << b[i] << " ";
}

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;

void YangHui(int n)
{
queue<int>Q;
int j,s,t;
printf("1\n");
if(n==1)
{
return ;
}
Q.push(0);
Q.push(1);
Q.push(1);

for(j = 2;j < n;++ j)
{
Q.push(0);
do{
s = Q.front();
Q.pop();
t = Q.front();
if(t)
{
printf("%d ",t);
}else{
printf("\n");
}
Q.push(s+t);
}while(t!= 0);
}
Q.pop();
while(!Q.empty())
{
cout << Q.front() << " ";
Q.pop();
}
}

int main()
{
int n;
cin >> n;
YangHui(n);


return 0;
}

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
#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin >> n;
stack<int> ans;
while(n != 1)
{
ans.push(n);
if(n&1){
n = n * 3 + 1;
}else{
n = n / 2;
}
}
ans.push(1);
while(!ans.empty())
{
cout << ans.top() << " " ;
ans.pop();
}

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
int n;
string number;
struct num{
string l1;
string l2;
string l3;
string l4;
string l5;
}Data[10];

int main()
{
Data[0].l1 = "XXX";
Data[0].l2 = "X.X";
Data[0].l3 = "X.X";
Data[0].l4 = "X.X";
Data[0].l5 = "XXX";

Data[1].l1 = "..X";
Data[1].l2 = "..X";
Data[1].l3 = "..X";
Data[1].l4 = "..X";
Data[1].l5 = "..X";

Data[2].l1 = "XXX";
Data[2].l2 = "..X";
Data[2].l3 = "XXX";
Data[2].l4 = "X..";
Data[2].l5 = "XXX";


Data[3].l1 = "XXX";
Data[3].l2 = "..X";
Data[3].l3 = "XXX";
Data[3].l4 = "..X";
Data[3].l5 = "XXX";

Data[4].l1 = "X.X";
Data[4].l2 = "X.X";
Data[4].l3 = "XXX";
Data[4].l4 = "..X";
Data[4].l5 = "..X";


Data[5].l1 = "XXX";
Data[5].l2 = "X..";
Data[5].l3 = "XXX";
Data[5].l4 = "..X";
Data[5].l5 = "XXX";

Data[6].l1 = "XXX";
Data[6].l2 = "X..";
Data[6].l3 = "XXX";
Data[6].l4 = "X.X";
Data[6].l5 = "XXX";

Data[7].l1 = "XXX";
Data[7].l2 = "..X";
Data[7].l3 = "..X";
Data[7].l4 = "..X";
Data[7].l5 = "..X";


Data[8].l1 = "XXX";
Data[8].l2 = "X.X";
Data[8].l3 = "XXX";
Data[8].l4 = "X.X";
Data[8].l5 = "XXX";

Data[9].l1 = "XXX";
Data[9].l2 = "X.X";
Data[9].l3 = "XXX";
Data[9].l4 = "..X";
Data[9].l5 = "XXX";

cin >> n;
cin >> number;

struct num ans;
for(int i = 0;i < n ;++ i)
{
int numm = number[i] - '0';
ans.l1 = ans.l1 + Data[numm].l1 + ". "[i==n-1];
ans.l2 = ans.l2 + Data[numm].l2 + ". "[i==n-1];
ans.l3 = ans.l3 + Data[numm].l3 + ". "[i==n-1];
ans.l4 = ans.l4 + Data[numm].l4 + ". "[i==n-1];
ans.l5 = ans.l5 + Data[numm].l5 + ". "[i==n-1];
}
cout << ans.l1 << endl << ans.l2 << endl << ans.l3<<endl << ans.l4 <<endl << ans.l5;

return 0;
}
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
#include<bits/stdc++.h>
using namespace std;
const int N = 15;
int vis[N];
int ans = 0;
int n;
int PD(int deep)
{
for(int i = 1;i < deep;++ i)
{
if(abs(deep-i) == abs(vis[deep] - vis[i]))
{
return 0;
}else if(vis[deep] == vis[i])
{
return 0;
}
}
return 1;
}
bool check(int a)
{
if(a > n)
{
ans++;
}else{
return 0;
}
return 1;
}
void dfs(int deep)
{
if(check(deep))
{
return ;
}

for(int i = 1;i <= n;++ i)
{
vis[deep] = i;
if(PD(deep)){
dfs(deep+1);
}else {
continue;
}
}
}

int main()
{

cin >> n;

dfs(1);
cout << ans << endl;

return 0;
}

课堂练习
https://43.242.201.154/2024/09/12/Scupractice/
Author
Dong
Posted on
September 12, 2024
Licensed under