不包含指定字首的 N 位數的個數
本問題旨在確定長度為 N 的字串的總數,這些字串包含字元“0”到“9”,給定一個整數 N 和一個字串字首陣列 pre[],這樣任何字串都不能包含所提供的字首。本文旨在實現一個程式來查詢不包含給定字首的 N 位數的個數。
在 C 語言中,各種字串的集合被稱為陣列,因為陣列是相似型別資料的線性分組。
眾所周知,字串是一個字元一個字元的,一維陣列,以空字元或 null 字元結尾。
示例 1
讓我們輸入 N = 2,
The given prefix, pre = {“1”}
Output obtained: 90
解釋
這裡所有兩位數字符串,除了 {"01","10",“11”, “12”, “13", “14”, “15”, “16”, “17”, “18”, “19”, "21", "31", "41", "51", "61", "71", "81", "91"} 都是有效的。
示例 2
讓我們輸入 N = 3,
The given prefix, pre = {“56”}
Output obtained: 990
解釋
這裡所有三位數字符串,除了 {"560", "561", “562”, “563", “564”, “565”, “566”, “567”, “568”, “569”} 都是有效的。
示例 3
讓我們輸入 N = 1,
The given prefix, pre = {“6”}
Output obtained: 9
解釋
這裡所有一位數字符串,除了 {"6"} 都是有效的。
問題陳述
實現一個程式來查詢不包含給定字首的 N 位數的個數。
方法
為了找到不包含給定字首的 N 位數的個數,我們使用以下方法。
解決這個問題並找到不包含給定字首的 N 位數個數的方法
鑑於字串中每個位置都有 10 個字元選項,因此共有 (10N) 個潛在字串。與其計算所需字串的總數,不如減去不需要的字串的總數。在遍歷字首之前,將初始字元相同的較長前綴合並,可能會消除某些重複。
演算法
下面給出了查詢不包含給定字首的 N 位數個數的演算法
步驟 1 − 開始
步驟 2 − 定義函式以計算長度為 N 的字串總數,不包含給定字首
步驟 3 − 計算存在的字串總數
步驟 4 − 分別建立陣列和計數器 a 和 aCount,並將這些具有相同的字首插入其中
步驟 5 − 建立一個新的字首字串陣列
步驟 6 − 迭代每個起始字元
步驟 7 − 迭代陣列以計算最小大小的字首
步驟 8 − 現在將所有這些最小字首放入新的字首陣列中
步驟 9 − 迭代新的字首
步驟 10 − 減去不需要的字串
步驟 11 − 列印獲得的結果
步驟 12 − 停止
示例:C 程式
這是上面編寫的演算法的 C 語言程式實現,用於查詢不包含給定字首的 N 位數的個數。
#include <stdio.h>
#include <math.h>
#include <string.h>
#define MAX_LENGTH 10
// Function to calculate total strings of length N without the given prefixes
int totalStrings(int N, char pre[][MAX_LENGTH], int pre_Count){
// Calculate total strings present
int total = (int)(pow(10, N) + 0.5);
// Make an array and counter a and aCount respectively and insert these prefixes with same character in the array
char a[10][MAX_LENGTH];
int aCount[10] = {0};
for (int i = 0; i < pre_Count; i++) {
int index = pre[i][0] - '0';
strcpy(a[index] + aCount[index] * MAX_LENGTH, pre[i]);
aCount[index]++;
}
// Make a new array of prefixes strings
char new_pre[pre_Count][MAX_LENGTH];
int new_pre_count = 0;
// Iterating for each of the starting //character
for (int x = 0; x < 10; x++){
int m = N;
// Iterate over the array to calculate minimum size prefix
for (int j = 0; j < aCount[x]; j++){
int p_length = strlen(a[x] + j * MAX_LENGTH);
m = (m < p_length) ? m : p_length;
}
// now take all these minimum prefixes in the new array of prefixes
for (int j = 0; j < aCount[x]; j++){
int p_length = strlen(a[x] + j * MAX_LENGTH);
if (p_length <= m){
strcpy(new_pre[new_pre_count], a[x] + j * MAX_LENGTH);
new_pre_count++;
}
}
}
// Iterating through the new prefixes
for (int i = 0; i < new_pre_count; i++){
// Subtract the unwanted strings
total -= (int)(pow(10, N - strlen(new_pre[i])) + 0.5);
}
return total;
}
// The main function
int main(){
int N = 5;
char pre[][MAX_LENGTH] = {"1", "0", "2"};
int pre_Count = sizeof(pre) / sizeof(pre[0]);
printf("%d\n", totalStrings(N, pre, pre_Count));
return 0;
}
輸出
70000
結論
同樣,我們可以找到不包含給定字首的 N 位數的個數。
本文解決了獲取查詢不包含給定字首的 N 位數個數的程式的挑戰。
這裡提供了 C 語言程式碼以及查詢不包含給定字首的 N 位數個數的演算法。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP