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.

翻译:

三、理论与实验

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

相关推荐
无聊的小坏坏1 小时前
一文讲通:二分查找的边界处理
数据结构·c++·算法
m0_528749001 小时前
C语言错误处理宏两个比较重要的
java·linux·算法
TracyCoder1232 小时前
LeetCode Hot100(50/100)——153. 寻找旋转排序数组中的最小值
算法·leetcode·职场和发展
诸葛务农2 小时前
点云配准在人形机器人中的应用:ICP算法(2)
人工智能·算法·机器学习·机器人
摘星编程2 小时前
**解锁Agent智能体新纪元:自主协作、任务分解与人类意图对齐的终极指南**
算法
mmz12072 小时前
逆序对问题(c++)
c++·算法
化学在逃硬闯CS2 小时前
Leetcode110.平衡二叉树
数据结构·c++·算法·leetcode
谢铭轩2 小时前
题解:P8035 [COCI 2015/2016 #7] Otpor
c++·算法
listhi5202 小时前
双目立体视觉中的彩色SAD算法
算法