A.元素查找(函数模板)

题目描述

编写一个在数组中进行查找的函数模板,其中数组为具有_n_个元素,类型为T,要查找的元素为key。

注意:必须使用模板函数

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
#include<iostream>
using namespace std;

template<class Type>
void search(Type *a,Type key,int n)
{
for (int i = 0; i < n; i++)
{
if (a[i] == key)
{
cout << i + 1 << endl;
return ;
}
}
cout << '0' << endl;
}

int main()
{
int t;
char ch;
cin >> t;
while (t--)
{
int n;
cin >> ch >> n;
if (ch == 'I')
{
int* a = new int[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int key;
cin >> key;
search(a, key, n);
}
else if (ch == 'D')
{
double* a = new double[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
double key;
cin >> key;
search(a, key, n);
}
else if (ch == 'C')
{
char* a = new char[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
char key;
cin >> key;
search(a, key, n);
}
else if (ch == 'S')
{
string* a = new string[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
string key;
cin >> key;
search(a, key, n);
}

}
return 0;
}

B.谁的票数最高(函数模板)

题目描述

某小镇要票选镇长,得票最高者当选。但由于投票机制不健全,导致每届投票时,候选人在投票系统的识别码类型不一致。请编写函数模板,能针对多种类型的数据,查找出得票最高的元素。其中,每届投票的选票有n张,识别码类型为T

注意:必须使用模板函数

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
#include<iostream>
#include<cstring>
using namespace std;

template <class Type>
void Max(Type *a, int n)
{
int i, j;
Type tmp;
for (i = 0; i < n-1; i++)
{
for (j = i + 1; j < n; j++)
{
if(a[i]>a[j])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
Type max_person = a[0];
Type person = a[0];
int max = 0;
int num = 1;
for (i = 0; i < n; i++)
{
if (person == a[i + 1])
{
num++;
}
else
{
if (num > max)
{
max = num;
max_person = person;
}
num = 1;
person = a[i + 1];
}
}
cout << max_person << " " << max << endl;
}

int main()
{
int t;
cin >> t;
while (t--)
{
char ch;
int n;
cin >> ch >> n;
if (ch == 'I')
{
int* a = new int[n+1];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
Max(a, n);
}
else if (ch == 'C')
{
char* a = new char[n+1];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
Max(a, n);
}
else if (ch == 'S')
{
string* a = new string[n+1];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
Max(a, n);
}
}
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
#include<iostream>
#include<cstring>
using namespace std;

template <class Type>
void check(Type* a)
{
int i;
for (i = 1; i < 6; i++)
{
if (a[i] - a[i - 1] < 0)
{
cout << "Invalid" << endl;
return;
}
}
cout << "Valid" << endl;
}
int main()
{
char ch;
while (cin >> ch)
{
if (ch == 'i')
{
int* a = new int[6];
for (int i = 0; i < 6; i++)
{
cin >> a[i];
}
check(a);
}
else if (ch == 'c')
{
char* a = new char[6];
for (int i = 0; i < 6; i++)
{
cin >> a[i];
}
check(a);
}
else if (ch == 'f')
{
double* a = new double[6];
for (int i = 0; i < 6; i++)
{
cin >> a[i];
}
check(a);
}
}
return 0;
}

D.矩阵类模板(类模板)

题目描述

设计一个矩阵类模板Matrix,支持任意数据类型的数据。

要求至少包含2个成员函数:矩阵转置函数transport、以及打印输出函数print

编写main函数进行测试,调用类的成员函数完成转置和输出。

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
#include<iostream>
using namespace std;

template<class Type>
class Matrix
{
protected:
int m;
int n;
Type** mat;
public:
Matrix(int _m, int _n, Type** set);
void transport();
void print();
~Matrix();
};

template<class Type>
Matrix<Type>::Matrix(int _m, int _n, Type** set) :m(_m), n(_n)
{
mat = new Type * [m];
for (int i = 0; i < m; i++)
{
mat[i] = new Type[n];
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
mat[i][j]=set[i][j];
}
}
}

template<class Type>
void Matrix<Type>::transport()
{
Type** middle;
int i, j;
middle = new Type * [n];
for (i = 0; i < n; i++)
{
middle[i] = new Type[m];
}
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
middle[i][j] = mat[j][i];
}
}

for (i = 0; i < m; i++)
{
delete[]mat[i];
}
delete[]mat;

mat = new Type *[n];
for (i = 0; i < n; i++)
{
mat[i] = new Type[m];
}
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
mat[i][j] = middle[i][j];
}
}

int tmp;
tmp = m;
m = n;
n = tmp;
}

template<class Type>
void Matrix<Type>::print()
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n - 1; j++)
{
cout << mat[i][j] << " ";
}
cout << mat[i][j] << endl;
}
}

