首頁  >  文章  >  後端開發  >  計算不具有給定前綴的N位數字的數量

計算不具有給定前綴的N位數字的數量

WBOY
WBOY轉載
2023-09-03 19:53:061267瀏覽

計算不具有給定前綴的N位數字的數量

這裡的問題是確定長度為N的字串中包含的字元'0'到'9'的總數,提​​供一個整數N和一個字串前綴數組pre[] ,使得這些字串中沒有任何一個包含提供的前綴。本文的目的是實現一個程序,找到不具有給定前綴的N位數的數量。

在C程式語言中,一組不同的字串稱為數組,因為數組是一組具有相似類型的資料片段的線性組合。

As we already know, the string is a character-by-character, one-dimensional array that ends with an empty or a null character.

範例範例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位數字串都是有效的。

範例範例2

讓我們將輸入值 N = 3 作為範例。

The given prefix, pre = {“56”}
Output obtained: 990

解釋

在這裡,除了{"560", "561", “562”, “563", “564”, “565”, “566”, “567”, “568”, “569”}之外的所有3位數字字串都是有效的。

範例範例3

讓我們來看一個輸入N = 1,

The given prefix, pre = {“6”}
Output obtained: 9

解釋

除了{"6"}之外,這裡的所有1位數字串都是有效的。

問題陳述

實作一個程式來找出不具有給定前綴的N位數的數量。

方法

為了找到不帶給定前綴的N位數的數量,我們使用以下方法。

解決這個問題並找到不具有給定前綴的N位數的方法

考慮到字串中每個位置有10個字元選項,總共有(10N)個潛在字串。不要計算所需字串的總數,而是減去不需要的字串的總數。在迭代前將具有相同初始字元的前綴合併為較長的前綴可能會導致某些重複的刪除。

演算法

找到不具有以下給定前綴的N位數的計數演算法

  • 第一步 − 開始

  • 第二步 - 定義函數來計算長度為N的字串中不包含給定前綴的總數

  • #第三步 - 計算總共存在的字串

  • 第四步 - 建立一個陣列和計數器 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位數字計數的演算法。

以上是計算不具有給定前綴的N位數字的數量的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除