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

📅  最后修改于: 2021-09-07 04:49:41             🧑  作者: Mango

给定一个数字作为字符串,其中一些数字被‘$’替换,任务是通过用给定字符串的任何数字替换‘$’来生成所有可能的数字。
例子:

方法:

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

下面是上述方法的实现:

C
// 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 character 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 character is a '$' then replace
        // it with the other digits of the string and
        // recursively find all the combinations
        // Else go to the next character 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 character $ in the input string
    char pre[MAX];
 
    end = 0;
 
    // Traverse the input string and check if
    // the current character 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 character
            // 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 Classes Live