我有這樣一段代碼:
for (int i = 0; argv[1][i]; i ) {
case '2':
strcpy(letters[i], "abc");
break;
case '3':
strcpy(letters[i], "def");
break;
case '4':
strcpy(letters[i], "def");
break;
case '5':
strcpy(letters[i], "jkl");
break;
case '6':
strcpy(letters[i], "mno");
break;
case '7':
strcpy(letters[i], "pqrs");
break;
case '8':
strcpy(letters[i], "tuv");
break;
case '9':
strcpy(letters[i], "wxyz");
break;
}
}
我必須從不同的元素中獲得不同的字符組合。例如,如果用戶列印 575,這段代碼將生成二維陣列 {"jkl", "pqrs", "jkl"}。之后我還應該用不同的字母組合制作二維陣列,一個來自元素的字母(例如{“jpj”、“jpk”、“jpl”、“krl”}等)。問題在于如何進行這些組合。我不明白我該怎么做。還禁止使用動態記憶體(malloc、free 等)和準備好的排序函式(qsort、lsearch 等)。
我嘗試使用嵌套回圈和遞回,但我不知道如何在這種情況下正確使用它。我將非常感謝任何理論或實踐的幫助。
uj5u.com熱心網友回復:
信集
下面的解決方案僅使用迭代。獲取字母集的 2D 陣列非常簡單,并且使用您已有的代碼。需要注意的一個重要細節是,如果您將這些字母設定為可列印,則需要考慮額外的空終止字符'\0'。因此,字母集陣列的每一行將包含最大字母數 1,即4 1個字符。
組合陣列
要在不調整動態陣列大小的情況下生成組合,您需要知道存在多少組合。這是計算為每組字母數的乘積。所以現在,您可以宣告一個靜態陣列,每個組合和n 1列有 1 行,每個字母集一個加上空終止字符。
代表一個組合
除了明顯的基于字母的方式外,組合可以表示為索引向量,其中第 i 個索引表示從第 i 個字符集中選擇的字符。因此,向量(0, 0, 2)表示第一個字符取自第一組,第一個字符取自第二組,第三個字符取自第三組的組合。
有了這個索引向量,就很容易構造相應的字串。優點是也很容易生成下一個組合!從最后一個索引開始,如果結果索引小于字母集的大小,則將其遞增。如果是這樣,那么你有一個新的索引向量代表一個新的看不見的組合。如果結果索引超過設定大小,則需要將其設定為零并將此程序應用于向量的下一個(向左)索引,直到找到有效增量。
組合順序
您希望對所有組合進行排序。請注意,所有集合中的字母都已排序。因此,按照我描述的程序,組合本身也將被排序,因為您從最后一個字符到第一個字符詳盡地生成它們,并且每個位置的字符都按字典順序提供。所以不需要顯式地對結果進行排序。
注意:在代碼中,為了方便測驗,我使用了一個區域變數arg來提供輸入數字。只需將其更改為從argv[1].
#include <stdio.h>
#include <string.h>
int main(){
char *arg = "575";
// Get length of argument (denotes number of letter sets)
int n = strlen(arg);
if (n == 0){
printf("No digits given");
return 0;
}
// Declare 2D array of letters (n rows, 5 columns)
char letters[n][5];
int total_combinations = 1;
// Fill array with characters
for (int i = 0; arg[i]; i ) {
switch(arg[i]){
case '2':
strcpy(letters[i], "abc");
break;
case '3':
strcpy(letters[i], "def");
break;
case '4':
strcpy(letters[i], "ghi");
break;
case '5':
strcpy(letters[i], "jkl");
break;
case '6':
strcpy(letters[i], "mno");
break;
case '7':
strcpy(letters[i], "pqrs");
break;
case '8':
strcpy(letters[i], "tuv");
break;
case '9':
strcpy(letters[i], "wxyz");
break;
}
total_combinations *= strlen(letters[i]);
}
// Print all letter sets
for (int i=0; i<n; i ){
printf("letters %d: %s\n", i, letters[i]);
}
// Declare array of sorted combinations (total_combinations rows, n 1 columns)
// and an array keeping track of the current combination
char combinations[total_combinations][n 1];
int current_comb[n];
// Initialize current_comb to zeros
for (int i = 0; i < n; i ) current_comb[i] = 0;
// Generate all combinations
int k = 0;
while (k < total_combinations){
// Store current combination
for (int set_idx = 0; set_idx < n; set_idx ){
int letter_idx = current_comb[set_idx];
combinations[k][set_idx] = letters[set_idx][letter_idx];
}
// Null-terminate the string
combinations[k][n] = '\0';
// Generate next combination
// From the end of the combination indices
for (int i = n-1; i >= 0; i--){
// Increment index
current_comb[i] ;
// Set to zero if it exceeds the letters bounds, otherwise break out of the loop
if (current_comb[i] == strlen(letters[i])){
current_comb[i] = 0;
}
else {
break;
}
}
// Increment combinations count
k ;
}
// Print all combinations
for (int i=0; i<total_combinations; i ){
printf("%s\n", combinations[i]);
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/522244.html
標籤:数组C
上一篇:C宏在函式呼叫中插入引數
下一篇:始終打開資料庫事務可以嗎?
