C:创建具有唯一数字的二维数组

问题描述 投票:1回答:3

我想使用随机数在C中创建一个多维数组,但每行都有唯一的数字。我该怎么做呢 ?这是代码:

int main(int argc, char **argv)
{

int playfields [12][7];
int rows, columns;
int i;
int t,j;
int number[7], n=0;


for (i = 0; i<35; i++) {    
    number[i] = i +1;
}

for(rows = 0; rows < 12; rows++) {              
        for (columns = 0; columns < 7; columns++) {
            j = (rand() % 35) + 1;      
            t = number[j];
            number[rows] = t;
        }
    }


       for(rows = 0; rows < 12; rows++) {       
            for (columns=0; columns<7; columns++) { 
                playfields[rows][columns] = number[n++]; 
                printf("%d ", playfields[rows][columns]);
            }
            printf("\n");
        }

return 0;

}
c for-loop multidimensional-array random unique
3个回答
1
投票

简单来说,一种方法是将1中的数字分配给84数组中的1d,然后应用Fisher Yates algorithm进行洗牌。初始数字集之间的唯一性确保了您提到的标准之一。将1d数组中的数字分配给2d数组。这会在2d数组中生成随机数字序列。

您也可以将其更改为适用于2D阵列

srand(time(NULL));
...
for (int i = row - 1; i > 0; i--) {
        for (int j = col - 1; j > 0; j--) {
            int rr = rand() % (i) ;
            int cc = rand() % (j) ;

            int t = arr[i][j];
            arr[i][j] = arr[rr][cc];
            arr[rr][cc] = t;
        }
    }

0
投票

创建一个包含随机数的堆栈,然后将堆栈放入一个二维数组:

#include <time.h>
#include <stdlib.h>

#define ItemsCount  10

int main()
{
    srand(time(NULL));
    int i,j,flag,array[ItemsCount],array2d[ItemsCount/2][ItemsCount/2];

    // Create the stack
    for(i=0;i<ItemsCount;i++)
    {
        flag = 0;
        do
        {
            array[i] = rand();
            for(j=0;j<i;j++)
                if(array[i]==array[j])
                    flag = 1;

        }
        while(flag)

    }

    // assign them to 2d array
    for(i=0;i<ItemsCount/2;i++)
        for(j=0;j<ItemsCount/2;j++)
            array2d[i][j]=array[i+j];

    return 0;
}

另一种更有效的解决方案是生成连续数组。

1  2  3  4 
5  6  7  8
9 10 11 12

然后foreach元素选择另一个随机项表单数组并交换它们。这种方法不是那么随意但它有效。这取决于您的需求。


0
投票

所提出的解决方案为阵列中的每个单元生成唯一的随机数。

每个单元格都会从移动范围中获得一个随机数。一旦阵列具有唯一的随机数,如果可以进一步改组。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NR_OF_ROWS   12
#define NR_OF_COLS    7
#define RANGE        10

int get_random(int M, int N)
{
    int r = M + rand() / (RAND_MAX / (N - M) + 1);
    return r; 
}

void shuffle(int numberOfShuffles, int table[][NR_OF_COLS] ) 
{
    int row1,row2,col1,col2,temp1,temp2;

    for (int i=0; i< numberOfShuffles; i++)
    {
        row1 = get_random(0, NR_OF_ROWS);
        col1 = get_random(0, NR_OF_COLS);

        row2 = get_random(0, NR_OF_ROWS);
        col2 = get_random(0, NR_OF_COLS);

        temp1 = table[row1][col1];
        temp2 = table[row2][col2];

        table[row1][col1] = temp2;
        table[row2][col2] = temp1;         
    }
}

void print_table( int table[][NR_OF_COLS] ) 
{
    printf("\n");   

    for(int rows = 0; rows < NR_OF_ROWS ; rows++) {              
      for (int columns = 0; columns < NR_OF_COLS; columns++){
        printf( "<pf=%4d>  ",table[rows][columns]);   
      }
     printf("\n");
    }
}

int main(int argc, char **argv)
{
int playfields [NR_OF_ROWS][NR_OF_COLS];

int rows, columns;
int number;
int lRange,hRange;
int n=0;
int numberOfShuffles = NR_OF_ROWS*NR_OF_COLS;

srand (time (NULL)); //  seed generator

for(rows = 0; rows < NR_OF_ROWS ; rows++) {              
    for (columns = 0; columns < NR_OF_COLS; columns++) {

        lRange = n*RANGE+1;
        hRange = n*RANGE + RANGE;

        number = get_random(lRange, hRange); // generate a unique number which will belong to  <lRange, lRange> range 

        playfields[rows][columns] = number;
        printf( "l=%4d h=%4d <pf=%4d>  ", lRange, hRange, playfields[rows][columns]);

        n++;
    }
    printf("\n");
}

//Now your table has unique numbers you can shuffle them if you wish: 

shuffle(numberOfShuffles, playfields);
print_table(playfields);

return 0;
}

OUTPUT:

