[Coursera C++程序设计] 期末考试—编程试题

编程题#1:输出200

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 1000ms 内存限制: 1024kB

描述

使以下代码输出结果为200.

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#include<iostream>

using namespace std;

class Number {

public:

int num;

Number(int n=0): num(n) {}

// 在此处补充你的代码

};

int main() {

Number n1(10), n2(20);

Number n3;n3 = n1*n2;

cout << int(n3) << endl;

return 0;

}

 

输入

不需要输入。

 

输出

输出结果为200。

 

样例输入

 

1

不需要输入。

 

样例输出

 

1

200

#include<iostream>
using namespace std;
class Number {
public:
	int num;
	Number(int n = 0) : num(n) {}
	// 在此处补充你的代码
	int operator * (Number &n)
	{
		return this->num * n.num;
	}
	operator int() { return num; }
	
};

int main() {
	Number n1(10), n2(20);
	Number n3; 
	n3 = n1*n2;
	cout << int(n3) << endl;
	return 0;
}

编程题#2:输出指定结果一

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 1000ms 内存限制: 1024kB

描述

填写代码,使输出结果为

2

2

8

10

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#include <iostream>

using namespace std;

class Number {

public:

int num;

Number(int n): num(n) {

}

// 在此处补充你的代码

};

int main() {

Number a(2);

Number b = a;

cout << a.value() << endl;

cout << b.value() << endl;

a.value() = 8;

cout << a.value() << endl;

a+b;

cout << a.value() << endl;

return 0;

}

 

输入

不需要输入。

 

输出

使输出结果为

2

2

8

10

 

样例输入

 

1

不需要输入。

样例输出

 

1

2

3

4

2

2

8

10

#include <iostream>
using namespace std;
class Number {
public:
	int num;
	Number(int n) : num(n) {
	}
	// 在此处补充你的代码
	int& value()
	{
		return num;
	}
	void operator + (Number& n)
	{
		this->num += n.num;
	}
};

int main() {
	Number a(2);
	Number b = a;
	cout << a.value() << endl;
	cout << b.value() << endl;
	a.value() = 8;
	cout << a.value() << endl;
	a + b;
	cout << a.value() << endl;
	return 0;
}

编程题#3:计算数列平方和

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 1000ms 内存限制: 1024kB

描述

请写出sum函数,使其可以计算输入数列的平方和。

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include <iostream>

using namespace std;

// 在此处补充你的代码

int sqr(int n) {

return n * n;

}

int main() {

int t, n, a[0x100];

cin >> t;

for (int c = 0; c < t; ++c) {

cin >> n;

for (int i = 0; i < n; ++i) cin >> a[i];

cout << sum(a, n, sqr) << endl;

}

return 0;

}

输入

第一行是一个整数 t (t <= 10),表示数据组数;

每组输入数据包含两行,第一行是一个整数 n (n <= 100),

第二行是 n 个用空格分隔开的整数

 

输出

对每组输入数据,输出该组数据中 n 个整数的平方和

 

样例输入

 

1

2

3

4

5

2

2

4 3

3

0 1 2

样例输出

 

1

2

25

5

#include <iostream>
using namespace std;
// 在此处补充你的代码
int sum(int a[],int n,int (*sqr)(int)) {//函数指针int (*f) (int x);  声明一个函数指针
	int sum = 0;
	for (int i = 0; i < n; i++) {
		sum += sqr(a[i]);
	}
	return sum;
}
int sqr(int n) {
	return n * n;
}
int main() {
	int t, n, a[0x100];
	cin >> t;
	for (int c = 0; c < t; ++c) {
		cin >> n;
		for (int i = 0; i < n; ++i) cin >> a[i];
		cout << sum(a, n, sqr) << endl;
	}
	return 0;
}

编程题#4:计算整数平方和

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 1000ms 内存限制: 1024kB

描述

下列程序每次读入一个整数N,若N为0则退出,否则输出N和N的平方。

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#include <iostream>

using namespace std;

// 在此处补充你的代码

