📌  相关文章
📜  从字符串与任何其他数字代替“$”打印字符串的所有可能的组合

📅  最后修改于: 2021-05-25 18:53:58             🧑  作者: Mango

给定一个数字字符串,其中一些数字用“ $”代替,任务是通过用给定字符串的任何数字替换“ $”来生成所有可能的数字。

例子:

方法:

  • 找到字符串的所有组合通过与任何字符串的数字替换字符$,在本次检查,如果当前字符是一个数字,如果是,那么这个字符存储到一个数组预[]然后递归找到所有的组合否则,如果当前字符为“ $”,则将其替换为存储在数组中的数字,然后递归查找所有组合。
  • 要查找所有可能的数字,请初始化存储所有可能数字的数组set [] ,要生成数字,请使用两个嵌套循环,其中外循环用于输入字符串,内循环用于存储所有数字可能组合的数组set [] 。初始化布尔标志来检查是否输入字符串的字符是在已经存在[]或不如果输入字符串的字符是在已经存在[]然后设定标志= FALSE否则,如果标志为真然后移动的当前字符输入要设置set []的字符串,然后递归地找到输入字符串的所有组合,并将其存储在数组set []中。最后打印数组集中的每个数字[]

下面是上述方法的实现:

// C++ implementation of the approach
#include 
#include 
#include 
#define MAX 20
#define DIGITS 10
  
// Array to store all the
// possible numbers
char set[DIGITS];
  
// Index to set[] element
int end;
  
// Function to find all the combinations
// of the string by replacing '$' with
// the other digits of the string
void combinations(char* num, char* pre, int curr, int lvl)
{
  
    // Check if current length is less than
    // the length of the input string
    if (curr < strlen(num)) {
  
        // If current charecter is a digit
        // then store digit into pre[] and
        // recursively find all the combinations
        if (num[curr] >= '0' && num[curr] <= '9') {
            pre[lvl] = num[curr];
            combinations(num, pre, curr + 1, lvl + 1);
        }
  
        // If current charecter is a '$' then replace
        // it with the other digits of the string and
        // recursively find all the combinations
        // Else go to the next charecter and
        // recursively find all the combinations
        else if (num[curr] == '$')
            for (int i = 0; i < end; i++) {
                pre[lvl] = set[i];
                combinations(num, pre, curr + 1, lvl + 1);
            }
        else
            combinations(num, pre, curr + 1, lvl);
    }
  
    // Print the array pre[]
    else {
        pre[lvl] = '\0';
        printf("%s\n", pre);
    }
}
  
// Function to find all the numbers formed
// from the input string by replacing '$' with
// all the digits of the input string
int findNumUtil(char num[])
{
    // Array that stores the digits before
    // the charecter $ in the input string
    char pre[MAX];
  
    end = 0;
  
    // Traverse the input string and check if
    // the current charecter is a digit
    // if it is then set flag to true
    for (int i = 0; i < strlen(num); i++)
        if (num[i] >= '0' && num[i] <= '9') {
            bool flag = true;
  
            // Check if current character of the input
            // string is already present in the array set[]
            // then set flag to false
            for (int j = 0; j < end; j++)
                if (set[j] == num[i])
                    flag = false;
  
            // Flag is true then store the charecter
            // into set[] and recursively find all
            // the combinations of numbers and store
            // it in the set[] array
  
            if (flag == true)
                set[end++] = num[i];
        }
  
    combinations(num, pre, 0, 0);
  
    return 0;
}
  
// Function to print all the combinations
// of the numbers by replacing '$' with
// the other digits of the input string
int findNum(char* num, int* result_count)
{
    int i;
    if (num[i]) {
        result_count[i] = findNumUtil(num);
        return (result_count[i]);
    }
    return 0;
}
  
// Driver code
int main()
{
    char num[MAX] = "23$$";
    int result_count[MAX];
  
    findNum(num, result_count);
  
    return 0;
}
输出:
2322
2323
2332
2333

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。