l=   1 h=  10 <pf=   4>  l=  11 h=  20 <pf=  13>  l=  21 h=  30 <pf=  22>  l=  31 h=  40 <pf=  33>  l=  41 h=  50 <pf=  41>  l=  51 h=  60 <pf=  52>  l=  61 h=  70 <pf=  68>                                                                     
l=  71 h=  80 <pf=  78>  l=  81 h=  90 <pf=  86>  l=  91 h= 100 <pf=  91>  l= 101 h= 110 <pf= 106>  l= 111 h= 120 <pf= 116>  l= 121 h= 130 <pf= 123>  l= 131 h= 140 <pf= 132>                                                                     
l= 141 h= 150 <pf= 148>  l= 151 h= 160 <pf= 156>  l= 161 h= 170 <pf= 167>  l= 171 h= 180 <pf= 171>  l= 181 h= 190 <pf= 187>  l= 191 h= 200 <pf= 194>  l= 201 h= 210 <pf= 203>                                                                     
l= 211 h= 220 <pf= 219>  l= 221 h= 230 <pf= 225>  l= 231 h= 240 <pf= 232>  l= 241 h= 250 <pf= 248>  l= 251 h= 260 <pf= 256>  l= 261 h= 270 <pf= 262>  l= 271 h= 280 <pf= 273>                                                                     
l= 281 h= 290 <pf= 287>  l= 291 h= 300 <pf= 295>  l= 301 h= 310 <pf= 304>  l= 311 h= 320 <pf= 312>  l= 321 h= 330 <pf= 328>  l= 331 h= 340 <pf= 335>  l= 341 h= 350 <pf= 345>                                                                     
l= 351 h= 360 <pf= 358>  l= 361 h= 370 <pf= 367>  l= 371 h= 380 <pf= 373>  l= 381 h= 390 <pf= 386>  l= 391 h= 400 <pf= 393>  l= 401 h= 410 <pf= 403>  l= 411 h= 420 <pf= 413>                                                                     
l= 421 h= 430 <pf= 429>  l= 431 h= 440 <pf= 436>  l= 441 h= 450 <pf= 444>  l= 451 h= 460 <pf= 458>  l= 461 h= 470 <pf= 462>  l= 471 h= 480 <pf= 472>  l= 481 h= 490 <pf= 488>                                                                     
l= 491 h= 500 <pf= 499>  l= 501 h= 510 <pf= 506>  l= 511 h= 520 <pf= 512>  l= 521 h= 530 <pf= 528>  l= 531 h= 540 <pf= 531>  l= 541 h= 550 <pf= 544>  l= 551 h= 560 <pf= 557>                                                                     
l= 561 h= 570 <pf= 566>  l= 571 h= 580 <pf= 575>  l= 581 h= 590 <pf= 581>  l= 591 h= 600 <pf= 594>  l= 601 h= 610 <pf= 601>  l= 611 h= 620 <pf= 614>  l= 621 h= 630 <pf= 625>                                                                     
l= 631 h= 640 <pf= 639>  l= 641 h= 650 <pf= 648>  l= 651 h= 660 <pf= 651>  l= 661 h= 670 <pf= 667>  l= 671 h= 680 <pf= 675>  l= 681 h= 690 <pf= 683>  l= 691 h= 700 <pf= 694>                                                                     
l= 701 h= 710 <pf= 708>  l= 711 h= 720 <pf= 715>  l= 721 h= 730 <pf= 727>  l= 731 h= 740 <pf= 738>  l= 741 h= 750 <pf= 742>  l= 751 h= 760 <pf= 752>  l= 761 h= 770 <pf= 766>                                                                     
l= 771 h= 780 <pf= 774>  l= 781 h= 790 <pf= 784>  l= 791 h= 800 <pf= 795>  l= 801 h= 810 <pf= 803>  l= 811 h= 820 <pf= 819>  l= 821 h= 830 <pf= 826>  l= 831 h= 840 <pf= 832>                                                                           

<pf= 287>  <pf= 256>  <pf= 106>  <pf= 738>  <pf=  22>  <pf= 648>  <pf= 819>                                                                                                                                                                       
<pf=  33>  <pf= 295>  <pf=  91>  <pf= 358>  <pf= 203>  <pf= 774>  <pf= 132>                                                                                                                                                                       
<pf= 403>  <pf= 594>  <pf= 667>  <pf= 499>  <pf= 156>  <pf=  13>  <pf= 194>                                                                                                                                                                       
<pf= 614>  <pf= 694>  <pf= 826>  <pf= 557>  <pf= 708>  <pf= 367>  <pf= 273>                                                                                                                                                                       
<pf= 742>  <pf= 225>  <pf= 506>  <pf= 312>  <pf= 328>  <pf= 335>  <pf= 345>                                                                                                                                                                       
<pf= 123>  <pf= 373>  <pf= 528>  <pf= 248>  <pf= 393>  <pf= 472>  <pf= 187>                                                                                                                                                                       
<pf= 386>  <pf= 262>  <pf= 444>  <pf= 458>  <pf= 167>  <pf= 625>  <pf= 752>                                                                                                                                                                       
<pf= 512>  <pf= 601>  <pf=   4>  <pf=  41>  <pf= 531>  <pf= 727>  <pf= 651>                                                                                                                                                                       
<pf= 566>  <pf=  86>  <pf= 683>  <pf=  68>  <pf= 832>  <pf= 488>  <pf= 171>                                                                                                                                                                       
<pf= 116>  <pf=  52>  <pf= 429>  <pf= 544>  <pf= 675>  <pf= 148>  <pf= 639>                                                                                                                                                                       
<pf= 436>  <pf= 715>  <pf= 575>  <pf= 219>  <pf= 413>  <pf= 581>  <pf= 766>                                                                                                                                                                       
<pf=  78>  <pf= 784>  <pf= 795>  <pf= 803>  <pf= 462>  <pf= 232>  <pf= 304>  
© www.soinside.com 2019 - 2024. All rights reserved.