int main(int argc, char* argv[]) {

CType obj;

int n;

cin>>n;

while ( n ) {

obj.setvalue(n);

cout<<obj++<<" "<<obj<<endl;

cin>>n;

}

return 0;

}

输入

K个整数。除最后一个数据外,其他数据均不为0。

 

输出

K-1行。第I行输出第I个输入数和它的平方。

 

样例输入

 

1

1 5 8 9 0

 

样例输出

 

1

2

3

4

1 1

5 25

8 64

9 81

#include <iostream>
using namespace std;
// 在此处补充你的代码
class CType {
private:
	int value;
public:
	int m;
	int sqr;
	CType() :value(0) {};
	void setvalue(int n) {
		value = n;
	}
	CType& operator++(int)
	{
		static CType tmp;//必须使用static变量,否则返回时内存就被释放了
		tmp.value = value;
		value *= value;
		return tmp;
	
	}
	friend ostream& operator << (ostream& o, CType& cType)//此处必须为友元函数
	{
		o << cType.value;
		return o;
	}

};
int main(int argc, char* argv[]) {
	CType obj;
	int n;
	cin >> n;
	while (n) {
		obj.setvalue(n);
		cout << obj++ << " " << obj << endl;
		cin >> n;
	}
	return 0;
}

编程题#5:计算数组的低3位之和

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 1000ms 内存限制: 1024kB

描述

输入一个正整数构成的数组a[0], a[1], a[2], ... , a[n-1], 计算它们的二进制低3位之和。

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

// 在此处补充你的代码

int main(int argc, char* argv[]) {

int v, my_sum=0;

vector<int> vec;

cin>>v;

while ( v ) {

vec.push_back(v);

cin>>v;

}

for_each(vec.begin(), vec.end(), CMy_add(my_sum));

cout<<my_sum<<endl;

return 0;

}

 

输入

数组a,以0表示输入结束。

 

输出

一个整数 , 所输入数组各元素的二进制低3位之和。

 

样例输入

 

1

1 3 9 7 3 6 20 15 18 17 4 8 18 0

 

样例输出

 

1

41

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 在此处补充你的代码
class CMy_add {
public:
	CMy_add(int &v ) :theValue(v) {}
	void operator() (int x)
	{
		x %= 8;//得到其低3位
		theValue += x;
	}
private:
	int& theValue;

};
int main(int argc, char* argv[]) {
	int v, my_sum = 0;
	vector<int> vec;
	cin >> v;
	while (v) {
		vec.push_back(v);
		cin >> v;
	}
	for_each(vec.begin(), vec.end(), CMy_add(my_sum))//此处my_sum和theValue公用一个地址
	cout << my_sum << endl;
	return 0;
}

编程题#6:MyString

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 1000ms 内存限制: 1024kB

描述

请写出 MyString类,使得下面程序的输出结果符合下面的要求。

 

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

#include <cstring>

#include <cstdlib>

#include <string>

#include <iostream>

using namespace std;

// 在此处补充你的代码

int CompareString( const void * e1, const void * e2)

{

MyString * s1 = (MyString * ) e1;

MyString * s2 = (MyString * ) e2;

if( * s1 < *s2 )

return -1;

else if( *s1 == *s2)

return 0;

else if( *s1 > *s2 )

return 1;

}

int main()

{

MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);

MyString SArray[4] = {"big","me","about","take"};

cout << "1. " << s1 << s2 << s3<< s4<< endl;

s4 = s3;

s3 = s1 + s3;

cout << "2. " << s1 << endl;

cout << "3. " << s2 << endl;

cout << "4. " << s3 << endl;

cout << "5. " << s4 << endl;

cout << "6. " << s1[2] << endl;

s2 = s1;

s1 = "ijkl-";

s1[2] = 'A' ;

cout << "7. " << s2 << endl;

cout << "8. " << s1 << endl;

s1 += "mnop";

cout << "9. " << s1 << endl;

s4 = "qrst-" + s2;

cout << "10. " << s4 << endl;

s1 = s2 + s4 + " uvw " + "xyz";

cout << "11. " << s1 << endl;

