2.12矩阵问题,发牌,数字金字塔

1.题目:

从键盘上输入一个整数N,按以下规律输出一个(2N+1)*(2N+1)的矩阵:

对角线的值为1,

上半三角区域值为2,

下半三角区域值为3,

左半三角区域的值为4,

右半三角区域的值为5。

输入:

1

8

输出:

1 2 1

4 1 5

1 3 1

1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1

4 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1 5

4 4 1 2 2 2 2 2 2 2 2 2 2 2 1 5 5

4 4 4 1 2 2 2 2 2 2 2 2 2 1 5 5 5

4 4 4 4 1 2 2 2 2 2 2 2 1 5 5 5 5

4 4 4 4 4 1 2 2 2 2 2 1 5 5 5 5 5

4 4 4 4 4 4 1 2 2 2 1 5 5 5 5 5 5

4 4 4 4 4 4 4 1 2 1 5 5 5 5 5 5 5

4 4 4 4 4 4 4 4 1 5 5 5 5 5 5 5 5

4 4 4 4 4 4 4 1 3 1 5 5 5 5 5 5 5

4 4 4 4 4 4 1 3 3 3 1 5 5 5 5 5 5

4 4 4 4 4 1 3 3 3 3 3 1 5 5 5 5 5

4 4 4 4 1 3 3 3 3 3 3 3 1 5 5 5 5

4 4 4 1 3 3 3 3 3 3 3 3 3 1 5 5 5

4 4 1 3 3 3 3 3 3 3 3 3 3 3 1 5 5

4 1 3 3 3 3 3 3 3 3 3 3 3 3 3 1 5

1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1

个人心得:

1.输入先创立一个静态数组,在填写对应的先令对角线i=j为1

2.在让上三角区为2,j>i,m-1-j>i为上三角区,其它区域见代码

3.以此填好以后按行列输出

复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<vector>
using namespace std;
int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		int a[20][20] = { 0 };
		int m = 2 * n + 1;
		for (int i = 0; i < m; ++i) {
			a[i][i] = 1;
			a[i][m -1 - i] = 1;
			for (int j = 0; j < m; ++j) {
				if (j > i && m-1 - j > i) {
					a[i][j] = 2;
				}
				if (j > i && m-1 - j < i) {
					a[i][j] = 5;
				}
				if (j<i && m-1 - j>i) {
					a[i][j] = 4;
				}
				if (j < i && m-1 - j < i) {
					a[i][j] = 3;
				}
			}
		}
		//输出
		for (int i = 0; i < m; ++i) {
			for (int j = 0; j < m; ++j) {
				if (j > 0) {
					printf(" ");
				}
				printf("%d", a[i][j]);
			}
			printf("\n");
		}
	}
}

2.题目:

编制一个模拟发牌的程序。有编号为1,2,3,4四个人,将一付去掉大小怪的扑克按照如下顺序排列梅花c0-c12,方块d0-d12,红桃h0--h12,黑桃s0-s12,然后按照1,2,3,4四个人的顺序发牌,问最后每个人手上的牌有哪些。

输入:

1

2

输出:

c 0 c 4 c 8 c 12 d 3 d 7 d 11 h 2 h 6 h 10 s 1 s 5 s 9

c 1 c 5 c 9 d 0 d 4 d 8 d 12 h 3 h 7 h 11 s 2 s 6 s 10

个人心得:

1.建立一个数组里面存放字母c到s

2.建立一个空数组,向里面存放0到12的数字,每一行的数字从i开始,相差4

3.输出时,当数字大于12则余13,每次输出前面为b数组中的字母,后面为数字

复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<vector>
using namespace std;
int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		int a[4][13];
		char b[] = { 'c','d','h','s' };
		int k = 0;
		int count = 0;
		for (int i = 0; i < 4; i++) {
			int paishu = i;
			for (int j = 0; j < 13; j++) {
				a[i][j] = paishu;
				paishu = paishu + 4;
			}
		}
		for (int i = 0; i < 13; ++i) {
			if (i > 0) {
				printf(" ");
			}
			int card = a[n - 1][i];
			int suit = card / 13;   
			int num = card % 13;

			printf("%c %d", b[suit], num);
		}
		printf("\n");
	}
}

3.题目:

考虑在下面被显示的数字金字塔(第n行有n列)。写一个程序来计算从最高点开始在底部任意处结束的路径经过数字的和的最大。每前进一步可以走到它的正下方或者右下方(往下一行、往右一列)的位置。

7

3 8

8 1 0

2 7 4 4

4 5 2 6 5

在上面的样例中,从7 到 3 到 8 到 7 到 5 的路径产生了最大和:30

输入:

6

7

