2013-05-13 26 views
0

幾個小時前我做了a question,但是在完成我在這個問題上提出的問題之後,我不得不做些什麼。人們給我的所有解決方案都沒問題,但對於我真正需要的東西卻毫無用處,因爲我沒有寫出這個問題。我必須保存一個重要的價值位置,而不必爲了解決問題而保存在其他問題上。所以這是正確的。我有一個8x8的矩陣,在選擇了我想要的那一行之後,我想得到它的三個最小元素,然後從這三個元素中選擇一個隨機。然後,刪除包含此號碼的行和列。問題是我不知道如何處理這三個元素並刪除列/行。我只知道如何獲得最小的元素,即下面的代碼。查找值後刪除矩陣的行和列(C)

int pieza[ROWS][COLS] = { 
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, 
0, 1, 3, 2, 0, 0, 0, 0, 
}; 

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); 

這是我想做的事情。如果初始矩陣是(這始終是一個nxn矩陣):

{ 
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, 
0, 1, 3, 2, 0, 0, 0, 0, 
}; 

我選擇行號3:

0, 3, 1, 5, 1, 2, 3, 4, 

該算法必須選擇該行的三個最小元素。

0, 1, 1 

並隨機選擇其中的一個。如果,例如,它選擇的第一個「一」 ...

0, **1**, 1 

...算法必須去到該行的這是第3列(becaue那是那是立場,即「1」)和刪除的行和列,所以輸出矩陣將如下,一個尺寸小於原來的矩陣(beucase您已刪除的行和列):

{ 
    0, 2, 5, 3, 2, 1, 1, 
    0, 4, 2, 4, 3, 0, 0, 
    0, 4, 2, 1, 2, 3, 2, 
    2, 5, 5, 3, 1, 2, 7, 
    8, 2, 0, 0, 2, 1, 1, 
    1, 2, 1, 1, 6, 3, 4, 
    0, 1, 2, 0, 0, 0, 0, 
    }; 

我只知道如何到達行,但我有問題處理三個最低限度,因爲我有很多問題指針,我不是很多C.
在此先感謝

+0

你的最終矩陣(所希望的輸出)不有道理的是,你不能有一個被聲明爲[8] [8]的矩陣,並且只爲7x7矩陣提供初始值。事實上,在7個元素之後放置一個換行符對C編譯器沒有任何意義,它會給你一個8x8矩陣,因爲這就是你要求的。 – unwind 2013-05-13 11:03:16

+0

我的錯。如果給出一個n×n矩陣,輸出矩陣必須是n-1×n-1矩陣。少一個維度。該帖子現在被編輯。 – 2013-05-13 11:05:10

回答

1

要按列數排序的示例。

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

typedef struct pair { 
    int value, column; 
} Pair; 

int cmp(const void *a, const void *b){ 
    Pair *pa = (Pair *)a; 
    Pair *pb = (Pair *)b; 
    return pa->value - pb->value; 
} 

int main(void){ 
    int data[8] = {0, 3, 1, 5, 1, 2, 3, 4}; 
    Pair data_pair[8]; 
    int i; 
    for(i=0;i<8;++i){ 
     data_pair[i].value = data[i]; 
     data_pair[i].column = i; 
    } 
    qsort(data_pair, 8, sizeof(Pair), cmp); 
    for(i=0;i<3;++i) 
     printf("value = %d, column = %d\n", data_pair[i].value, data_pair[i].column); 
    return 0; 
} 
/* result 
value = 0, column = 0 
value = 1, column = 2 
value = 1, column = 4 
*/ 
+0

非常感謝你這麼這麼好BLUEPIXY。 – 2013-05-14 09:58:40

+0

不客氣。 – BLUEPIXY 2013-05-14 10:00:28

1
#include <stdio.h> 
#include <string.h> 

#define SIZE 8 

