C++第五章习题

这篇文章包含多个C++程序示例,分别实现了素数筛选(埃拉托斯特尼筛法),整数排序,已排序数组中插入元素,数组元素逆序,杨辉三角的生成,查找算法(顺序查找和折半查找),以及日期转换为天数。此外,还包括了字符串操作和特定条件的字符串输出。
摘要由CSDN通过智能技术生成

1、筛选法判断素数,逐渐除的方式
判断素数:
不能被2~(int)sqrt(m)整除 (无法判断素数2,单独处理)
不能被2~m-1整除 (无法判断素数2,单独处理)

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int i, j, n, a[101];
	for (i = 1;i <= 100;i++)
		a[i] = i;
	a[1] = 0;
	for (i = 2;i <= 100;i++)
	{
		for (j = i + 1;j <= 100;j++)//j从3开始,确保素数2保留下来
		{
			if (a[i] != 0 && a[j] != 0)
				if (a[j] % a[i] == 0)
					a[j] = 0;
		}
	}
	for (i = 1, n = 0;i <= 100;i++) {
		if (a[i] != 0)
		{
			cout << setw(6) << a[i];n++;
		}
		if (n % 10 == 0)
			cout << endl;
	}
	return 0;

}

2.选择法对十个整数排序
每次遍历找到一个最小的

#include <iostream>
using namespace std;
int main()
{
	int a[11],i,j,temp=0;
	cout << "enter 10 numbers:";
	for (i = 1;i <= 10;i++) {
		cin >> a[i];
	}
	for (i = 1;i <= 9;i++) {
		for (j = i + 1;j <= 10;j++) {
			if (a[i] > a[j]) { temp = a[i];a[i] = a[j];a[j] = temp; }
		}
	}
	for (i = 1;i <= 10;i++) {
		cout << "  " << a[i];
	}
	return 0;
}

3,已经排好序的数组,输入一个数字,按原来的规律插入

#include <iostream>
using namespace std;
int main()
{
	int a[11] = { 1,6,13,17,28,40,56,78,89,100 };
	int i, j,num;
	for (i = 0;i <= 9;i++)
		cout << a[i] << "  ";
	cout << endl << "enter a number:";
	cin >> num;
	if (num > a[9]) a[10] = num;
	else 
		for (i = 0;i <= 8;i++) {
			if (a[i] > num)
			{
				for (j = 9;j >= i;j--)
					a[j + 1] = a[j];
				a[i] = num;
				break;
			}
		}

	for(i=0;i<=10;i++)
		cout << a[i] << " ";
	return 0;
}

5.数组中的值逆序重新存放:
以中间元素为中心,将两侧对称的元素的值互换即可

#include <iostream>
using namespace std;
int main()
{
	const int n = 6;
	int a[n],i,temp;
	cout << "enter " << n << " number:";
	for (i = 0;i < n;i++) {
		cin >> a[i];
	}
	for (i = 0;i < n / 2;i++)
	{
		temp=a[i];a[i] = a[n - 1 - i];a[n - 1 - i] = temp;
	}
	for (i = 0;i < n;i++)
		cout << a[i] << " ";

	return 0;
}

6.杨辉三角形,打印十行
实际为(a+b)的n次方展开后的各项系数
找规律:每行第一个元素为1;对角线元素为1;等于上行该列元素 + 上行前一列元素
在这里插入图片描述

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
	int i, j, a[11] [11];//不用第0行 第0列比较方便
	for (i = 1;i <= 10;i++)
	{
		a[i][1] = 1;//每行第一个元素为1
		a[i][i] = 1;//对角线元素为1
	}
	for (i = 3;i <= 10;i++)
		for (j = 2;j < i;j++)
			//规律:等于上行该列元素 + 上行前一列元素
			a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
	for (i = 1;i <= 10;i++)
	{
		for (j = 1;j <= i;j++)
			cout << setw(5) << a[i][j];
		cout << endl;
	}

	return 0;
}

7.寻找鞍点

#include <iostream>
using namespace std;
int main()
{
	const int n = 4, m = 5;
	int i, j, a[n][m], max, maxj;
	bool flag;
	for (i = 0;i < n;i++)
		for (j = 0;j < m;j++)
			cin >> a[i][j];

	for (i = 0;i < n;i++) {
		max = a[i][0];maxj = 0;
		flag = true;
		for (j = 0;j < m;j++) 
			if (a[i][j] > max)
			{
				max = a[i][j];maxj = j;
			}
		for(int k=0;k<n;k++)
			if (a[k][maxj] < max) {
				flag = false;
				break;
			}
		if (flag)
		{
			cout << a[i][maxj] <<" "<<max<< endl;
			break;
		}

	}
	if (!flag) cout << "not exist";
	return 0;
}

8.折半法查找有序数组元素
比顺序查找效率更高

#include <iostream>
using namespace std;
int main()
{
	const int n = 5;
	int a[n] = { 2,3,4,5,6 }, num, top, bott, mid, ;
	bool flag;
	flag = false;
	top = 0;//查找区间的起始位置
	bott = n - 1;//查找区间的最末位置
	//bool sign;
	cout << "enter a number:"<<endl;
	cin >> num;
	if ((num < a[0]) || (num > a[n-1]))
		cout << "not found"<<endl;
	else
		while ((!flag)&&(top <= bott))
		{
			mid = (bott + top) / 2; //中间元素的下标
			if (num == a[mid])
			{
				cout << num << " : position is " << mid + 1;
				flag = true; //不用flag直接break也可以
			}
			else if (num < a[mid])
				bott = mid - 1;//新范围top ~ mid-1
			else top = mid + 1;//新范围mid+1 ~ bott
		}


	return 0;
}

