经典错误:二维数组与实际矩阵的差异所构成的易错点
经典错误:二维数组与实际矩阵的差异所构成的易错点
经典错误:二维数组与实际矩阵的差异所构成的易错点
标签:C语言 二维数组 矩阵
by 小威威
在线性代数的课程中,我们接触到了矩阵这一工具,不难发现它很类似于C语言中的二维数组。虽然我们在C语言中可以用二维数组表示矩阵,但是二者的微小差异仍需要我们注意,否则有可能会导致错误。那么,它们两者有何差异呢?
很明显,由于数组的性质,二维数组有“0”行、“0”列,而矩阵没有。
即矩阵的第一行相当于二维数组的“0”行,矩阵的第一列相当于二维数组的“0”列。
那么,这一差异会带来什么错误呢?下面我用实例介绍。
军团再临
燃烧军团即将再次降临艾泽拉斯。萨尔为了改变对抗燃烧军团,使用地震法术,把一处平原改造成了山丘。之后,萨尔需要绘制出新的地图。
平原可以看做一个*M的棋盘(国际象棋那种)初始海拔为0。每次,萨尔将一个矩阵的海拔全部提高1 。这一个矩阵可以用左上角(sx,sx)和右下角(tx,ty)表示,也即第sx行第sy列的方格到第tx行第ty列的方格这样的一个矩形区域。绘制地图前,萨尔想知道最高的山峰 海拔是多少。使用完地震法术后,萨尔实在太累了,因此他把这个任务交给了你—他最聪明的朋友。
给出平原大小,M(行M列,行列都从1开始计算,也即第一行到第行,第一列到第M列)K 使用地震的次数。,M,K不超过500
问最高的山峰海拔是多少。
样例输入:
10 10
1 1
5 5
10 10
样例输出
我的代码:
# include <stdio.h>
int main(void) {int a[500][500] = {0}; // 将二维数组内全部元素赋0int , M, K, max; // 指行,M指列,K指地震次数,max表示最高海拔int b[500], c[500], d[500], e[500]; // 构造数组,用于存储坐标(不一定要构造数组)scanf(%d%d%d, &, &M, &K);for (int i = 0; i < K; i) // 该for循环将坐标值存储到数组中scanf(%d%d%d%d, &b[i], &c[i], &d[i], &e[i]);for (int i = 0; i < K; i) // 该for循环用于读取一维数组的数值for (int j = b[i]-1; j <= d[i]-1; j) // 这两个for循环用于对二维数组的操作for (int k = c[i]-1; k <= e[i]-1; k) { // 此处需要注意,要将坐标都减1a[j][k]; // 每地震一次,海拔加1}max = a[0][0]; // 接下来便是打擂台算法,可求出数组内最大值for (int j = 0; j < ; j)for (int k = 0; k < M; k) {if (a[j][k] > max) // 当元素值大于max中的值时,就将该元素的值赋给maxmax = a[j][k];}printf(%d\n, max);return 0;
}
本题的思路便是:用二维数组构造一个棋盘,并将所有元素赋值为0。地震区域内的元素全部加1,最后用打擂台算法得出数组内的最大值.
本题的易错点便是:地震区域+1处理这一环节。棋盘中的i行j列的元素其实是数组内的a[i-1][j-1](因为数组有“0”行也有“0”列)如果不进行坐标减1的操作,可能会导致数据溢出棋盘。如我的棋盘是10*10,但是我的数组最大元素也只是a[9][9],如果坐标没有减 1,则是直接对a[10][10]进行操作,已超出临界范围,会导致数据的疏漏。
Description
Mo and Larry have devised a way of encrypting messages. They first decide secretly on the number of columns and write the message (letters only) down the columns, padding with extra random letters so as to make a rectangular array of letters. For example, if the message is “There’s no place like home on a snowy night” and there are five columns, Mo would write down t o i o y h p k n n e l e a i r a h s g e c o n h s e m o t n l e w x ote that Mo includes only letters and writes them all in lower case. In this example, Mo used the character `x’ to pad the message out to make a rectangle, although he could have used any letter. Mo then sends the message to Larry by writing the letters in each row, alternating left-to-right and right-to-left. So, the above would be encrypted as toioynnkpheleaigshareconhtomesnlewx Your job is to recover for Larry the original message (along with any extra padding letters) from the encrypted one.
Input
There will be multiple input sets. Input for each set will cist of two lines. The first line will contain an integer in the range 2 . ..20 indicating the number of columns used. The next line is a string of up to 200 lower case letters. The last input set is followed by a line containing a single 0, indicating end of input.
Output
Each input set should generate one line of output, giving the original plaintext message, with no spaces.
Sample Input
Copy sample input to clipboard
5
toioynnkpheleaigshareconhtomesnlewx
ttyohhieneesiaabss
0
Sample Output
theresnoplacelikehomeonasnowynightx
thisistheeasyoneab
我的代码:
# include <stdio.h>
# include <string.h>
int main(void) {char code[200];int array[100][100]; // 用于储存字符数组内的字符int Columnumber, t; // Columnumber 指密码构成矩阵列数,t是一个中间变量,用于交换while (1) {scanf(%d, &Columnumber); // 输出密码构成的矩阵列数if (Columnumber == 0) return 0; // 倘若输入为0,结束程序scanf(%s, code); // 输入加密后的字符串for (int i = 0; i < strlen(code)/Columnumber; i)for (int j = 0; j < Columnumber; j)array[i][j] = code[Columnumber*ij]; // 实现字符数组向二维数组转移(类似于二维数组存储到一维数组)可看作一个通式for (int i = 1; i < strlen(code)/Columnumber; i=2) // 对偶数行进行倒序操作for (int j = 0; j < Columnumber; j) {if (j < Columnumber-1-j) { // 记得倒序要用交换而不是赋值t = array[i][j];array[i][j] = array[i][Columnumber-1-j];array[i][Columnumber-1-j] = t;}}for (int j = 0; j < Columnumber; j)for (int i = 0; i < strlen(code)/Columnumber; i)printf(%c, array[i][j]); // 将数组一列一列输出,记得以%c的形式printf(\n);}return 0;
}
这道题目的意思是要通过一个程序将加密后的字符串恢复原状。根据题目的意思:加密的原理便是:先要决定矩阵的列数,然后将原内容转成一个矩阵,不足以构成矩阵的在原内容后补上随机的字母。将原内容转成矩阵的方式便是将原内容竖着写,最终形成一个矩阵。接下来,把矩阵的偶数列进行倒序,得到一个新的矩阵,然后按照一行一行输出的形式输出新矩阵的内容,即为加密的内容。
本题的关键点:要读懂题目,到规律,用二维数组来解决有关矩阵的问题,这里就涉及到将字符数组转到二维数组,等同于将一维数组转成而二维数组。并且运用逆向思维解题。
本题的易错点:便是在倒序的过程中:1.用简单赋值的方式进行倒序(当然不能实现倒序);2.循环条件的控制,避免倒序完成后没有停止最终变回原来的顺序。
这里还有一个大牛写的代码,等我以后学了位运算,在对此代码进行解析。
#include <stdio.h>
#include <string.h>#define MAXl (2001)int main() {char s[MAXl];unsigned m, n, i, j;for (scanf(%u, &n); n > 0; scanf(%u, &n)) {scanf(%s, s);m = strlen(s) / n;for (i = 0; i < n; i)for (j = 0; j < m; j)printf(%c, s[j * n ((j & 1) ? (n - 1 - i) : i)]);printf(\n);}return 0;
}
以上内容皆为本人观点,欢迎大家提出意见,我们一起探讨!
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 10 条评论) |
本站网友 鼻咽癌治疗 | 3分钟前 发表 |
那么,它们两者有何差异呢? 很明显,由于数组的性质,二维数组有“0”行 | |
本站网友 如何瘦双下巴 | 7分钟前 发表 |
那么,这一差异会带来什么错误呢?下面我用实例介绍 | |
本站网友 哈尔滨商品房 | 18分钟前 发表 |
K | |
本站网友 设计师广场房价 | 0秒前 发表 |
棋盘中的i行j列的元素其实是数组内的a[i-1][j-1](因为数组有“0”行也有“0”列)如果不进行坐标减1的操作,可能会导致数据溢出棋盘 | |
本站网友 nudge | 10分钟前 发表 |
if the message is “There’s no place like home on a snowy night” and there are five columns | |
本站网友 男女之事 | 14分钟前 发表 |
样例输入: 10 10 1 1 5 5 10 10 样例输出 我的代码: # include <stdio.h> int main(void) {int a[500][500] = {0}; // 将二维数组内全部元素赋0int | |
本站网友 深圳出入境边防检查总站 | 18分钟前 发表 |
the above would be encrypted as toioynnkpheleaigshareconhtomesnlewx Your job is to recover for Larry the original message (along with any extra padding letters) from the encrypted one. Input There will be multiple input sets. Input for each set will cist of two lines. The first line will contain an integer in the range 2 . ..20 indicating the number of columns used. The next line is a string of up to 200 lower case letters. The last input set is followed by a line containing a single 0 | |
本站网友 values | 10分钟前 发表 |
&n)) {scanf(%s | |
本站网友 李晞媛 | 20分钟前 发表 |
d[500] |