template<class Type>
Matrix<Type>::~Matrix()
{
for (int i = 0; i < m; i++)
{
delete[]mat[i];
}
delete[]mat;
}

int main()
{
int t;
int i, j;
char ch;
int m, n;
cin >> t;
while (t--)
{
cin >> ch >> m >> n;
if (ch == 'I')
{
int** mat = new int* [m];
for (i = 0; i < m; i++)
{
mat[i] = new int[n];
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cin >> mat[i][j];
}
}
Matrix<int> Mat(m, n, mat);
Mat.transport();
Mat.print();


}
else if (ch == 'D')
{
double** mat = new double* [m];
for (i = 0; i < m; i++)
{
mat[i] = new double[n];
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cin >> mat[i][j];
}
}
Matrix<double> Mat(m, n, mat);
Mat.transport();
Mat.print();

}
else if (ch == 'C')
{
char** mat = new char* [m];
for (i = 0; i < m; i++)
{
mat[i] = new char[n];
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cin >> mat[i][j];
}
}
Matrix<char> Mat(m, n, mat);
Mat.transport();
Mat.print();


}
}
return 0;
}

E.简单类模板(类模板)

题目描述

定义一个列表类,该列表包含属性:数值列表(用长度为100的数组表示),数据长度(实际的数据个数);包含的方法:初始化、插入、删除、打印,方法定义为:

1)初始化,接受外来参数,把数据保存在数值列表中,未使用的列表部分全部初始化为-1
2)插入,接受外来参数的插入位置和插入数值,插入位置从0开始计算,注意从插入位置开始,原有数据都要往后移动一位,且数据长度+1
3)删除,接受外来参数的删除位置,删除位置从0开始计算,注意从删除位置后一位开始,原有数据都要往前移动一位,且数据长度-1
4)打印,把包含的数据按位置顺序输出一行,数据之间单个空格隔开

使用类模板的方法,使得这个类支持整数int类型和浮点数double类型

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
#include<iostream>
using namespace std;
template<class Type>
class List
{
protected:
Type list[100];
int num;
public:
List(int n);
void insert(int a,Type cl);
void back(int a);
void print(int n);
~List() {}
};

template<class Type>
List <Type>::List(int n): num(n)
{
for (int i = 0; i < num; i++)
{
cin >> list[i];
}
for (int i = num; i < 100; i++)
{
list[i] = -1;
}
}

template<class Type>
void List<Type>::insert(int a,Type n)
{
num++;
for (int i = num; i > a; i--)
{
list[i] = list[i - 1];
}
list[a] = n;
}

template<class Type>
void List<Type>::back(int a)
{
for (int i = a; i < num; i++)
{
list[i] = list[i + 1];
}
num++;
}

template<class Type>
void List<Type>::print(int n)
{
num = n;
int i;
for (i = 0; i < num-1; i++)
{
cout << list[i] << " ";
}
cout << list[i] << endl;
}


int main()
{
int n;
int tmp = 0;
while (cin >> n)
{
int start, end;
if (tmp == 0)
{
int sta;
List <int>L1(n);
cin >> start >> sta;
L1.insert(start, sta);
cin >> end;
L1.back(end);
L1.print(n);
tmp = 1;
}
else if (tmp == 1)
{
double sta;
List<double>L2(n);
cin >> start >> sta;
L2.insert(start, sta);
cin >> end;
L2.back(end);
L2.print(n);
tmp = 0;
}
}
return 0;
}