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

这是一份来自Coursera的C++程序设计课程的期末考试编程题汇总,包括11道编程题目,涵盖输出特定结果、数列计算、字符串操作、数组处理等不同主题。每道题目提供了详细的描述、输入输出要求和样例,旨在测试学生对C++编程的掌握程度。
摘要由CSDN通过智能技术生成

编程题#1:输出200

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

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

描述

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

 

输入

不需要输入。

 

输出

输出结果为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;
}

样例输入

 

 

1

不需要输入。

 

样例输出

 

 

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

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

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

描述

填写代码,使输出结果为

2

2

8

10

#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函数,使其可以计算输入数列的平方和。

 
#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的平方。

 
#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上完成的

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值