3 8

8 1 0

2 7 4 100

4 5 2 6 5

3 2 5 8 7 6

输出:

129

个人心得:

1.先建立一个返回输入最大值的函数,方便后续使用

2.在主函数中输入完数组以后,我们对数组处理,如果i为0j为0时我们只能向下垂直走,如果为对角线,则只可能是斜上方走下来的,没一层都保留上面走下来的最大值,最后遍历最后一层,选出最大值即可。

复制代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<vector>
using namespace std;
int max(int a, int b) {
	if (a > b) {
		return a;
	}
	else {
		return b;
	}
}
int a[1000][1000] = { 0 };
int main() {
	int n;
	scanf("%d", &n);

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j <= i; ++j) {
			scanf("%d", &a[i][j]);
		}
	}
	for (int i = 1; i < n ; ++i) {
		for (int j = 0; j <= i; j++) {
			if (j == 0) {
				a[i][j] = a[i][j] + a[i - 1][j];
			}
			else if (i == j) {
				a[i][j] = a[i][j] + a[i - 1][j - 1];
			}
			else {
				a[i][j] = a[i][j] + max(a[i - 1][j], a[i - 1][j - 1]);
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; ++i) {
		if (a[n - 1][i] > sum) {
			sum = a[n - 1][i];
		}
	}
	printf("%d\n", sum);

}

第一段:

Computer scientists continue to expand the frontiers of computer and information systems by pioneering the designs of more complex, reliable, and powerful computers; enabling networks of computers to efficiently exchange vast amounts of information; and seeking ways to make computers behave intelligently. As computers become an increasingly integral part of modern society, computer scientists strive to solve new problems and invent better methods of solving current problems.

翻译:

计算机科学家们通过开创更加复杂、可靠和强大的计算机设计,建立能够高效交换海量信息的计算机网络,以及探索使计算机具备智能行为的方法,不断拓展计算机与信息系统的前沿。随着计算机日益成为现代社会中不可或缺的一部分,计算机科学家努力解决新的问题,并发明更好的方法来解决当前的问题。

第二段;

The goals of computer science range from finding ways to better educate people in the use of existing computers to highly speculative research into technologies and approaches that may not be viable for decades. Underlying all of these specific goals is the desire to better the human condition today and in the future through the improved use of information.

翻译:

计算机科学的目标范围很广,从寻找更好的方法教育人们使用现有计算机,到对那些可能在几十年内才可行的技术和方法进行高度探索性的研究。在所有这些具体目标的背后,都有一个共同的愿望------通过更好地利用信息,改善当今和未来的人类生活。

第三段:

III. Theory and Experiment

Computer science is a combination of theory, engineering, and experimentation. In some cases, a computer scientist develops a theory, then engineers a combination of computer hardware and software based on that theory, and experimentally tests it. An example of such a theory-driven approach is the development of new software engineering tools that are then evaluated in actual use. In other cases, experimentation may result in new theory, such as the discovery that an artificial neural network exhibits behavior similar to neurons in the brain, leading to a new theory in neurophysiology.

翻译:

三、理论与实验

计算机科学是理论、工程和实验的结合。在某些情况下,计算机科学家首先提出一个理论,然后基于该理论设计计算机硬件和软件的组合,并通过实验进行测试。这种以理论为驱动的方法的一个例子是开发新的软件工程工具,然后在实际使用中对其进行评估。另一方面,有时实验也可能催生新的理论,例如发现人工神经网络表现出类似大脑神经元的行为,从而引发神经生理学中的新理论。

相关推荐
hetao17338375 小时前
2026-09-17 hetao1733837 的刷题记录
c++·算法
午彦琳6 小时前
2026.9.17
数据结构·算法·leetcode
木井巳6 小时前
【记忆化搜索】不同路径
java·算法·leetcode·深度优先·剪枝·推荐算法
怕浪猫7 小时前
从 Windows 换到 Mac 三个月,我真香了
算法·面试·架构
aichitang20248 小时前
前端小skill
前端·人工智能·算法·ai·前端框架
All for pursuit.9 小时前
【链表-9】146.LRU缓存
数据结构·c++·算法·leetcode
木子算法9 小时前
测出来的值会抖:约束和目标带噪声时,「可行」和「更好」该怎么判
人工智能·算法·目标跟踪
hanlin0310 小时前
刷题笔记:力扣第144题-二叉树的前序遍历
笔记·算法·leetcode
金士曼10 小时前
从规则到涌现:算法认知的三个层次
算法
微三云 - 廖会灵 (私域系统开发)11 小时前
私域电商多端商城系统架构实践:小程序 / APP / H5 一套后端如何落地
矩阵·重构·零售