C语言程序设计;生产具有不重复的随机数2维矩阵 [英] C Programming; Producing a 2-Dimensional Matrix with Random Numbers without Repetition
本文介绍了C语言程序设计;生产具有不重复的随机数2维矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过插入随机数的行和列中的元素不重复,以产生一个6×6矩阵。这是我的code为止。谢谢你的帮助!
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主要(无效)
{
int数组[6] [6];
INT行,列;
INT随机的;函数srand((无符号)时间(NULL));为(行= 0;行6;;行++)
{
为(列= 0;列6;;列++)
{
随机=兰特()%36 + 1; 数组[行] [列] =随机的;
的printf(%i的,数组[行] [列]);
} 的printf(\\ n);
}返回0;
}
解决方案
要避免重复:
- 创建36种元素的一维数组
- 填写的一维数组与数字1 THRU 36
- 费雪耶茨洗牌中的一维数组
- 使用一维数组的洗牌内容初始化的二维数组
I'd like to produce a 6x6 matrix by inserting random numbers to the elements in the row and column without repeating. This is my code so far. Thank you for your help!
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int array[6][6];
int rows, columns;
int random;
srand((unsigned)time(NULL));
for(rows=0;rows<6;rows++)
{
for(columns=0;columns<6;columns++)
{
random=rand()%36+1;
array[rows][columns] = random;
printf("%i",array[rows][columns]);
}
printf("\n");
}
return 0;
}
解决方案
To avoid repetition:
- create a 1D array of 36 elements
- fill the 1D array with the numbers 1 thru 36
- Fisher-Yates shuffle the 1D array
- use the shuffled contents of the 1D array to initialize the 2D array
这篇关于C语言程序设计;生产具有不重复的随机数2维矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文