qsort(SArray,4,sizeof(MyString),CompareString);

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

cout << SArray[i] << endl;

//s1的从下标0开始长度为4的子串

cout << s1(0,4) << endl;

//s1的从下标5开始长度为10的子串

cout << s1(5,10) << endl;

return 0;

}

输入

 

输出

1. abcd-efgh-abcd-

2. abcd-

3.

4. abcd-efgh-

5. efgh-

6. c

7. abcd-

8. ijAl-

9. ijAl-mnop

10. qrst-abcd-

11. abcd-qrst-abcd- uvw xyz

about

big

me

take

abcd

qrst-abcd-

 

样例输入

 

1

 

样例输出

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

1. abcd-efgh-abcd-

2. abcd-

3.

4. abcd-efgh-

5. efgh-

6. c

7. abcd-

8. ijAl-

9. ijAl-mnop

10. qrst-abcd-

11. abcd-qrst-abcd- uvw xyz

about

big

me

take

abcd

qrst-abcd-

#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;
// 在此处补充你的代码
class MyString :public string {


public:
	MyString() : string() {};
	//1.0继承类继承父类所有的成员变量和成员函数,但不继承构造函数和析构函数 
	//1.1继承类的无参构造函数,会隐式调用父类的无参构造函数
	MyString(const char *s) :string(s) {};//类型转换构造函数
										  //1.2继承类的有参构造函数,如果父类也有有参构造函数,则必须显示调用它 
										  //2.0这里的参数根据reference有两种选择,此处必须用const char*,"xxx"的类型是const char* 
	MyString(const string &s) :string(s) {};//char*是数据类型,而string是类,要弄清楚。
											//1.3继承类的复制构造函数必须要显示的调用父类的复制构造函数,不然就会默认调用父类的无参构造函数 
	MyString(MyString& myStr) :string(myStr) {};//复制构造函数
												//我们发现在派生类的拷贝构造函数中的初始化列表中,
												//基类拷贝构造函数的参数是派生类,但是这样子是没有关系的,编译系统会自动将派生类缩减成基类规模(这是我的个人理解,进行缩减的只是派生类的临时对象,不会对参数进行修改),
												//然后传入给基类的拷贝构造函数,然后在派生类的拷贝构造函数当中再将派生类比基类多出的成员变量进行拷贝。 
	MyString operator()(int i, int j) {
		return this->substr(i, j);
	}




};
int CompareString(const void * e1, const void * e2)
{
	MyString * s1 = (MyString *)e1;
	MyString * s2 = (MyString *)e2;
	if (*s1 < *s2)
		return -1;
	else if (*s1 == *s2)
		return 0;
	else if (*s1 > *s2)
		return 1;
}
int main()
{
	MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);
	MyString SArray[4] = { "big","me","about","take" };
	cout << "1. " << s1 << s2 << s3 << s4 << endl;
	s4 = s3;
	s3 = s1 + s3;
	cout << "2. " << s1 << endl;
	cout << "3. " << s2 << endl;
	cout << "4. " << s3 << endl;
	cout << "5. " << s4 << endl;
	cout << "6. " << s1[2] << endl;
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A';
	cout << "7. " << s2 << endl;
	cout << "8. " << s1 << endl;
	s1 += "mnop";
	cout << "9. " << s1 << endl;
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << endl;
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << endl;
	qsort(SArray, 4, sizeof(MyString), CompareString);
	for (int i = 0; i < 4; i++)
		cout << SArray[i] << endl;
	//s1的从下标0开始长度为4的子串
	cout << s1(0, 4) << endl;
	//s1的从下标5开始长度为10的子串
	cout << s1(5, 10) << endl;
	return 0;
}

编程题#7:字符串排序

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 1000ms 内存限制: 1024kB

描述

请按照要求对输入的字符串进行排序。

 

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 <string>

#include <list>

using namespace std;

class A{

private:

string name;

public:

A(string n) :name(n){}

friend bool operator < (const class A& a1, const class A &a2);

friend bool operator == (const class A &a1, const class A &a2){

if (a1.name.size() == a2.name.size())

return true;

else

return false;

}

friend ostream & operator << (ostream &o, const A &a){

o << a.name;

return o;

}

string get_name() const{

return name;

}

int get_size() const{

return name.size();

}

};