void delrow(int a[SIZE][SIZE], int row){ 
    if(row < SIZE - 1) 
     memmove(&a[row], &a[row+1], (SIZE*SIZE - SIZE*(row+1))*sizeof(int)); 
}; 
void delcol(int a[SIZE][SIZE], int col){ 
    int r; 
    if(col < SIZE - 1){ 
     for(r=0;r<SIZE;++r){ 
      memmove(&a[r][col], &a[r][col+1], (SIZE - (col+1))*sizeof(int)); 
     } 
    } 
} 

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, 
     0, 1, 3, 2, 0, 0, 0, 0, 
    }; 
    //test 
    int row = 8, col = 8; 
    int r,c; 
    delrow(piezas, 3); 
    row -= 1; 
    for(r=0;r<row;++r){ 
     for(c=0;c<col;++c) 
      printf("%2d", piezas[r][c]); 
     printf("\n"); 
    } 
    printf("\n"); 
    delcol(piezas, 1); 
    col -= 1; 
    for(r=0;r<row;++r){ 
     for(c=0;c<col;++c) 
      printf("%2d", piezas[r][c]); 
     printf("\n"); 
    } 
    return 0; 
} 
/* result 
0 2 2 5 3 2 1 1 
0 4 5 2 4 3 0 0 
0 4 2 2 1 2 3 2 
2 5 6 5 3 1 2 7 
8 2 0 0 0 2 1 1 
1 2 2 1 1 6 3 4 
0 1 3 2 0 0 0 0 

0 2 5 3 2 1 1 
0 5 2 4 3 0 0 
0 2 2 1 2 3 2 
2 6 5 3 1 2 7 
8 0 0 0 2 1 1 
1 2 1 1 6 3 4 
0 3 2 0 0 0 0 
*/ 
+0

感謝您的答案,但只刪除一列而不是一行。 事情是,在到達刪除行和列之前,我有問題找到值,因爲你可以在帖子中閱讀。 – 2013-05-13 11:45:40

+0

@Borja你的數據似乎已經改變了,但我認爲我的程序沒有問題。你的例子刪除第二colomn。 – BLUEPIXY 2013-05-13 12:02:07

+0

我只是把矩陣放在'nxn'格式上。你的輸入矩陣有7行,你的輸出矩陣也有7行,輸出矩陣必須少一行,就像列一樣,因爲你必須刪除兩行,而你只刪除一列。檢查帖子的例子。提前致謝。 – 2013-05-13 12:05:52

0

下面是提取物中除去的行和列之前從選定的行獲得第n個最小的元素 - 商家第一要求。

  1. 複製行
  2. 排序複製的行(保存索引太而分選)
  3. 排序索引表示按排序順序的值位置
  4. 挑0 th或第一或第n分鐘索引排序索引。

------------非常代碼草案----嘗試優化-------

#include <stdio.h> 
#include<memory.h> 

void sortIndex(int *array, int *arrayIdx) 
{ 
int i=0,j=0; 
int temp=0; 

int tempArr[4]; 

memcpy(tempArr, array, 4*sizeof(int)); 
for(i=0;i<4;i++) 
{ 
    printf("%d ",tempArr[i]); 
} 
printf("\n"); 

    for(i=0;i<4;i++) 
    { 
    for(j=i+1;j<4;j++) 
    { 
     if(tempArr[i]>tempArr[j]) 
     { 
     temp = arrayIdx[i]; 
     arrayIdx[i]=arrayIdx[j]; 
     arrayIdx[j]=temp; 

     temp = tempArr[i]; 
     tempArr[i]=tempArr[j]; 
     tempArr[j]=temp; 
     } 
    } 
    } 
printf("Sorted array Index\n"); 
for(i=0;i<4;i++) 
{ 
    printf("%d ",arrayIdx[i]); 
} 
printf("\n"); 
printf("Sorted array Value\n"); 
for(i=0;i<4;i++) 
{ 
    printf("%d ",array[arrayIdx[i]]); 
} 
printf("\n"); 
} 


int main() 
{ 
int array[4][4] = {{4,3,2,1},{7,5,4,3},{6,5,4,4},{5,5,2,1}}; 
int sortedIdx[4] = {0,1,2,3}; 
int i,ii; 

for(i=0;i<4;i++) 
{ 
    for(ii=0;ii<4;ii++) 
     printf("%d ",array[i][ii]); 
    printf("\n"); 
} 

printf("(Note:Count from 0). Which Row : "); 
scanf("%d",&i); 
sortIndex(array[i],sortedIdx); 

printf("\n"); 

printf("(Nth smallest value)Give a N value (0 to 3): "); 
scanf("%d",&ii); 
printf(" (%d) smallest value in row (%d) is (%d)\n",ii,i,array[i][sortedIdx[ii]]); 

printf("Now call function to remove Row (%d) and column (%d)\n",i,sortedIdx[ii]); 

return 0; 
}