问题
I've a 8x8 matrix, and after choosing the row I desire, I want to get the three minimum elements of it, and choose one of this three randomly. The thing is that I'dont know how to handle those three elements. I just know how to get the minimum element, that is the following code.
int piezas[8][8] = {
0, 2, 2, 5, 3, 2, 1, 1,
0, 4, 5, 2, 4, 3, 0, 0,
0, 4, 2, 2, 1, 2, 3, 2,
0, 3, 1, 5, 1, 2, 3, 4,
2, 5, 6, 5, 3, 1, 2, 7,
8, 2, 0, 0, 0, 2, 1, 1,
1, 2, 2, 1, 1, 6, 3, 4,
};
int myrow = 3; // the row I want to analyze
int index;
int min=0;
for (index=0;index<8;index++) {
printf("%d", piezas[myrow][index] );
if(piezas[myrow][index]<min)
min=piezas[myrow][index];
printf("\t\t");
}
printf("min: %d", min);
The output I want to have is, if the initial matrix is:
int piezas[8][8] = {
0, 2, 2, 5, 3, 2, 1, 1,
0, 4, 5, 2, 4, 3, 0, 0,
0, 4, 2, 2, 1, 2, 3, 2,
0, 3, 1, 5, 1, 2, 3, 4,
2, 5, 6, 5, 3, 1, 2, 7,
8, 2, 0, 0, 0, 2, 1, 1,
1, 2, 2, 1, 1, 6, 3, 4,
};
And I choose row number 3:
0, 3, 1, 5, 1, 2, 3, 4,
The algorithm must choose
0, 1, 1
And choose randomly one of these three.
Can someone give me any ideas of how can I do it? I'm stuck with this since early this morning. Thanks
回答1:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE_ROW 8
#define N_MIN 3
int piezas[SIZE_ROW][SIZE_ROW] = {
0, 2, 2, 5, 3, 2, 1, 1,
0, 4, 5, 2, 4, 3, 0, 0,
0, 4, 2, 2, 1, 2, 3, 2,
0, 3, 1, 5, 1, 2, 3, 4,
2, 5, 6, 5, 3, 1, 2, 7,
8, 2, 0, 0, 0, 2, 1, 1,
1, 2, 2, 1, 1, 6, 3, 4,
};
int sort(const void *x, const void *y) {
return (*(int*)x - *(int*)y);
}
int* sort_array(int* row, int size_row){
int* output = (int*) calloc(size_row, sizeof(int) );
memcpy(output, row, size_row*sizeof(int) ); // copy array
qsort (output, size_row, sizeof (int), sort);
return output;
}
int random_pick(int* array, int size_row){
return array[ rand() % size_row ]; // possible buffer overflow if size_row too big.
}
int main(void){
srand(time(NULL));
int myrow = 3; // the row I want to analyze
int* sorted_row = NULL;
int i,j;
sorted_row = sort_array(piezas[myrow],SIZE_ROW );
printf("N mins : \n");
for(i=0;i<N_MIN;i++){
printf(" %d ", sorted_row[i] );
}
printf("\n");
printf("Random Pick : %d \n", random_pick(sorted_row, N_MIN) );
}
回答2:
I would try sorting the row and random one of the three first elements then.
// integer comparator
int compare(int * a, int * b) {return *a - *b;}
// allocate memory to hold the copy
int rowCopy[sizeof(piezas[myrow])/sizeof(int)];
// copy the row
memcpy(rowCopy, piezas[myrow], sizeof(piezas[myrow]));
// sort it
qsort(rowCopy, sizeof(piezas[myrow])/sizeof(int), sizeof(rowCopy[0]), compare);
// initialize the random number generator
srand(time(NULL));
// return randomly one of the first 3 elements
return rowCopy[rand() % 3]
回答3:
Create a copy of one row in tmp_array (8 elements), then qsort(tmp_array)
, and finally use rand() % 3
as your answer element number.
回答4:
Simple solution is you have three variables min1, min2, min3 to hold the three minimum variables.
回答5:
take a array numbers[3] and save these three values in it.Now
int length = sizeof(numbers) / sizeof(int);
int randomNumber = numbers[rand() % length];
回答6:
Generally speaking , you can use heapsort to get the N minimum elements in a array such as the row you choose, you needn't sort all elements.But remember not saving the sorting result in the row itself. And use rand() to pick up one of them.
回答7:
#include <stdio.h>
int main(void){
int piezas[8][8] = {
0, 2, 2, 5, 3, 2, 1, 1,
0, 4, 5, 2, 4, 3, 0, 0,
0, 4, 2, 2, 1, 2, 3, 2,
0, 3, 1, 5, 1, 2, 3, 4,
2, 5, 6, 5, 3, 1, 2, 7,
8, 2, 0, 0, 0, 2, 1, 1,
1, 2, 2, 1, 1, 6, 3, 4,
};
int myrow = 3; // the row I want to analyze
int index;
int min[3]={99,99,99};//or INT_MAX at <limits.h>
for (index=0;index<8;index++) {
printf("%d", piezas[myrow][index] );
int i, temp = piezas[myrow][index];
for(i=0;i<3;++i){
if(temp<=min[i]){
int wk = min[i];
min[i]=temp;
temp = wk;
}
}
printf(" ");
}
printf("min:");
for(index=0;index<3;++index)
printf(" %d", min[index]);
return 0;
}
来源:https://stackoverflow.com/questions/16518371/c-getting-the-3-minimum-elements-of-an-row-in-a-matrix-and-choose-one-randoml