9,给出年月日,计算是第几天
用数组存放每月的天数,如果闰年,需要判断一下,如果大大于2月,加一天

#include <iostream>
using namespace std;
int main()
{
	int leap(int year);
	int sun_day(int month, int day);
	int year, month, day, days;
	cout << "please input year,month,day:" << endl;
	cin >> year >> month >> day;
	days = sun_day(month, day);
	if (leap(year) && (month > 2))
		days += 1;
	cout << days;

	return 0;
}

int leap(int year) {
	if ((year % 4 == 0) && (year % 100 != 0) || year % 400 == 0)
		return 1;
	return 0;
}
int sun_day(int month, int day) {
	int a[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 },i;
	for (i = 0;i < month - 1;i++) {
		day += a[i];
	}
	return day;
}

在这里插入图片描述
char

#include <iostream>
using namespace std;
int main()
{
	char a[5]{ '*','*','*','*','*' };
	int i, j, k;
	const char space = ' ';
	for (i = 0;i < 5;i++)
	{
		for (j = 0;j <= i;j++)
			cout << space;
		for (k = 0;k < 5;k++) 
			cout << a[k];
		cout << endl;
		
	}
	return 0;
}

string

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str = "*****";
	int i, j, k;
	for (i = 0;i < 5;i++) {
		for (j = 0;j <= i;j++)
			cout << "  ";
		cout << str<<endl;
	}
	return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
	//用&&和||多加括号,而且,cin输入,不能打空格键,是结束的标志
	string str="IIs ssa  sasa";
	int i=0;

	//cout << "输入电文:" << endl;
	//cin >> str;这种方式不能输入空格键
	cout << "显示电文:" << str<<endl;
	while (i < str.size()) {
		if (str[i] >= 'A' && str[i] <= 'Z')
			str[i] = 155 - str[i];
		if (str[i] >= 'a' && str[i] <='z')
			str[i] = 219 - str[i];
		i++;
	}
	cout << "译成原文" << str<<endl;
	return 0;
}

13
(1)用字符数组写strcat函数

#include <iostream>
using namespace std;
int main()
{
	char s1[80], s2[20];
	cout << "input str1:" << endl;
	cin >> s1;
	cout << "input str2: " << endl;
	cin >> s2;
	int i = 0, j = 0;
	while (s1[i] != '\0')
		i++;
	while (s2[j] != '\0')
		s1[i++] = s2[j++];
	s1[i] = '\0';
	cout << s1;
	return 0;
}

(2)调用strcat函数

#include <iostream>
using namespace std;
int main()
{
	char s1[80], s2[20];
	cout << "input str1:" << endl;
	cin >> s1;
	cout << "input str2: " << endl;
	cin >> s2;
	strcat(s1, s2);
	cout << s1;
	return 0;
}

(3)用string方法

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str1, str2;
	cout << "input str1:" << endl;
	cin >> str1;
	cout << "input str2:" << endl;
	cin >> str2;
	str1 = str1 + str2;
	cout << str1;
	return 0;
}

14,字符串按从小到大排序`
和第二题的方法一样,也可以用起泡法

#include <iostream>
#include <string>
using namespace std;
int main()
{
	const int n = 5;
	int i,j;
	string str[n], temp=" ";
	for (i = 0;i < n;i++)
	{
		cout << "输入第 " << i+1 << " 个字符串:" << endl;
		cin >> str[i];
	}
	for (i = 0;i < n - 1;i++) 
		for (j = i + 1;j < n;j++) 
			if (str[i] > str[j]) {
				temp = str[i];
				str[i] = str[j];
				str[j] = temp;
			}
	cout << "排序后: "<<endl;
	for (i = 0;i < n;i++)
		cout << str[i] << endl;
		
	
	return 0;
}

15.以A打头的字符串输出

#include <iostream>
#include <string>
using namespace std;
int main()
{
	const int n = 5;
	int i,j;
	string str[n], temp=" ";
	for (i = 0;i < n;i++)
	{
		cout << "输入第 " << i+1 << " 个字符串:" << endl;
		cin >> str[i];
	}
	cout << "以A打头的字符串"<<endl;
	for (i = 0;i < n;i++) {
		if (str[i][0] == 'A')
			cout << str[i]<<endl;
	}
	
	return 0;
}

16
(1)字符串数组实现逆序输出

#include <iostream>
using namespace std;
int main()
{
	char ch[20];
	cout << "input string:" << endl;
	cin >> ch;
	int i = 0,j;
	while (ch[i] != '\0')
		i++;
	for (j = i - 1;j >= 0;j--)
		cout << ch[j];
	return 0;
}

(2)string方法

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str;
	cout << "input string " << endl;
	cin >> str;
	int i,n;
	n = str.size();
	for (i = n-1;i >=0;i--)
		cout << str[i];


	return 0;
}

17.输入十个学生的姓名、学号、成绩,输出不合格的学生信息

#include <iostream>
#include <string>
using namespace std;
int main()
{
	const int n = 10;
	string name[n];
	int num[n], score[n];
	int i;
	for (i = 0;i < n;i++) {
		cout << "输入第" << i + 1 << "个学生的姓名 学号 成绩" << endl;
		cin >> name[i] >> num[i] >> score[i];
	}
	cout << "不合格:";
	for (i = 0;i < n;i++) {
		if (score[i] < 60)
			cout << name[i] << " " << num[i] << " " << score[i];
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值