// 在此处补充你的代码

int main(int argc, char* argv[])

{

list<A> lst;

int ncase, n, i = 1;

string s;

cin >> ncase;

while (ncase--){

cout << "Case: "<<i++ << endl;

cin >> n;

for (int i = 0; i < n; i++){

cin >> s;

lst.push_back(A(s));

}

lst.sort();

Show(lst.begin(), lst.end(), Print());

cout << endl;

lst.sort(MyLarge<A>());

Show(lst.begin(), lst.end(), Print());

cout << endl;

lst.clear();

}

return 0;

}

输入

第一行是正整数T,表示测试数据的组数

每组测试数据输入共两行,

第一行是正整数N,表示字符串个数

第二行是N个字符串, 字符串间用空格分离

 

输出

对于每组测试数据,先输出一行:

Case: n

如对第一组数据就输出Case: 1

第二行按照字符串长度从小到大排序之后输出N个字符串,字符串之间以空格间隔(不会出现字符串长度相同的情况)

第三行按照字符串首字符ASCII码序从小到大排序之后输出N个字符串,字符串之间以空格间隔(不会出现字符串首字母相同的情况)

 

样例输入

 

1

2

3

4

5

2

4

a bnss ds tsdfasg

5

aaa bbbb ccccd sa q

 

样例输出

 

1

2

3

4

5

6

Case: 1

a ds bnss tsdfasg

a bnss ds tsdfasg

Case: 2

q sa aaa bbbb ccccd

aaa bbbb ccccd q sa

#include <iostream>
#include <string>
#include <list>
using namespace std;

class A {
private:
	string name;
public:
	A(string n) :name(n) {}
	friend bool operator < (const class A& a1, const class A &a2);
	friend bool operator == (const class A &a1, const class A &a2) {
		if (a1.name.size() == a2.name.size())
			return true;
		else
			return false;
	}
	friend ostream & operator << (ostream &o, const A &a) {
		o << a.name;
		return o;
	}
	string get_name() const {
		return name;
	}
	int get_size() const {
		return name.size();
	}
};
// 在此处补充你的代码
bool operator < (const A& a1, const A& a2)
{
	return a1.get_size() < a2.get_size();
}

template <class Iterator, class Function>//函数模板
void Show(Iterator begin, Iterator end, Function print)
{
	for (Iterator iterator1 = begin; iterator1 != end; iterator1++)
	{
		print(*iterator1);
	}
}

class Print //函数对象类
{
public:
	void operator() (const A& a)
	{
		cout << a.get_name() << " ";
	}
};

template <class A>  //函数对象类模板
struct MyLarge
{
	inline bool operator() (const A& a1, const A& a2)
	{
		return a1.get_name() < a2.get_name();
	}
};

int main(int argc, char* argv[])
{
	list<A> lst;
	int ncase, n, i = 1;
	string s;
	cin >> ncase;
	while (ncase--) {
		cout << "Case: " << i++ << endl;
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> s;
			lst.push_back(A(s));
		}
		lst.sort();//如上已经重载的A类型的“<”号,按size排序
		Show(lst.begin(), lst.end(), Print());//Print()为函数对象,用途类似于函数指针或函数名字

		cout << endl;
		lst.sort(MyLarge<A>());//按照函数对象的方式进行排序
		Show(lst.begin(), lst.end(), Print());
		cout << endl;
		lst.clear();
	}
	return 0;
}

编程题#8:计算整数k

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 1000ms 内存限制: 1024kB

描述

输入整数 n ( 0 <=n<= 2^30) , 以及整数i,j(0 <= i,j <31,i < j-1), 输出整数k(按十六进制输出结果 ),k的第i位和n相同,第j位和n不同,i,j之间的位是1, 其他位都是0。这里提到的所有的位,指的都是二进制位,最右边算第0位。

 

输入

第一行是整数 t,表示数据组数。

