C++第五周mooc在线测评— 问题的模块化求解

//编写递归函数求数组最大值(20分)
//题目内容:
//编写函数求一个数组中数组元素的最大值,要求必须用递归方法解决。
//
//输入格式 :
//数组中的数字均为整型数,输入的第一个数为数组长度,后续为数组的所有元素。
//
//输出格式:
//该数组的最大值
//
//输入样例:
//5
//92 18 77 6 15
//
//输出样例:
//92
#include<iostream>
using namespace std;
int maxmum(int str[], int i, int max);
int main()
{
	int count;
	cin >> count;
	int data[20];
	for (int i = 0; i < count; i++)
	{
		cin >> data[i];
	}
	int max = maxmum(data,count,data[0]);
	cout << max;
	system("pause");
	return 0;
}

int maxmum(int str[], int i,int max)
{
	if (i == 0)
	{
		return max;
	}
	else
	{	
		if (max<str[i - 1])
		{
			max = str[i - 1];
		}
		i = i - 1;
		return maxmum(str, i, max);
	}
}

//编写内联函数求矩形的面积和周长(20分)
//题目内容:
//编写函数求矩形的面积和周长,由于算式非常简单,请使用内联函数方式编写,提高程序运行效率
//
//输入格式 :
//矩形的长和宽,均为整数
//
//输出格式:
//矩形的面积和周长
//
//输入样例:
//3 5
//
//输出样例:
//15 16
#include<iostream>
using namespace std;

inline int area(int a, int b)
{
	return a*b;
}

inline int cir(int a, int b)
{
	return 2 * (a + b);
}

int main()
{
	int a, b;
	cin >> a >> b;
	int s, l;
	s = area(a, b);
	l = cir(a, b);
	cout << s << " " << l;
	system("pause");
	return 0;
}


//编写重载函数来打印字符串(20分)
//题目内容:
//编写函数 print_spaced 来打印字符串,要求打印出的字符串每个字母之间都有一个空格。要求编写两个同名函数,一个支持字符数组输入,另一个支持string类型输入。然后编写main函数测试这两个函数,第一个使用字符数组输入,第二个使用string类型输入。
//
//输入格式 :
//两个字符串,长度不超过100,只包含英文大小写字母,不含其他字符。
//
//输出格式:
//经间隔空格处理后的两个字符串,两个字符串分居两行。注意字符串的最后一个字母后面没有空格。
//
//输入样例:
//news
//final
//
//输出样例:
//n e w s
//f i n a l
#include<iostream>
#include<string> 
#include<string.h> 

using namespace std;
void print_spaced(char str[]);
void print_spaced(string & str);

int main()
{
	char str[100];
	cin.getline(str,100);
	string str2;
	getline(cin, str2);

	print_spaced(str2);
	print_spaced(str);

	cout << str << endl;
	cout << str2;
	system("pause");
	return 0;
}

void print_spaced(char str[])
{
	int length = strlen(str);
	char temp[100];
	for (int i = 0; i < length; i++)
	{
		temp[i] = str[i];
	}

	int j = 0;
	for (int i = 0; i < length; i++)
	{
		str[j] = temp[i];
		j++;
		if (i != length - 1)
		{
			str[j] = 32;
			j++;
		}
	}
	str[j] = 0;
}

void print_spaced(string & str)
{
	int length = str.length();
	string temp;
	for (int i = 0; i < length; i++)
	{
		temp = temp + str[i];
		if (i != length - 1)
		{
			temp = temp + " ";
		}
	}
	str = temp;
}


//4排序函数重载(20分)
//题目内容:
//编写一组重载的排序函数,可以对两个整数、三个整数、四个整数、整数数组从大到小排序, 函数名为sort, 其中数组排序应使用递归的方法,另补充print函数,在一行显示排序后的数组元素。
//主函数如下:
//int main()
//{
//	int a, b, c, d;
//	int data[100];
//	int k, n, i;
//	cin >> k;
//	switch (k)
//	{
//	case 1:
//		cin >> a >> b;
//		sort(a, b);
//		cout << a << " " << b << endl;
//		break;
//	case 2:
//		cin >> a >> b >> c;
//		sort(a, b, c);
//		cout << a << " " << b << " " << c << endl;
//		break;
//	case 3:
//		cin >> a >> b >> c >> d;
//		sort(a, b, c, d);
//		cout << a << " " << b << " " << c << " " << d << endl;
//		break;
//	case 4:
//		cin >> n;
//		for (i = 0; i<n; i++)
//		{
//			cin >> data[i];
//		}
//		sort(data, n);
//		print(data, n);
//		break;
//	}
//	return 0;
//}
//输入格式:
//请根据主程序自己分析。
//
//输出格式:
//排序后的数据,一行,从大到小,末尾没有空格。
//
//输入样例:
//4
//10
//22 15 20 16 3 27 14 64 108 10
//
//输出样例:
//108 64 27 22 20 16 15 14 10 3
#include<iostream>
using namespace std;

void sort(int &a, int &b);
void sort(int &a, int &b, int &c);
void sort(int &a, int &b, int &c, int &d);
void sort(int data[], int n);

void print(int data[],int n);

int main()
{
	int a, b, c, d;
	int data[100];
	int k, n, i;
	cin >> k;
	switch (k)
	{
	case 1:
		cin >> a >> b;
		sort(a, b);
		cout << a << " " << b << endl;
		break;
	case 2:
		cin >> a >> b >> c;
		sort(a, b, c);
		cout << a << " " << b << " " << c << endl;
		break;
	case 3:
		cin >> a >> b >> c >> d;
		sort(a, b, c, d);
		cout << a << " " << b << " " << c << " " << d << endl;
		break;
	case 4:
		cin >> n;
		for (i = 0; i<n; i++)
		{
			cin >> data[i];
		}
		sort(data, n);
		print(data, n);
		break;
	}
	system("pause");
	return 0;
}

void sort(int &a, int &b)
{
	if (a<b)
	{
		int temp = a;
		a = b;
		b = temp;
	}
}
void sort(int &a, int &b, int &c)
{
	sort(a, b);
	sort(a, c);
	sort(b, c);
}
void sort(int &a, int &b, int &c, int &d)
{
	sort(a, b, c);
	sort(a, d);
	sort(b, c, d);
}

void sort(int data[], int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < n - 1 - i; j++)
		{
			sort(data[j], data[j + 1]);
		}
	}
}
void print(int data[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << data[i];
		if (i != n-1)
		{
			cout << " ";
		}
	}
}

//5编写递归函数来使字符串逆序(20分)
//题目内容:
//编写函数来使一个字符串逆序输出,要求必须用递归函数。
//
//输入格式 :
//一个字符串,不会超过100个字符长,中间可能包含空格
//
//输出格式:
//该字符串的逆序
//
//输入样例:
//Hello, everyone
//
//输出样例:
//enoyreve, olleH
#include<iostream>
#include<string.h>
using namespace std;
void rev(char str[], int n);
void allrev(char str[]);
int main()
{
	char str[100];
	cin.getline(str, 100);
	allrev(str);
	cout << str;
	system("pause");
	return 0;

}
void rev(char &a, char &b)
{
	char temp = a;
	a = b;
	b = temp;
}

void allrev(char str[])
{
	int length = strlen(str);
	for (int i = 0; i < length/2; i++)
	{
		rev(str[i], str[length - 1 - i]);
	}
}



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值