第二次上机作业

题目一:代码如下:

#include <iostream>
using namespace std;
int main() {
	cout << "请输入3*3的矩阵" << endl;
	int arr1[3][3];
	int arr2[3][3];
	int s = 0;
	int a, b, c;
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 3; j++)
			cin >> arr1[i][j];
	cout << "您输入的矩阵为:"<<endl;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++)
		{
			cout << arr1[i][j] << "\t";
		}
		cout << endl;
	}
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++)
		{

			s += arr1[i][j];
		}
		if (i == 0) a = s;
		if (i == 1) b = s;
		if (i == 2) c = s;
		s = 0;
	}
	if (b > c && b > a) {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++)
			{
				if (i == 0)
				{
					arr2[i][j] = arr1[i][j];
				}
				if (i == 1) {
					arr1[0][j] = arr1[1][j];
					arr1[1][j] = arr2[0][j];
				}
			}
			cout << endl;
		}
	}
	if (c > a && c > b) {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++)
			{
				if (i == 0)
				{
					arr2[i][j] = arr1[i][j];
				}
				if (i == 2) {
					arr1[0][j] = arr1[2][j];
					arr1[2][j] = arr2[0][j];
				}
			}
			cout << endl;
		}
	}

	cout << "互换后的矩阵是:" << endl;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++)
		{
			cout << arr1[i][j] << "\t";
		}
		cout << endl;
	}
	return 0;
}

应该明白的是:
二维数组的遍历方式

for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++)
		{
			cout << arr1[i][j] << "\t";
		}
		cout << endl;

if逻辑语句判断大小

if (b > c && b > a)

演示结果:

在这里插入图片描述

题目二:在这里插入图片描述

#include <iostream>
using namespace std;
int main() {
	
		cout << "请输入3*3的矩阵" << endl;
	int arr1[3][3];
	int i, j;
	for (i = 0; i < 3; i++)
		for (j = 0; j < 3; j++)
			cin >> arr1[i][j];
	cout << "您输入的矩阵为:" << endl;
	for (i = 0; i < 3; i++) {
		for (j = 0; j < 3; j++)
		{
			cout << arr1[i][j] << "\t";
		}
		cout << endl;
	}
	int sum = 0;
	 i = 0;
	 j = 0;
	int m = 3;
	do {
		for (j = 0; j < m; j++)

			sum += *(arr1[i] + j);
				i++,m--;
		
	} while (i <= 3);
	cout <<"上对角线的值是"<< sum<<endl;
	i = 0;
	int n = 1;
	sum = 0;
	do {
		for (j = 0; j < n; j++)
		
			sum += *(arr1[i] + j);
			i++, n++;
		
	} while (i <= 2);
	cout <<"下对角线的值是"<< sum<<endl;
	 sum = 0;
	
		for(i=0,j=0;j<=2;j++)
		{
			i = j; sum += arr1[i][j];
		}
		cout <<"主对角线的值是"<< sum;
	
	return 0;
}

本题在数组的基础上更加考察对指针的熟练运用。
使用for循环加上

演示结果:
在这里插入图片描述

*(arr[i]+j)
题目三:

在这里插入图片描述
举例:
在这里插入图片描述

#include<iostream>
#include<string>
using namespace std;
int main() {
	string zf;
	string zf2;
	int judge = 1;//1表示是回文数,0表示不是回文数
	cout << "Please input a string.." << endl;
	getline(cin, zf);//获取用户输入的字符串,如果用cin>>会造成无法读取用户驶入的空格和tab键
	int num = zf.length();//获取用户所输入的字符串长度
	for (int i = 0; i < num; i++) {
		if (zf[i] > 64 && zf[i] < 123)//这个范围内有所有的字母,只需在这个ASCII值范围内筛选。
		{
			if (zf[i] > 64 && zf[i] < 91) //大写字母
			{
				zf[i] += 32; zf2 += zf[i]; continue;
			}//转化为小写字母
			if (zf[i] > 96 && zf[i] < 123)
			{
				zf2 += zf[i];
			}
		}
	}//题目要求滤去所有非字母字符(包括空格),且不考虑字母的大小写,所以用ASCII值将其转化为小写
	int num2 = zf2.length();
	for (int i = 0; i < num2 / 2; i++)
	{
		if (zf2[i] != zf[num2 - 1 - i]) {
			judge = 0;
			break;
		}
	}//左右方向比较大小是否相同
	if (judge == 1)
		cout << "The string is a palindrome." << endl;
	else 
		cout << "The string is not a palindrome." << endl;

	return 0;
}

题目四:
在这里插入图片描述

#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
int main() {
	srand((unsigned int)time(NULL));
	 int n ;//有n个人玩游戏;
	 cin >> n;
	 vector<int>v(n, 1);//数值1表示在圈内,0表示圈外
	 int m = rand()%n+1;//确定报到第m个数出圈
	 cout << "数到"<<m<<"时出圈" << endl;
	int s = 0;//出圈的人数;
	int c = 0;
	int k = 0;
	while (s != n)
	{
		c++;//当前报数人的编号
		if (c > n-1)//一共有n个人,在vector容器中最后一位是n-1
			c = 0;//循环一圈后,重新从第一个人计数
		if (v[c] == 1) {
			k++;//当前第c个人报的数为k
			if (k == m)
			{
				v[c] = 0;
				s++;
				cout << c << "  ";
				k = 0;
			}
		}
	}
	if (s = n)
		cout <<endl<< "此时最后一个报数的人是序号:" << c;
	return 0;
}

这一题关键在于vector是线型的,而题目的人物是按照圆圈站的,
要想到:

if (c > n-1)//一共有n个人,在vector容器中最后一位是n-1
			c = 0;//循环一圈后,重新从第一个人计数

并且要区分开,保号的人所报的数和保号的人是第几号,分别用两个字母表示。
出圈后,k恢复成0,而人数过一圈后要恢复成0

演示结果
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值