每组输入数据是一行,三个整数 n,i和j。

 

输出

对每组输入数据,按十六进制输出结果。

 

样例输入

 

1

2

3

2

23 3 5

7 0 2

样例输出

 

1

2

30

3

 

提示

本题请写出完整的程序。

#include <iostream>

using namespace std;

// 在此处补充你的代码
int main(int argc, char* argv[])
{
	int t;
	cin >> t;
	while (t--) {
		int n, i, j;
		cin >> n >> i >> j;
		int  result = 0;
		if (n &(1 << i)) {//n的第i位为1,如果是0不需要改变
			result |= 1 << i;
		}
		if (!(n & (1 << j)))//n的第j位为0,如果是1不需要改变
		{
			result |= 1 << j;
		}
		for (int k = i + 1; k < j; k++)
		{
			result |= 1 << k;
		}
		cout << hex << result << endl;
	}
	
	
	return 0;
}

编程题#9:人群的排序和分类

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 1000ms 内存限制: 1024kB

 

描述

对人群按照输入的信息进行排序和分类。

 

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

#include <iostream>

#include <set>

#include <iterator>

#include <algorithm>

using namespace std;

// 在此处补充你的代码

int main()

{

int t;

cin >> t;

set<A*,Comp> ct;

while( t -- ) {

int n;

cin >> n;

ct.clear();

for( int i = 0;i < n; ++i) {

char c; int k;

cin >> c >> k;

if( c == 'A')

ct.insert(new A(k));

else

ct.insert(new B(k));

}

for_each(ct.begin(),ct.end(),Print);

cout << "****" << endl;

}

}

输入

第一行是整数t,表明一共t组数据. t < 20

对每组数据:

第一行是整数n,表示下面一共有n行。 0 < n < 100

下面的每行代表一个人。每行以一个字母开头,代表该人所属的类别,然后跟着一个整数,代表年龄。字母只会是 'A‘或‘B' 。整数范围0到100。数据保证年龄都不相同。

 

输出

对每组输入数据,将这些人按年龄从小到大输出。每个人先输出类别,再输出年龄。每组数据的末尾加一行 "****"

 

样例输入

 

1

2

3

4

5

6

7

8

9

10

2

4

A 3

B 4

A 5

B 6

3

A 4

A 3

A 2

样例输出

 

1

2

3

4

5

6

7

8

9

A 3

B 4

A 5

B 6

****

A 2

A 3

A 4

****

#include <iostream>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;
// 在此处补充你的代码
class A {
public:
	int age;
	char s;
	A(int a) : age(a), s('A') {}
	A(int a, char str) : age(a), s(str) {}

};
class B : public A
{
public:
	B(int b) :A(b, 'B') {}
};
struct Comp
{
	bool operator () (const A* x, const A* y) const
	{
		return x->age < y->age;
	}
};
void Print(const A* a)
{
	cout << a->s << " " << a->age << endl;
}

int main()
{

	int t;
	cin >> t;
	set<A*, Comp> ct;//A*为指向A的指针
	while (t--) {
		int n;
		cin >> n;
		ct.clear();
		for (int i = 0; i < n; ++i) {
			char c; int k;
			cin >> c >> k;

			if (c == 'A')
				ct.insert(new A(k));//new A(k)为动态内存分配,返回该内存空间的起始地址
			else
				ct.insert(new B(k));
		}
		for_each(ct.begin(), ct.end(), Print);
		cout << "****" << endl;
	}
}

编程题#10:输出指定结果二

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 1000ms 内存限制: 1024kB

描述

通过填空使得程序输出的结果符合下面的要求。

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#include <iostream>

#include <map>

using namespace std;

// 在此处补充你的代码

int A::count = 0;

void func(B b) { }

int main()

{

A a1(5),a2;

cout << A::count << endl;

B b1(4);

cout << A::count << endl;

func(b1);

cout << A::count << endl;

A * pa = new B(4);

cout << A::count << endl;

delete pa;

cout << A::count << endl;

return 0;

}

输入

不需要输入。

 

输出

使得程序的输出结果是:

