2014-04-23 24 views
0

我想創建整數的多個陣列(在C#)的陣列。但是它們都必須有索引,這是任何其他陣列具有在索引數唯一號。所以讓我嘗試告訴你我的意思是:C#訴諸以獨特的方式

int[] ints_array = new int[30]; 
     for (int i = 0; i < ints_array.Count(); i++) 
      ints_array[i] = i; 
//create a int array with 30 elems with each value increment by 1 

     List<int[]> arrayList = new List<int[]>(); 
     for(int i = 0; i < ints_array.Count(); i++) 
      arrayList.Add(ints_array[i]. //somehow sort the array here randomly so it will be unique 

所以我試圖讓ArrayList中有30 INT []數組和每個排序所以沒有陣列具有相同的索引另一相同INT 。

例子:

arrayList[0] = {5,2,3,4,1,6,7,8,20,21... etc } 
arrayList[1] = {1,0,5,2,9,10,29,15,29... etc } 
arrayList[2] = {0,28,4,7,29,23,22,17... etc } 

所以會這樣能夠在這個獨特的一種方式對數組進行排序?如果您需要了解更多信息,只需要詢問和填寫:)

+3

這只是一個謎題或有一個目的? –

+3

你有多少個陣列?它與數組長度相同嗎?這是否有效像數獨一樣? –

+0

@JonSkeet對不起,它與'ints_array'的長度相同。我想這是數獨的! – user3228693

回答

1

你可以那樣做:

List<int[]> arrayList = new List<int[]>(); 
Random rnd = new Random(); 
for (int i = 0; i < ints_array.Length; i++) 
{ 
    ints_array = ints_array.OrderBy(x => rnd.Next()).ToArray(); 
    var isDuplicate = arrayList.Any(x => x.SequenceEqual(ints_array)); 
    if (isDuplicate) 
    { 
      while (arrayList.Any(x => x.SequenceEqual(ints_array))) 
      { 
       ints_array = ints_array.OrderBy(x => rnd.Next()).ToArray(); 
      } 
     } 
     arrayList.Add(ints_array); 
} 

我想,這不會是在這種情況下比30。但大號碼,以便高效它不應該是一個問題,在我的機器它需要7毫秒。除非你需要一個純粹的隨機模式

+0

這似乎是在做伎倆:http://prntscr.com/3cqg4y 時間真的不重要。發揮魅力。謝謝。 – user3228693

-1
int size = 10; 

// generate table (no duplicates in rows, no duplicates in columns) 
// 0 1 2 
// 1 2 0 
// 2 0 1 
int[,] table = new int[size, size]; 
for (int y = 0; y < size; y++) 
    for (int x = 0; x < size; x++) 
     table[y, x] = (y + x) % size; 

// shuffle rows 
Random rnd = new Random(); 
for (int i = 0; i < size; i++) 
{ 
    int y1 = rnd.Next(0, size); 
    int y2 = rnd.Next(0, size); 
    for (int x = 0; x < size; x++) 
    { 
     int tmp = table[y1, x]; 
     table[y1, x] = table[y2, x]; 
     table[y2, x] = tmp; 
    } 
} 

// shuffle columns 
for (int i = 0; i < size; i++) 
{ 
    int x1 = rnd.Next(0, size); 
    int x2 = rnd.Next(0, size); 
    for (int y = 0; y < size; y++) 
    { 
     int tmp = table[y, x1]; 
     table[y, x1] = table[y, x2]; 
     table[y, x2] = tmp; 
    } 
} 

// sample output 
for (int y = 0; y < size; y++) 
{ 
    for (int x = 0; x < size; x++) 
     Console.Write("{0} ", table[y, x]); 
    Console.WriteLine(); 
} 
2

使用偏移模式迭代創建數組不會更容易嗎?

我的意思是,如果你使用創建1-30,其中1爲索引0處的第一陣列,接下來的陣列可以重複使用這其中2-30 2是在索引0,然後繞回至1和啓動只要你過去30年就會再次向前計數。這將是一種簡單可重複的方法,以確保沒有數組共享相同的值/索引對。

+0

這可以工作,但所有的數組將具有完全相同的順序,如果你想有不同的元素順序,但仍然保留唯一的索引?說,交換。 – Vlad

+0

@Vlad這是一個有效的點,但是OP沒有提到這個要求。如果這是他需要的東西,他必須澄清,然後這個答案可能不是一個合適的解決方案。現在,我認爲它應該工作正常:) –

1

傑西的想法是最好的。在這種情況下,我會建議生成一個隨機數,檢查所有以前的數組,然後將其放入一個數組,如果它不匹配任何其他數組當前索引。否則,生成一個新的隨機數,直到找到新的隨機數。把它放到一個循環中,直到所有的數組都被填充。

1

使用的矩陣(二維陣列)。比列表更容易處理。創建一個隨機數字發生器。確保只初始化它一次,否則隨機數字發生器可能會創建不好的隨機數字,如果在太短的時間間隔內創建的話,因爲慢速PC時鐘之間可能沒有打勾。 (實際時間用作種子值)。

private static Random random = new Random(); 

創建具有shuffeled索引兩個輔助陣列以行和列:

const int N = 30; 

    int[] col = CreateUniqueShuffledValues(N); 
    int[] row = CreateUniqueShuffledValues(N); 

然後創建並通過使用shuffeled行和列索引初始化矩陣:

// Create matrix 
    int[,] matrix = new int[N, N]; 
    for (int i = 0; i < N; i++) { 
     for (int j = 0; j < N; j++) { 
      matrix[row[i], col[j]] = (i + j) % N; 
     } 
    } 

的代碼使用這兩個輔助方法:

private static int[] CreateUniqueShuffledValues(int n) 
{ 
    // Create and initialize array with indexes. 
    int[] array = new int[n]; 
    for (int i = 0; i < n; i++) { 
     array[i] = i; 
    } 

    // Shuffel array using one variant of Fisher–Yates shuffle 
    // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle#The_modern_algorithm 
    for (int i = 0; i < n; i++) { 
     int j = random.Next(i, n); 
     Swap(array, i, j); 
    } 
    return array; 
} 

private static void Swap(int[] array, int i, int j) 
{ 
    int temp = array[i]; 
    array[i] = array[j]; 
    array[j] = temp; 
}