[Coursera 计算导论与C语言基础] 期末编程测试

编程题#1:判断闰年

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述

判断某年是否是闰年。

输入

输入只有一行,包含一个整数a(0 < a < 3000)

输出

一行,如果公元a年是闰年输出Y,否则输出N

提示

公历纪年法中,能被4整除的大多是闰年,但能被100整除而不能被400整除的年份不是闰年, 能被3200整除的也不是闰年,如1900年是平年,2000年是闰年,3200年不是闰年。

#include<iostream>
using namespace std;

int main() {
	int year = 0;
	cin >> year;
	if (year % 4 == 0) {
		if (year % 100 == 0) {
			if (year % 400 == 0&&year%3200!=0) {
				cout << "Y" << endl;
				

			}
			else {
				cout << "N" << endl;
			}
		}
	
		else { 
			cout << "Y" << endl; 
		}
		
	
	}
	else { cout << "N" << endl; }


	return 0;
}

编程题#2:能被3,5,7整除的数

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述

输入一个整数,判断它能否被3,5,7整除,并输出以下信息:

1、能同时被3,5,7整除(直接输出3 5 7,每个数中间一个空格);

2、能被其中两个数整除(输出两个数,小的在前,大的在后。例如:3 5或者 3 7或者5 7,中间用空格分隔)

3、能被其中一个数整除(输出这个除数)

4、不能被任何数整除;(输出小写字符'n',不包括单引号)

输入

一个数字

输出

一行数字,从小到大排列,包含3,5,7中为该输入的除数的数字,数字中间用空格隔开

参考答案

#include<iostream>
using namespace std;

#include <iostream>  
using namespace std;

int main()
{
	int num = 0;
	bool flag = false;
	while (cin >> num)
	{
		if (num % 3 == 0)
		{
			cout << "3 ";
			flag = true;
		}
		if (num % 5 == 0)
		{
			cout << "5 ";
			flag = true;
		}
		if (num % 7 == 0)
		{
			cout << "7";
			flag = true;
		}
		if (flag == false)
		{
			cout << "n";
		}
		cout << endl;
		flag = false;
	}
	return 0;
}


编程题#3:最远距离

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述

给定一组点(x,y),求距离最远的两个点之间的距离。

输入

第一行是点数n(n大于等于2)

接着每一行代表一个点,由两个浮点数x y组成。

输出

输出一行是最远两点之间的距离。

使用cout << fixed << setprecision(4) << dis << endl;输出距离值并精确到小数点后4位。

fixed和setprecision是在<iomanip>头文件里定义的格式控制操作符,需要#include <iomanip>.

提示

注意在内部计算时最好使用double类型,float精准度不能满足本题测试数据要求。

#include<iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main() {
	int n,l=0;
	double x[100];
	double y[100];
	double result[100];
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> x[i] >> y[i];
	}
	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < n-1; j++) {
			result[l] = sqrt(pow((x[i] - x[j + 1]), 2) + pow((y[i] - y[j + 1]), 2));
				l++;
		}
	}
	double max = 0;
	for (int i = 0; i < l; i++) {
		if (result[i] > max) {
			max = result[i];
		}
	}
	cout << fixed << setprecision(4) << max << endl;


	return 0;
}

编程题#4:简单计算器

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述

一个最简单的计算器,支持+, -, *, / 四种运算。仅需考虑输入输出为整数的情况,数据和运算结果不会超过int表示的范围。

输入

输入只有一行,共有三个参数,其中第1、2个参数为整数,第3个参数为操作符(+,-,*,/)。

输出

输出只有一行,一个整数,为运算结果。然而:

1. 如果出现除数为0的情况,则输出:Divided by zero!

2. 如果出现无效的操作符(即不为 +, -, *, / 之一),则输出:Invalid operator!

提示

可以考虑使用if和switch结构。

#include<iostream>

using namespace std;
int main() {
	int n, m,result;
	char op;
	cin >> n >> m>>op;
	if (op == '+' || op == '-' || op == '*' || op == '/') {
		if (op == '+') {
			result = n + m;
			cout << result << endl;
		}
		if (op == '-') {
			result = n - m;
			cout << result << endl;
		}
		if (op == '*') {
			result = n * m;
			cout << result << endl;
		}
		if (op == '/'&&m != 0) {
			result = n / m;
			cout << result << endl;
		}
		else if (op == '/'&&m == 0) {
			cout << "Divided by zero!" << endl;
		}



	}
	else {
		cout << "Invalid operator!" << endl;
	}
	

	return 0;
}

编程题#5:字符串插入

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB、

描述

有两个字符串str和substr,str的字符个数不超过10,substr的字符个数为3。(字符个数不包括字符串结尾处的'\0'。)将substr插入到str中ASCII码最大的那个字符后面,若有多个最大则只考虑第一个。

输入

输入包括若干行每一行为一组测试数据,格式为

str substr

输出

对于每一组测试数据,输出插入之后的字符串。

提示

这题有多组输入,请参照第二题的提示依次读入和处理每一组数据。

如果使用了字符串函数,比如strlen,请包含cstring头文件 #include <cstring>。


#include <iostream>  
using namespace std;
#include <cstring>

int main()
{
	char str[11] = { 0 }, substr[4] = { 0 };

	while (cin >> str >> substr) {
		int l = 0;
		int max=0,ith,out;
		int a = strlen(str);
		char insert[10] = { 0 };
		for (int i = 0; i < a; i++) {
			if (str[i] > max) {
				max = str[i];
				ith = i;
			}
		}
		for (int i = ith+1; i < a; i++) {
			
			insert[l] = str[i];
			l++;
		
		}

		int b = strlen(substr);
		int c = strlen(insert);
		for (int j = 0; j < b; j++) {
			str[ith + j + 1] = substr[j];
			out = ith + j + 1;
		}
		for (int i = 0; i < c; i++) {
			str[out + 1 + i] = insert[i];
		}
		cout << str << endl;



			
	}
	system("pause");
	
	return 0;
}
参考答案
#include <iostream>  
using namespace std;

int main()
{
	char str[14] = { 0 }, substr[4] = { 0 };
	while (cin >> str >> substr)
	{
		int i = 0, posBiggest = 0;
		char strBiggest = 0;
		while (str[i])
		{
			if (str[i] > strBiggest)
			{
				posBiggest = i;
				strBiggest = str[i];
			}
			i++;
		}
		for (int j = 9; j > posBiggest; j--)
		{
			str[j + 3] = str[j];
		}
		str[posBiggest + 1] = substr[0];
		str[posBiggest + 2] = substr[1];
		str[posBiggest + 3] = substr[2];
		cout << str << endl;
	}
	return 0;
}

猜你喜欢

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