2

3

B::destructor

A::destructor

3

4

B::destructor

A::destructor

3

B::destructor

A::destructor

A::destructor

A::destructor

 

样例输入

 

1

不需要输入。

 

样例输出

 

1

2

3

4

5

6

7

8

9

10

11

12

13

2

3

B::destructor

A::destructor

3

4

B::destructor

A::destructor

3

B::destructor

A::destructor

A::destructor

A::destructor

提示

int A::count = 0; 这个变量是用来记录一共有多少个类A及类A的派生类的对象的。

#include <iostream>
#include <map>
using namespace std;
// 在此处补充你的代码
class A {
public:
	
	static int count;
	A() { count++; }
	A(int a) { count++; }
	virtual ~A()
	{
		cout << "A::destructor" << endl;
	}

	void operator delete(void* a)//重载delete
	{
		count--;
	}
};
class B: public A
{
public:
	B() :A() {}
	B(int b) :A() {}
	B& operator = (B& b)
	{
		return b;
	}
	virtual ~B()
	{
		cout << "B::destructor" << endl;
	}
};
int A::count = 0;
void func(B b) { }
int main()
{
	A a1(5), a2;
	cout << A::count << endl;
	B b1(4);
	cout << A::count << endl;
	func(b1);
	cout << A::count << endl;
	A * pa = new B(4);
	cout << A::count << endl;
	delete pa;
	cout << A::count << endl;
	return 0;
}

编程题#11:数据库内的学生信息

来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

总时间限制: 3000ms 内存限制: 20480kB

描述

程序填空,使得下面的程序,先输出

(Tom,80),(Tom,70),(Jone,90),(Jack,70),(Alice,100),

(Tom,78),(Tom,78),(Jone,90),(Jack,70),(Alice,100),

(70,Jack),(70,Tom),(80,Tom),(90,Jone),(100,Alice),

(70,Error),(70,Error),(80,Tom),(90,Jone),(100,Alice),

******

然后,再根据输入数据按要求产生输出数据

 

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

#include <iostream>

#include <string>

#include <map>

#include <iterator>

#include <algorithm>

using namespace std;

// 在此处补充你的代码

struct Student

{

string name;

int score;

};

template <class T>

void Print(T first,T last) {

for(;first!= last; ++ first)

cout << * first << ",";

cout << endl;

}

int main()

{

Student s[] = { {"Tom",80},{"Jack",70},

{"Jone",90},{"Tom",70},{"Alice"

                                   ,100} };

MyMultimap<string,int> mp;

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

mp.insert(make_pair(s[i].name,s[i].score));

Print(mp.begin(),mp.end()); //按姓名从大到小输出

mp.Set("Tom",78); //把所有名为"Tom"的学生的成绩都设置为78

Print(mp.begin(),mp.end());

MyMultimap<int,string,less<int> > mp2;

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

mp2.insert(make_pair(s[i].score,s[i].name));

Print(mp2.begin(),mp2.end()); //按成绩从小到大输出

mp2.Set(70,"Error");

            //把所有成绩为70的学生,名字都改为"Error"

Print(mp2.begin(),mp2.end());

cout << "******" << endl;

mp.clear();

string name;

string cmd;

int score;

while(cin >> cmd ) {

if( cmd == "A") {

cin >> name >> score;

if(mp.find(name) != mp.end() ) {

cout << "erroe" << endl;

}

mp.insert(make_pair(name,score));

}

else if(cmd == "Q") {

cin >> name;

MyMultimap<string,int>::iterator p = mp.find

                            (name);

if( p!= mp.end()) {

cout << p->second << endl;

}

else {

cout << "Not Found" << endl;

}

}

}

return 0;

}

输入

输入数据的每一行,格式为以下之一:

A name score

Q name score

name是个不带个空格的字符串,长度小于 20

score是个整数,能用int表示

A name score 表示往数据库中新增一个姓名为name的学生,其分数为score。开始时数据库中一个学生也没有。

Q name 表示在数据库中查询姓名为name的学生的分数

数据保证学生不重名。

输入数据少于200,000行。

 

输出

对于每个查询,输出学生的分数。如果查不到,则输出 "Not Found"

 

样例输入

 

1

2

3

4

5

6

A Tom1 30

A Tom2 40

Q Tom3

A Tom4 89

Q Tom1

Q Tom2

样例输出

 

1

2

3

4

5

6

7

8

(Tom,80),(Tom,70),(Jone,90),(Jack,70),(Alice,100),

(Tom,78),(Tom,78),(Jone,90),(Jack,70),(Alice,100),

(70,Jack),(70,Tom),(80,Tom),(90,Jone),(100,Alice),

(70,Error),(70,Error),(80,Tom),(90,Jone),(100,Alice),

******

Not Found

30

40

提示

1) 编写模板的时候,连续的两个 “>”最好要用空格分开,以免被编译器看作是 ">>"运算符。VS可能无此问题,但是Dev C++和服务器上的编译环境会有这个问题。

比如 vector<vector<int>> 有可能出错,要改成 vector<vector<int> >

2) 在模板中写迭代器时,最好在前面加上 typename关键字,否则可能会编译错。VS可能无此问题,但是Dev C++和服务器上的编译环境会有这个问题。

#include <iostream>
#include <string>
#include <map>
#include <iterator>
#include <algorithm>
using namespace std;
// 在此处补充你的代码
template <class T>
struct bigger :public  binary_function<T, T, bool>
{
	bool operator()(const T &t1, const T &t2) const { return t2 < t1; }
};
template<class T1, class T2, class Comp = bigger<T1> >
class MyMultimap {
public:
	typedef multimap<T1, T2, Comp> MAP;
	typedef typename multimap<T1, T2, Comp>::iterator iterator;
	MAP mymap;
	iterator begin() { return mymap.begin(); }
	iterator end() { return mymap.end(); }
	void Set(T1 t1, T2 t2)
	{
		iterator i = mymap.begin();
		for (; i != mymap.end(); i++)
			if (i->first == t1)
				i->second = t2;
	}
	void insert(pair<T1, T2> p) { mymap.insert(p); }
	void clear() { mymap.clear(); }
	iterator find(T1 t) { return mymap.find(t); }
};
template<class T1, class T2>
ostream & operator<<(ostream &o, pair<T1, T2>p)
{
	o << "(" << p.first << "," << p.second << ")";
	return o;
}

struct Student
{
	string name;
	int score;
};
template <class T>
void Print(T first, T last) {
	for (; first != last; ++first)
		cout << *first << ",";
	cout << endl;
}
int main()
{

	Student s[] = { { "Tom",80 },{ "Jack",70 },
	{ "Jone",90 },{ "Tom",70 },{ "Alice",100 } };
	MyMultimap<string, int> mp;
	for (int i = 0; i<5; ++i)
		mp.insert(make_pair(s[i].name, s[i].score));
	Print(mp.begin(), mp.end()); //按姓名从大到小输出

	mp.Set("Tom", 78); //把所有名为"Tom"的学生的成绩都设置为78
	Print(mp.begin(), mp.end());


	MyMultimap<int, string, less<int> > mp2;
	for (int i = 0; i<5; ++i)
		mp2.insert(make_pair(s[i].score, s[i].name));

	Print(mp2.begin(), mp2.end()); //按成绩从小到大输出
	mp2.Set(70, "Error");          //把所有成绩为70的学生,名字都改为"Error"
	Print(mp2.begin(), mp2.end());
	cout << "******" << endl;

	mp.clear();

	string name;
	string cmd;
	int score;
	while (cin >> cmd) {
		if (cmd == "A") {
			cin >> name >> score;
			if (mp.find(name) != mp.end()) {
				cout << "erroe" << endl;
			}
			mp.insert(make_pair(name, score));
		}
		else if (cmd == "Q") {
			cin >> name;
			MyMultimap<string, int>::iterator p = mp.find(name);
			if (p != mp.end()) {
				cout << p->second << endl;
			}
			else {
				cout << "Not Found" << endl;
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_30945147/article/details/81253136